|
32 | 32 | import org.codehaus.groovy.runtime.DefaultGroovyMethods; |
33 | 33 | import org.codehaus.groovy.runtime.typehandling.NumberMath; |
34 | 34 |
|
35 | | -import java.lang.reflect.Field; |
36 | | -import java.lang.reflect.Modifier; |
37 | 35 | import java.math.BigDecimal; |
38 | 36 | import java.util.List; |
39 | 37 | import java.util.ListIterator; |
@@ -258,33 +256,36 @@ public static Expression transformInlineConstants(final Expression exp) { |
258 | 256 | * <li>Binary expressions - string concatenation and numeric +, -, /, *</li> |
259 | 257 | * <li>List expressions - list of constants</li> |
260 | 258 | * </ul> |
| 259 | + * |
261 | 260 | * @param exp the original expression |
262 | 261 | * @param attrType the type that the final constant should be |
263 | 262 | * @return the transformed type or the original if no transformation was possible |
264 | 263 | */ |
265 | 264 | public static Expression transformInlineConstants(final Expression exp, final ClassNode attrType) { |
266 | 265 | if (exp instanceof PropertyExpression) { |
267 | 266 | PropertyExpression pe = (PropertyExpression) exp; |
268 | | - ClassNode type = pe.getObjectExpression().getType(); |
269 | | - if (pe.getObjectExpression() instanceof ClassExpression && !type.isEnum()) { |
270 | | - if (type.isPrimaryClassNode()) { |
271 | | - FieldNode fn = type.getField(pe.getPropertyAsString()); |
272 | | - if (fn != null && fn.isStatic() && fn.isFinal()) { |
273 | | - Expression e = transformInlineConstants(fn.getInitialValueExpression(), attrType); |
274 | | - if (e != null) { |
275 | | - return e; |
276 | | - } |
| 267 | + Expression e = pe.getObjectExpression(); |
| 268 | + ClassNode cn; |
| 269 | + FieldNode fn; |
| 270 | + if (e instanceof ClassExpression |
| 271 | + && !(cn = e.getType().redirect()).isEnum() |
| 272 | + && (cn.isPrimaryClassNode() || cn.isResolved()) |
| 273 | + && (fn = ClassNodeUtils.getField(cn, pe.getPropertyAsString())) != null |
| 274 | + && fn.isStatic() |
| 275 | + && fn.isFinal()) { |
| 276 | + if (fn.hasInitialExpression()) { |
| 277 | + e = transformInlineConstants(fn.getInitialValueExpression(), attrType); |
| 278 | + if (e instanceof ConstantExpression) { |
| 279 | + return e; |
277 | 280 | } |
278 | | - } else if (type.isResolved()) { |
| 281 | + } else if (cn.isResolved()) { |
279 | 282 | try { |
280 | | - Field field = type.redirect().getTypeClass().getField(pe.getPropertyAsString()); |
281 | | - if (field != null && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) { |
282 | | - ConstantExpression ce = new ConstantExpression(field.get(null), true); |
283 | | - configure(exp, ce); |
284 | | - return ce; |
| 283 | + var field = cn.getTypeClass().getField(pe.getPropertyAsString()); |
| 284 | + if (field != null) { |
| 285 | + return configure(exp, new ConstantExpression(field.get(null), true)); |
285 | 286 | } |
286 | | - } catch (Exception | LinkageError e) { |
287 | | - // ignore, leave property expression in place and we'll report later |
| 287 | + } catch (Exception | LinkageError ignore) { |
| 288 | + // leave property expression and we will report later |
288 | 289 | } |
289 | 290 | } |
290 | 291 | } |
|
0 commit comments