-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathReflectionObjectHandler.java
More file actions
220 lines (200 loc) · 8.13 KB
/
Copy pathReflectionObjectHandler.java
File metadata and controls
220 lines (200 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.github.mustachejava.reflect;
import com.github.mustachejava.Binding;
import com.github.mustachejava.Code;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.ObjectHandler;
import com.github.mustachejava.TemplateContext;
import com.github.mustachejava.reflect.guards.ClassGuard;
import com.github.mustachejava.reflect.guards.DepthGuard;
import com.github.mustachejava.reflect.guards.DotGuard;
import com.github.mustachejava.reflect.guards.MapGuard;
import com.github.mustachejava.reflect.guards.NullGuard;
import com.github.mustachejava.reflect.guards.WrappedGuard;
import com.github.mustachejava.util.GuardException;
import com.github.mustachejava.util.Wrapper;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import static java.util.Collections.singletonList;
/**
* Lookup objects using reflection and execute them the same way.
*
* User: sam
* Date: 7/24/11
* Time: 3:02 PM
*/
public class ReflectionObjectHandler extends BaseObjectHandler {
private static final Wrapper[] EMPTY_WRAPPERS = new Wrapper[0];
private static final Guard[] EMPTY_GUARDS = new Guard[0];
private static final Wrapper CALLABLE_WRAPPER = scopes -> {
Object scope = scopes.get(0);
if (!(scope instanceof Callable)) {
throw new GuardException();
}
try {
return ((Callable<?>) scope).call();
} catch (Exception e) {
throw new MustacheException("Failed to invoke callable while resolving dotted name", e);
}
};
protected static final Method MAP_METHOD;
public static Object unwrap(ObjectHandler oh, int scopeIndex, Wrapper[] wrappers, List<Object> scopes) throws GuardException {
Object scope = oh.coerce(scopes.get(scopeIndex));
// The value may be buried by . notation
if (wrappers != null) {
for (Wrapper wrapper : wrappers) {
scope = oh.coerce(wrapper.call(ObjectHandler.makeList(scope)));
}
}
return scope;
}
static {
try {
MAP_METHOD = Map.class.getMethod("get", Object.class);
} catch (NoSuchMethodException e) {
throw new AssertionError(e);
}
}
@SuppressWarnings("unchecked")
@Override
public Wrapper find(String name, final List<Object> scopes) {
Wrapper wrapper = null;
final int length = scopes.size();
List<Guard> guards = new ArrayList<>(length);
// Simple guard to break if the number of scopes at this call site have changed
guards.add(createDepthGuard(length));
NEXT:
for (int i = length - 1; i >= 0; i--) {
Object scope = scopes.get(i);
if (scope == null) continue;
// Make sure that the current scope is the same class
guards.add(createClassGuard(i, scope));
List<Wrapper> wrappers = null;
int dotIndex;
String subname = name;
// Try and find a wrapper using the simple name
wrapper = findWrapper(i, null, guards, scope, subname);
if (wrapper != null) {
break;
}
// If there is dot notation, start evaluating it
while ((dotIndex = subname.indexOf('.')) != -1) {
final String lookup = subname.substring(0, dotIndex);
subname = subname.substring(dotIndex + 1);
// This is used for lookups but otherwise always succeeds
guards.add(createDotGuard(i, scope, lookup));
List<Guard> wrapperGuard = new ArrayList<>(1);
// We need to coerce this scope as it is checked post coercion
wrapperGuard.add(createClassGuard(0, coerce(scope)));
wrapper = findWrapper(0, null, wrapperGuard, scope, lookup);
if (wrappers == null) wrappers = new ArrayList<>();
if (wrapper != null) {
// We need to dig into a scope when dot notation shows up
wrappers.add(wrapper);
try {
// Pull out the next level from the coerced scope
scope = coerce(wrapper.call(ObjectHandler.makeList(coerce(scope))));
while (scope instanceof Callable) {
wrappers.add(CALLABLE_WRAPPER);
scope = coerce(CALLABLE_WRAPPER.call(ObjectHandler.makeList(scope)));
}
} catch (GuardException e) {
throw new AssertionError(e);
}
} else {
// Failed to find a wrapper for the next dot
guards.add(createWrappedGuard(i, wrappers, wrapperGuard));
continue NEXT;
}
if (scope == null) {
// Found a wrapper, but the result of was null
guards.add(createWrappedGuard(i, wrappers, singletonList(createNullGuard())));
// Break here to allow the wrapper to be returned with the partial evaluation of the dot notation
break;
}
}
if (wrappers != null) {
guards.add(createWrappedGuard(i, wrappers, singletonList(new ClassGuard(0, scope))));
}
Wrapper[] foundWrappers = wrappers == null ? null : wrappers.toArray(EMPTY_WRAPPERS);
wrapper = findWrapper(i, foundWrappers, guards, scope, subname);
if (wrapper == null) {
// If we have found any wrappers we need to keep them rather than return a missing wrapper
// otherwise it will continue you on to other scopes and break context precedence
if (wrappers != null) {
wrapper = createMissingWrapper(subname, guards);
break;
}
} else {
break;
}
}
return wrapper == null ? createMissingWrapper(name, guards) : wrapper;
}
/**
* Find a wrapper given the current context. If not found, return null.
*
* @param scopeIndex the index into the scope array
* @param wrappers the current set of wrappers to get here
* @param guards the list of guards used to find this
* @param scope the current scope
* @param name the name in the scope
* @return null if not found, otherwise a wrapper for this scope and name
*/
protected Wrapper findWrapper(final int scopeIndex, Wrapper[] wrappers, List<Guard> guards, Object scope, final String name) {
scope = coerce(scope);
if (scope == null) return null;
// If the scope is a map, then we use the get() method
// to see if it contains a value named name.
if (scope instanceof Map) {
Map map = (Map) scope;
if (map.containsKey(name)) {
guards.add(createMapGuard(scopeIndex, wrappers, name, true));
return createWrapper(scopeIndex, wrappers, guards, MAP_METHOD, new Object[]{name});
} else {
guards.add(createMapGuard(scopeIndex, wrappers, name, false));
if (!areMethodsAccessible(map)) {
return null;
}
}
}
AccessibleObject member = findMember(scope.getClass(), name);
return member == null ? null : createWrapper(scopeIndex, wrappers, guards, member, new Object[0]);
}
// Factories
protected MissingWrapper createMissingWrapper(String name, List<Guard> guards) {
return new MissingWrapper(name, guards.toArray(EMPTY_GUARDS));
}
protected DotGuard createDotGuard(int i, Object scope, String lookup) {
return new DotGuard(lookup, i, scope);
}
protected WrappedGuard createWrappedGuard(int i, List<Wrapper> wrappers, List<Guard> wrapperGuard) {
return new WrappedGuard(this, i, wrappers, wrapperGuard);
}
protected NullGuard createNullGuard() {
return new NullGuard();
}
protected DepthGuard createDepthGuard(int length) {
return new DepthGuard(length);
}
protected ClassGuard createClassGuard(int i, Object scope) {
return new ClassGuard(i, scope);
}
protected MapGuard createMapGuard(int scopeIndex, Wrapper[] wrappers, String name, boolean contains) {
return new MapGuard(this, scopeIndex, name, contains, wrappers);
}
@SuppressWarnings("unchecked")
protected Wrapper createWrapper(int scopeIndex, Wrapper[] wrappers, List<? extends Guard> guard, AccessibleObject member, Object[] arguments) {
return new ReflectionWrapper(scopeIndex, wrappers, guard.toArray(EMPTY_GUARDS), member, arguments, this);
}
@Override
public Binding createBinding(String name, TemplateContext tc, Code code) {
return new GuardedBinding(this, name, tc, code);
}
protected boolean areMethodsAccessible(Map<?, ?> map) {
return false;
}
}