View Javadoc

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          super( id );
46      }
47  
48      public ASTVarRef( OgnlParser p, int id )
49      {
50          super( p, id );
51      }
52  
53      void setName( String name )
54      {
55          this.name = name;
56      }
57      /**
58       * Get the variable name.
59       *
60       * @return the variable name.
61       * @since 4.0
62       */
63      String getName()
64      {
65          return name;
66      }
67  
68      protected Object getValueBody( OgnlContext context, Object source )
69          throws OgnlException
70      {
71          return context.get( name );
72      }
73  
74      protected void setValueBody( OgnlContext context, Object target, Object value )
75          throws OgnlException
76      {
77          context.put( name, value );
78      }
79  
80      public Class getGetterClass()
81      {
82          return getterClass;
83      }
84  
85      public Class getSetterClass()
86      {
87          return null;
88      }
89  
90      public String getCoreExpression()
91      {
92          return core;
93      }
94  
95      public String getLastExpression()
96      {
97          return last;
98      }
99  
100     public String toGetSourceString( OgnlContext context, Object target )
101     {
102         Object value = context.get( name );
103 
104         if ( value != null )
105         {
106 
107             getterClass = value.getClass();
108         }
109 
110         context.setCurrentType( getterClass );
111         context.setCurrentAccessor( context.getClass() );
112 
113         context.setCurrentObject( value );
114         // context.setRoot(context.get(name));
115 
116         if ( context.getCurrentObject() == null )
117         {
118             throw new UnsupportedCompilationException( "Current context object is null, can't compile var reference." );
119         }
120         
121         String pre = "";
122         String post = "";
123         if ( context.getCurrentType() != null )
124         {
125             pre = "((" + OgnlRuntime.getCompiler( context ).getInterfaceClass( context.getCurrentType() ).getName() + ")";
126             post = ")";
127         }
128 
129         if ( parent != null && ASTAssign.class.isInstance( parent ) )
130         {
131             core = "$1.put(\"" + name + "\",";
132             last = pre + "$1.get(\"" + name + "\")" + post;
133 
134             return core;
135         }
136 
137         return pre + "$1.get(\"" + name + "\")" + post;
138     }
139 
140     public String toSetSourceString( OgnlContext context, Object target )
141     {
142         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         return visitor.visit( this, data );
149     }
150 }