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
40
41
42 public ObjectContext(final JexlEngine engine, final T wrapped) {
43 this.jexl = engine;
44 this.object = wrapped;
45 }
46
47 @Override
48 public Object get(final String name) {
49 final JexlPropertyGet jget = jexl.getUberspect().getPropertyGet(object, name);
50 if (jget != null) {
51 try {
52 return jget.invoke(object);
53 } catch (final Exception xany) {
54 if (jexl.isStrict()) {
55 throw new JexlException.Property(null, name, true, xany);
56 }
57 }
58 }
59 return null;
60 }
61
62
63
64
65
66
67 protected JexlEngine getJexl() {
68 return jexl;
69 }
70
71
72
73
74
75 protected T getObject() {
76 return object;
77 }
78
79 @Override
80 public boolean has(final String name) {
81 return jexl.getUberspect().getPropertyGet(object, name) != null;
82 }
83
84 @Override
85 public Object resolveNamespace(final String name) {
86 if (name == null || name.isEmpty()) {
87 return object;
88 }
89 return null;
90 }
91
92 @Override
93 public void set(final String name, final Object value) {
94 final JexlPropertySet jset = jexl.getUberspect().getPropertySet(object, name, value);
95 if (jset != null) {
96 try {
97 jset.invoke(object, value);
98 } catch (final Exception xany) {
99
100 if (jexl.isStrict()) {
101 throw new JexlException.Property(null, name, true, xany);
102 }
103 }
104 }
105 }
106 }