1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jexl3.internal.introspection;
19
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Modifier;
22 import org.apache.commons.jexl3.introspection.JexlPropertySet;
23
24
25
26
27 public final class FieldSetExecutor implements JexlPropertySet {
28
29
30
31 private final Field field;
32
33
34
35
36
37
38
39
40
41
42 public static JexlPropertySet discover(final Introspector is,
43 final Class<?> clazz,
44 final String identifier,
45 final Object value) {
46 if (identifier != null) {
47 final Field field = is.getField(clazz, identifier);
48 if (field != null
49 && !Modifier.isFinal(field.getModifiers())
50 && (value == null || MethodKey.isInvocationConvertible(field.getType(), value.getClass(), false))) {
51 return new FieldSetExecutor(field);
52 }
53 }
54 return null;
55 }
56
57
58
59
60
61 private FieldSetExecutor(final Field theField) {
62 field = theField;
63 }
64
65 @Override
66 public Object invoke(final Object obj, final Object arg) throws Exception {
67 field.set(obj, arg);
68 return arg;
69 }
70
71 @Override
72 public Object tryInvoke(final Object obj, final Object key, final Object value) {
73 if (obj.getClass().equals(field.getDeclaringClass())
74 && key.equals(field.getName())
75 && (value == null || MethodKey.isInvocationConvertible(field.getType(), value.getClass(), false))) {
76 try {
77 field.set(obj, value);
78 return value;
79 } catch (final IllegalAccessException xill) {
80 return Uberspect.TRY_FAILED;
81 }
82 }
83 return Uberspect.TRY_FAILED;
84 }
85
86 @Override
87 public boolean tryFailed(final Object rval) {
88 return rval == Uberspect.TRY_FAILED;
89 }
90
91 @Override
92 public boolean isCacheable() {
93 return true;
94 }
95 }