forked from spullara/mustache.java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionWrapper.java
More file actions
128 lines (112 loc) · 3.83 KB
/
ReflectionWrapper.java
File metadata and controls
128 lines (112 loc) · 3.83 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
package com.github.mustachejava.reflect;
import com.github.mustachejava.MustacheException;
import com.github.mustachejava.ObjectHandler;
import com.github.mustachejava.util.GuardException;
import com.github.mustachejava.util.Wrapper;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
/**
* Used for evaluating values at a callsite
*/
public class ReflectionWrapper extends GuardedWrapper {
// Context
protected final int scopeIndex;
protected final Wrapper[] wrappers;
protected final ObjectHandler oh;
// Dispatch
protected final Method method;
protected final Field field;
protected final Object[] arguments;
public ReflectionWrapper(int scopeIndex, Wrapper[] wrappers, Guard[] guard, AccessibleObject method, Object[] arguments, ObjectHandler oh) {
super(guard);
this.wrappers = wrappers;
this.oh = oh;
if (method instanceof Field) {
this.method = null;
this.field = (Field) method;
} else {
this.method = (Method) method;
this.field = null;
}
this.arguments = arguments;
this.scopeIndex = scopeIndex;
}
protected Object unwrap(List<Object> scopes) {
if (wrappers == null || wrappers.length == 0) {
return scopes.get(scopeIndex);
} else {
return ReflectionObjectHandler.unwrap(oh, scopeIndex, wrappers, scopes);
}
}
@Override
public Object call(List<Object> scopes) throws GuardException {
guardCall(scopes);
Object scope = oh.coerce(unwrap(scopes));
try {
if (scope == null) return null;
if (method == null) {
return field.get(scope);
} else {
return method.invoke(scope, arguments);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new MustacheException("Error accessing " + getTargetDescription() + " on " + elementToString(scope)
+ ", scope: [" + elementsToString(scopes, scopeIndex) + "]" + ", guards: " + Arrays.toString(guards), e);
} catch (InvocationTargetException e) {
throw new MustacheException("Error invoking " + getTargetDescription() + " on " + elementToString(scope), e.getTargetException());
} catch (Exception e) {
throw new MustacheException("Error invoking " + getTargetDescription() + " on " + elementToString(scope), e);
}
}
public Method getMethod() {
return method;
}
public Field getField() {
return field;
}
public Object[] getArguments() {
return arguments;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (field == null) {
sb.append(method.toString());
if (arguments != null) {
for (Object arg : arguments) {
sb.append(",").append(arg);
}
}
} else {
sb.append(field);
}
return sb.toString();
}
public Wrapper[] getWrappers() {
return wrappers;
}
private String getTargetDescription() {
return method == null
? "field " + field.getDeclaringClass() + "." + field.getName()
: "method " + method.getDeclaringClass().getCanonicalName() + "." + method.getName() + "(" + elementsToString(Arrays.asList(arguments), method.getParameterTypes().length - 1) + ")";
}
private String elementsToString(List<Object> objects, int showUpTo) {
if (objects == null || objects.size() == 0 || showUpTo < 0) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= showUpTo && i < objects.size(); i++) {
if (sb.length() > 0)
sb.append(",");
sb.append(elementToString(objects.get(i)));
}
return sb.toString();
}
private String elementToString(Object object) {
return object == null ? null : object.getClass().getCanonicalName() + '@' + object.hashCode();
}
}