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