Coverage Report - org.apache.commons.ognl.ASTVarRef
 
Classes in this File Line Coverage Branch Coverage Complexity
ASTVarRef
85%
29/34
80%
8/10
1.538
 
 1  
 package org.apache.commons.ognl;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.ognl.enhance.OrderedReturn;
 23  
 import org.apache.commons.ognl.enhance.UnsupportedCompilationException;
 24  
 
 25  
 /**
 26  
  * $Id: ASTVarRef.java 1194869 2011-10-29 11:10:16Z mcucchiara $
 27  
  * @author Luke Blanshard (blanshlu@netscape.net)
 28  
  * @author Drew Davidson (drew@ognl.org)
 29  
  */
 30  
 public class ASTVarRef
 31  
     extends SimpleNode
 32  
     implements NodeType, OrderedReturn
 33  
 {
 34  
 
 35  
     private String name;
 36  
 
 37  
     protected Class getterClass;
 38  
 
 39  
     protected String core;
 40  
 
 41  
     protected String last;
 42  
 
 43  
     public ASTVarRef( int id )
 44  
     {
 45  204
         super( id );
 46  204
     }
 47  
 
 48  
     public ASTVarRef( OgnlParser p, int id )
 49  
     {
 50  0
         super( p, id );
 51  0
     }
 52  
 
 53  
     void setName( String name )
 54  
     {
 55  204
         this.name = name;
 56  204
     }
 57  
     /**
 58  
      * Get the variable name.
 59  
      *
 60  
      * @return the variable name.
 61  
      * @since 4.0
 62  
      */
 63  
     String getName()
 64  
     {
 65  0
         return name;
 66  
     }
 67  
 
 68  
     protected Object getValueBody( OgnlContext context, Object source )
 69  
         throws OgnlException
 70  
     {
 71  164
         return context.get( name );
 72  
     }
 73  
 
 74  
     protected void setValueBody( OgnlContext context, Object target, Object value )
 75  
         throws OgnlException
 76  
     {
 77  47
         context.put( name, value );
 78  47
     }
 79  
 
 80  
     public Class getGetterClass()
 81  
     {
 82  30
         return getterClass;
 83  
     }
 84  
 
 85  
     public Class getSetterClass()
 86  
     {
 87  0
         return null;
 88  
     }
 89  
 
 90  
     public String getCoreExpression()
 91  
     {
 92  1
         return core;
 93  
     }
 94  
 
 95  
     public String getLastExpression()
 96  
     {
 97  8
         return last;
 98  
     }
 99  
 
 100  
     public String toGetSourceString( OgnlContext context, Object target )
 101  
     {
 102  30
         Object value = context.get( name );
 103  
 
 104  30
         if ( value != null )
 105  
         {
 106  
 
 107  14
             getterClass = value.getClass();
 108  
         }
 109  
 
 110  30
         context.setCurrentType( getterClass );
 111  30
         context.setCurrentAccessor( context.getClass() );
 112  
 
 113  30
         context.setCurrentObject( value );
 114  
         // context.setRoot(context.get(name));
 115  
 
 116  30
         if ( context.getCurrentObject() == null )
 117  
         {
 118  16
             throw new UnsupportedCompilationException( "Current context object is null, can't compile var reference." );
 119  
         }
 120  
         
 121  14
         String pre = "";
 122  14
         String post = "";
 123  14
         if ( context.getCurrentType() != null )
 124  
         {
 125  14
             pre = "((" + OgnlRuntime.getCompiler( context ).getInterfaceClass( context.getCurrentType() ).getName() + ")";
 126  14
             post = ")";
 127  
         }
 128  
 
 129  14
         if ( parent != null && ASTAssign.class.isInstance( parent ) )
 130  
         {
 131  1
             core = "$1.put(\"" + name + "\",";
 132  1
             last = pre + "$1.get(\"" + name + "\")" + post;
 133  
 
 134  1
             return core;
 135  
         }
 136  
 
 137  13
         return pre + "$1.get(\"" + name + "\")" + post;
 138  
     }
 139  
 
 140  
     public String toSetSourceString( OgnlContext context, Object target )
 141  
     {
 142  2
         return toGetSourceString( context, target );
 143  
     }
 144  
 
 145  
     public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
 146  
         throws OgnlException
 147  
     {
 148  0
         return visitor.visit( this, data );
 149  
     }
 150  
 }