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