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: ASTAssign.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  class ASTAssign
31      extends SimpleNode
32  {
33      public ASTAssign( int id )
34      {
35          super( id );
36      }
37  
38      public ASTAssign( OgnlParser p, int id )
39      {
40          super( p, id );
41      }
42  
43      protected Object getValueBody( OgnlContext context, Object source )
44          throws OgnlException
45      {
46          Object result = children[1].getValue( context, source );
47          children[0].setValue( context, source, result );
48          return result;
49      }
50  
51      public String toGetSourceString( OgnlContext context, Object target )
52      {
53          String result = "";
54  
55          String first = children[0].toGetSourceString( context, target );
56          String second = "";
57  
58          if ( ASTProperty.class.isInstance( children[1] ) )
59          {
60              second += "((" + OgnlRuntime.getCompiler( context ).getClassName( target.getClass() ) + ")$2).";
61          }
62  
63          second += children[1].toGetSourceString( context, target );
64  
65          if ( ASTSequence.class.isAssignableFrom( children[1].getClass() ) )
66          {
67              ASTSequence seq = (ASTSequence) children[1];
68  
69              context.setCurrentType( Object.class );
70  
71              String core = seq.getCoreExpression();
72              if ( core.endsWith( ";" ) )
73              {
74                  core = core.substring( 0, core.lastIndexOf( ";" ) );
75              }
76  
77              second =
78                  OgnlRuntime.getCompiler( context ).createLocalReference( context,
79                                                                  "org.apache.commons.ognl.OgnlOps.returnValue(($w)"
80                                                                      + core + ", ($w) " + seq.getLastExpression() + ")",
81                                                                  Object.class );
82          }
83  
84          if ( NodeType.class.isInstance( children[1] ) && !ASTProperty.class.isInstance( children[1] )
85              && ( (NodeType) children[1] ).getGetterClass() != null && !OrderedReturn.class.isInstance( children[1] ) )
86          {
87  
88              second = "new " + ( (NodeType) children[1] ).getGetterClass().getName() + "(" + second + ")";
89          }
90  
91          if ( OrderedReturn.class.isAssignableFrom( children[0].getClass() )
92              && ( (OrderedReturn) children[0] ).getCoreExpression() != null )
93          {
94              context.setCurrentType( Object.class );
95  
96              result = first + second + ")";
97  
98              // System.out.println("building ordered ret from child[0] with result of:" + result);
99  
100             result =
101                 OgnlRuntime
102                     .getCompiler( context )
103                     .createLocalReference( context,
104                                            "org.apache.commons.ognl.OgnlOps.returnValue(($w)" + result + ", ($w)"
105                                                + ( (OrderedReturn) children[0] ).getLastExpression() + ")",
106                                            Object.class );
107         }
108 
109         return result;
110     }
111 
112     public String toSetSourceString( OgnlContext context, Object target )
113     {
114         String result = "";
115 
116         result += children[0].toSetSourceString( context, target );
117 
118         if ( ASTProperty.class.isInstance( children[1] ) )
119         {
120             result += "((" + OgnlRuntime.getCompiler( context ).getClassName( target.getClass() ) + ")$2).";
121         }
122 
123         String value = children[1].toSetSourceString( context, target );
124 
125         if ( value == null )
126         {
127             throw new UnsupportedCompilationException(
128                 "Value for assignment is null, can't enhance statement to bytecode." );
129         }
130 
131         if ( ASTSequence.class.isAssignableFrom( children[1].getClass() ) )
132         {
133             ASTSequence seq = (ASTSequence) children[1];
134             result = seq.getCoreExpression() + result;
135             value = seq.getLastExpression();
136         }
137 
138         if ( NodeType.class.isInstance( children[1] ) && !ASTProperty.class.isInstance( children[1] )
139             && ( (NodeType) children[1] ).getGetterClass() != null )
140         {
141 
142             value = "new " + ( (NodeType) children[1] ).getGetterClass().getName() + "(" + value + ")";
143         }
144 
145         return result + value + ")";
146     }
147     
148     public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data )
149         throws OgnlException
150     {
151         return visitor.visit( this, data );
152     }
153 }