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.ExpressionCompiler;
23  import org.apache.commons.ognl.enhance.OgnlExpressionCompiler;
24  
25  
26  /**
27   * $Id: ASTMethodUtil.java 1194866 2011-10-29 10:51:32Z mcucchiara $
28   */
29  class ASTMethodUtil
30  {
31  
32      private ASTMethodUtil()
33      {
34      }
35  
36      static String getParmString( OgnlContext context, Object root, Node child, Class prevType )
37          throws OgnlException
38      {
39          String parmString = child.toGetSourceString( context, root );
40  
41          if ( parmString == null || parmString.trim().length() < 1 )
42          {
43              parmString = "null";
44          }
45  
46          // to undo type setting of constants when used as method parameters
47          if ( ASTConst.class.isInstance( child ) )
48          {
49              context.setCurrentType( prevType );
50          }
51  
52          parmString = ExpressionCompiler.getRootExpression( child, root, context ) + parmString;
53  
54          String cast = "";
55          if ( ExpressionCompiler.shouldCast( child ) )
56          {
57              cast = (String) context.remove( ExpressionCompiler.PRE_CAST );
58          }
59  
60          if ( cast == null )
61          {
62              cast = "";
63          }
64  
65          if ( !ASTConst.class.isInstance( child ) )
66          {
67              parmString = cast + parmString;
68          }
69          return parmString;
70      }
71  
72      static Class getValueClass( OgnlContext context, Object root, Node child )
73          throws OgnlException
74      {
75          Object value = child.getValue( context, root );
76          Class valueClass = value != null ? value.getClass() : null;
77          if ( NodeType.class.isAssignableFrom( child.getClass() ) )
78          {
79              valueClass = ( (NodeType) child ).getGetterClass();
80          }
81          return valueClass;
82      }
83  
84      static String getParmString( OgnlContext context, Class parm, String parmString, Node child, Class valueClass,
85                                   String endParam )
86      {
87          OgnlExpressionCompiler compiler = OgnlRuntime.getCompiler( context );
88          if ( parm.isArray() )
89          {
90              parmString = compiler.createLocalReference( context, "(" + ExpressionCompiler.getCastString( parm )
91                  + ")org.apache.commons.ognl.OgnlOps#toArray(" + parmString + ", " + parm.getComponentType().getName()
92                  + endParam, parm );
93  
94          }
95          else if ( parm.isPrimitive() )
96          {
97              Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass( parm );
98  
99              parmString = compiler.createLocalReference( context, "((" + wrapClass.getName()
100                 + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," + wrapClass.getName()
101                 + ".class, true))." + OgnlRuntime.getNumericValueGetter( wrapClass ), parm );
102 
103         }
104         else if ( parm != Object.class )
105         {
106             parmString = compiler.createLocalReference( context, "(" + parm.getName()
107                 + ")org.apache.commons.ognl.OgnlOps#convertValue(" + parmString + "," + parm.getName() + ".class)",
108                                                         parm );
109 
110         }
111         else if ( ( NodeType.class.isInstance( child ) && ( (NodeType) child ).getGetterClass() != null
112             && Number.class.isAssignableFrom( ( (NodeType) child ).getGetterClass() ) ) || ( valueClass != null
113             && valueClass.isPrimitive() ) )
114         {
115             parmString = " ($w) " + parmString;
116         }
117         else if ( valueClass != null && valueClass.isPrimitive() )
118         {
119             parmString = "($w) " + parmString;
120         }
121         return parmString;
122     }
123 }