1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3;
18
19 import org.apache.commons.jexl3.introspection.JexlPropertyGet;
20 import org.apache.commons.jexl3.introspection.JexlPropertySet;
21
22
23
24
25
26
27
28 public class ObjectContext<T> implements JexlContext, JexlContext.NamespaceResolver {
29
30
31 private final JexlEngine jexl;
32
33
34 private final T object;
35
36
37
38
39 protected JexlEngine getJexl() {
40 return jexl;
41 }
42
43
44
45
46 protected T getObject() {
47 return object;
48 }
49
50
51
52
53
54
55
56 public ObjectContext(final JexlEngine engine, final T wrapped) {
57 this.jexl = engine;
58 this.object = wrapped;
59 }
60
61 @Override
62 public Object get(final String name) {
63 final JexlPropertyGet jget = jexl.getUberspect().getPropertyGet(object, name);
64 if (jget != null) {
65 try {
66 return jget.invoke(object);
67 } catch (final Exception xany) {
68 if (jexl.isStrict()) {
69 throw new JexlException.Property(null, name, true, xany);
70 }
71 }
72 }
73 return null;
74 }
75
76 @Override
77 public void set(final String name, final Object value) {
78 final JexlPropertySet jset = jexl.getUberspect().getPropertySet(object, name, value);
79 if (jset != null) {
80 try {
81 jset.invoke(object, value);
82 } catch (final Exception xany) {
83
84 if (jexl.isStrict()) {
85 throw new JexlException.Property(null, name, true, xany);
86 }
87 }
88 }
89 }
90
91 @Override
92 public boolean has(final String name) {
93 return jexl.getUberspect().getPropertyGet(object, name) != null;
94 }
95
96 @Override
97 public Object resolveNamespace(final String name) {
98 if (name == null || name.isEmpty()) {
99 return object;
100 }
101 return null;
102 }
103 }