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 java.lang.reflect.Field; |
23 | |
import java.lang.reflect.Modifier; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class ASTStaticField |
31 | |
extends SimpleNode |
32 | |
implements NodeType |
33 | |
{ |
34 | |
|
35 | |
private String className; |
36 | |
|
37 | |
private String fieldName; |
38 | |
|
39 | |
private Class getterClass; |
40 | |
|
41 | |
public ASTStaticField( int id ) |
42 | |
{ |
43 | 40 | super( id ); |
44 | 40 | } |
45 | |
|
46 | |
public ASTStaticField( OgnlParser p, int id ) |
47 | |
{ |
48 | 0 | super( p, id ); |
49 | 0 | } |
50 | |
|
51 | |
|
52 | |
void init( String className, String fieldName ) |
53 | |
{ |
54 | 40 | this.className = className; |
55 | 40 | this.fieldName = fieldName; |
56 | 40 | } |
57 | |
|
58 | |
protected Object getValueBody( OgnlContext context, Object source ) |
59 | |
throws OgnlException |
60 | |
{ |
61 | 11 | return OgnlRuntime.getStaticField( context, className, fieldName ); |
62 | |
} |
63 | |
|
64 | |
public boolean isNodeConstant( OgnlContext context ) |
65 | |
throws OgnlException |
66 | |
{ |
67 | 15 | boolean result = false; |
68 | 15 | Exception cause = null; |
69 | |
|
70 | |
try |
71 | |
{ |
72 | 15 | Class clazz = OgnlRuntime.classForName( context, className ); |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 15 | if ( "class".equals( fieldName ) ) |
79 | |
{ |
80 | 0 | result = true; |
81 | |
} |
82 | 15 | else if ( clazz.isEnum() ) |
83 | |
{ |
84 | 2 | result = true; |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | 13 | Field field = clazz.getField( fieldName ); |
89 | |
|
90 | 13 | if ( !Modifier.isStatic( field.getModifiers() ) ) |
91 | |
{ |
92 | 0 | throw new OgnlException( "Field " + fieldName + " of class " + className + " is not static" ); |
93 | |
} |
94 | 13 | result = Modifier.isFinal( field.getModifiers() ); |
95 | |
} |
96 | |
} |
97 | 0 | catch ( ClassNotFoundException e ) |
98 | |
{ |
99 | 0 | cause = e; |
100 | |
} |
101 | 0 | catch ( NoSuchFieldException e ) |
102 | |
{ |
103 | 0 | cause = e; |
104 | |
} |
105 | 0 | catch ( SecurityException e ) |
106 | |
{ |
107 | 0 | cause = e; |
108 | 15 | } |
109 | |
|
110 | 15 | if ( cause != null ) |
111 | |
{ |
112 | 0 | throw new OgnlException( "Could not get static field " + fieldName + " from class " + className, cause ); |
113 | |
} |
114 | |
|
115 | 15 | return result; |
116 | |
} |
117 | |
|
118 | |
Class getFieldClass( OgnlContext context ) |
119 | |
throws OgnlException |
120 | |
{ |
121 | |
Exception cause; |
122 | |
|
123 | |
try |
124 | |
{ |
125 | 39 | Class clazz = OgnlRuntime.classForName( context, className ); |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | 39 | if ( "class".equals( fieldName ) ) |
132 | |
{ |
133 | 12 | return clazz; |
134 | |
} |
135 | 27 | else if ( clazz.isEnum() ) |
136 | |
{ |
137 | 5 | return clazz; |
138 | |
} |
139 | |
else |
140 | |
{ |
141 | 22 | Field field = clazz.getField( fieldName ); |
142 | |
|
143 | 22 | return field.getType(); |
144 | |
} |
145 | |
} |
146 | 0 | catch ( ClassNotFoundException e ) |
147 | |
{ |
148 | 0 | cause = e; |
149 | |
} |
150 | 0 | catch ( NoSuchFieldException e ) |
151 | |
{ |
152 | 0 | cause = e; |
153 | |
} |
154 | 0 | catch ( SecurityException e ) |
155 | |
{ |
156 | 0 | cause = e; |
157 | 0 | } |
158 | 0 | throw new OgnlException( "Could not get static field " + fieldName + " from class " + className, cause ); |
159 | |
} |
160 | |
|
161 | |
public Class getGetterClass() |
162 | |
{ |
163 | 43 | return getterClass; |
164 | |
} |
165 | |
|
166 | |
public Class getSetterClass() |
167 | |
{ |
168 | 0 | return getterClass; |
169 | |
} |
170 | |
|
171 | |
public String toGetSourceString( OgnlContext context, Object target ) |
172 | |
{ |
173 | |
try |
174 | |
{ |
175 | |
|
176 | 29 | Object obj = OgnlRuntime.getStaticField( context, className, fieldName ); |
177 | |
|
178 | 29 | context.setCurrentObject( obj ); |
179 | |
|
180 | 29 | getterClass = getFieldClass( context ); |
181 | |
|
182 | 29 | context.setCurrentType( getterClass ); |
183 | |
|
184 | |
} |
185 | 0 | catch ( Throwable t ) |
186 | |
{ |
187 | 0 | throw OgnlOps.castToRuntime( t ); |
188 | 29 | } |
189 | |
|
190 | 29 | return className + "." + fieldName; |
191 | |
} |
192 | |
|
193 | |
public String toSetSourceString( OgnlContext context, Object target ) |
194 | |
{ |
195 | |
try |
196 | |
{ |
197 | |
|
198 | 10 | Object obj = OgnlRuntime.getStaticField( context, className, fieldName ); |
199 | |
|
200 | 10 | context.setCurrentObject( obj ); |
201 | |
|
202 | 10 | getterClass = getFieldClass( context ); |
203 | |
|
204 | 10 | context.setCurrentType( getterClass ); |
205 | |
|
206 | |
} |
207 | 0 | catch ( Throwable t ) |
208 | |
{ |
209 | 0 | throw OgnlOps.castToRuntime( t ); |
210 | 10 | } |
211 | |
|
212 | 10 | return className + "." + fieldName; |
213 | |
} |
214 | |
|
215 | |
public <R, P> R accept( NodeVisitor<? extends R, ? super P> visitor, P data ) |
216 | |
throws OgnlException |
217 | |
{ |
218 | 0 | return visitor.visit( this, data ); |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
String getFieldName() |
228 | |
{ |
229 | 0 | return fieldName; |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
String getClassName() |
239 | |
{ |
240 | 0 | return className; |
241 | |
} |
242 | |
} |