Skip to content

Commit c4c5a6f

Browse files
committed
share find code between Map/Simple object handlers
1 parent 7b5b600 commit c4c5a6f

File tree

3 files changed

+42
-55
lines changed

3 files changed

+42
-55
lines changed

compiler/src/main/java/com/github/mustachejava/reflect/AbstractObjectHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
abstract class AbstractObjectHandler implements ObjectHandler {
1414

15+
protected static final Object NOT_FOUND = new Object();
16+
1517
@Override
1618
public Object coerce(Object object) {
1719
if (object instanceof Optional) {

compiler/src/main/java/com/github/mustachejava/reflect/MapObjectHandler.java

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,22 @@
22

33
import com.github.mustachejava.Binding;
44
import com.github.mustachejava.Code;
5-
import com.github.mustachejava.ObjectHandler;
65
import com.github.mustachejava.TemplateContext;
76
import com.github.mustachejava.util.Wrapper;
8-
import java.util.List;
7+
98
import java.util.Map;
109

11-
public class MapObjectHandler extends AbstractObjectHandler {
10+
public class MapObjectHandler extends SimpleObjectHandler {
1211

1312
@Override
14-
public Wrapper find(String name, List<Object> scopes) {
15-
return scopes1 -> {
16-
for (int i = scopes1.size() - 1; i >= 0; i--) {
17-
Object scope = scopes1.get(i);
18-
if (scope != null) {
19-
int index = name.indexOf(".");
20-
if (index == -1) {
21-
// Special case Maps
22-
if (scope instanceof Map) {
23-
Map map = (Map) scope;
24-
if (map.containsKey(name)) {
25-
return map.get(name);
26-
}
27-
}
28-
} else {
29-
// Dig into the dot-notation through recursion
30-
List<Object> subscope = ObjectHandler.makeList(scope);
31-
Wrapper wrapper = find(name.substring(0, index), subscope);
32-
if (wrapper != null) {
33-
scope = wrapper.call(subscope);
34-
if (scope == null) {
35-
continue;
36-
}
37-
subscope = ObjectHandler.makeList(scope);
38-
return find(name.substring(index + 1), ObjectHandler.makeList(subscope)).call(subscope);
39-
}
40-
}
41-
}
13+
public Object get(String name, Object scope) {
14+
if (scope instanceof Map) {
15+
Map map = (Map) scope;
16+
if (map.containsKey(name)) {
17+
return map.get(name);
4218
}
43-
return null;
44-
};
19+
}
20+
return NOT_FOUND;
4521
}
4622

4723
@Override

compiler/src/main/java/com/github/mustachejava/reflect/SimpleObjectHandler.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,43 @@ public Object get(List<Object> scopes) {
2626
};
2727
}
2828

29+
public Object get(String name, Object scope) {
30+
// Special case Maps
31+
if (scope instanceof Map) {
32+
Map map = (Map) scope;
33+
if (map.containsKey(name)) {
34+
return map.get(name);
35+
} else if (!areMethodsAccessible(map)) {
36+
return NOT_FOUND;
37+
}
38+
}
39+
// Check to see if there is a method or field that matches
40+
try {
41+
AccessibleObject ao = lookup(scope.getClass(), name);
42+
if (ao instanceof Method) {
43+
return ((Method) ao).invoke(scope);
44+
} else if (ao instanceof Field) {
45+
return ((Field) ao).get(scope);
46+
}
47+
} catch (InvocationTargetException ie) {
48+
throw new MustacheException("Failed to get " + name + " from " + scope.getClass(), ie);
49+
} catch (IllegalAccessException iae) {
50+
throw new MustacheException("Set accessible failed to get " + name + " from " + scope.getClass(), iae);
51+
}
52+
return NOT_FOUND;
53+
}
54+
2955
@Override
30-
public Wrapper find(final String name, final List<Object> scopes) {
56+
public Wrapper find(String name, List<Object> scopes) {
3157
return scopes1 -> {
3258
for (int i = scopes1.size() - 1; i >= 0; i--) {
3359
Object scope = scopes1.get(i);
3460
if (scope != null) {
3561
int index = name.indexOf(".");
3662
if (index == -1) {
37-
// Special case Maps
38-
if (scope instanceof Map) {
39-
Map map = (Map) scope;
40-
if (map.containsKey(name)) {
41-
return map.get(name);
42-
} else if (!areMethodsAccessible(map)) {
43-
continue; //don't check methods, move to next scope
44-
}
45-
}
46-
// Check to see if there is a method or field that matches
47-
try {
48-
AccessibleObject ao = lookup(scope.getClass(), name);
49-
if (ao instanceof Method) {
50-
return ((Method) ao).invoke(scope);
51-
} else if (ao instanceof Field) {
52-
return ((Field) ao).get(scope);
53-
}
54-
} catch (InvocationTargetException ie) {
55-
throw new MustacheException("Failed to get " + name + " from " + scope.getClass(), ie);
56-
} catch (IllegalAccessException iae) {
57-
throw new MustacheException("Set accessible failed to get " + name + " from " + scope.getClass(), iae);
63+
Object result = get(name, scope);
64+
if (result != NOT_FOUND) {
65+
return result;
5866
}
5967
} else {
6068
// Dig into the dot-notation through recursion
@@ -109,6 +117,7 @@ public boolean equals(Object obj) {
109117

110118
// Used to cache misses
111119
private static AccessibleObject NONE;
120+
112121
static {
113122
try {
114123
NONE = SimpleObjectHandler.class.getDeclaredField("NONE");

0 commit comments

Comments
 (0)