Skip to content

Commit

Permalink
share find code between Map/Simple object handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
spullara committed Jun 5, 2023
1 parent 7b5b600 commit c4c5a6f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

abstract class AbstractObjectHandler implements ObjectHandler {

protected static final Object NOT_FOUND = new Object();

@Override
public Object coerce(Object object) {
if (object instanceof Optional) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,22 @@

import com.github.mustachejava.Binding;
import com.github.mustachejava.Code;
import com.github.mustachejava.ObjectHandler;
import com.github.mustachejava.TemplateContext;
import com.github.mustachejava.util.Wrapper;
import java.util.List;

import java.util.Map;

public class MapObjectHandler extends AbstractObjectHandler {
public class MapObjectHandler extends SimpleObjectHandler {

@Override
public Wrapper find(String name, List<Object> scopes) {
return scopes1 -> {
for (int i = scopes1.size() - 1; i >= 0; i--) {
Object scope = scopes1.get(i);
if (scope != null) {
int index = name.indexOf(".");
if (index == -1) {
// Special case Maps
if (scope instanceof Map) {
Map map = (Map) scope;
if (map.containsKey(name)) {
return map.get(name);
}
}
} else {
// Dig into the dot-notation through recursion
List<Object> subscope = ObjectHandler.makeList(scope);
Wrapper wrapper = find(name.substring(0, index), subscope);
if (wrapper != null) {
scope = wrapper.call(subscope);
if (scope == null) {
continue;
}
subscope = ObjectHandler.makeList(scope);
return find(name.substring(index + 1), ObjectHandler.makeList(subscope)).call(subscope);
}
}
}
public Object get(String name, Object scope) {
if (scope instanceof Map) {
Map map = (Map) scope;
if (map.containsKey(name)) {
return map.get(name);
}
return null;
};
}
return NOT_FOUND;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,43 @@ public Object get(List<Object> scopes) {
};
}

public Object get(String name, Object scope) {
// Special case Maps
if (scope instanceof Map) {
Map map = (Map) scope;
if (map.containsKey(name)) {
return map.get(name);
} else if (!areMethodsAccessible(map)) {
return NOT_FOUND;
}
}
// Check to see if there is a method or field that matches
try {
AccessibleObject ao = lookup(scope.getClass(), name);
if (ao instanceof Method) {
return ((Method) ao).invoke(scope);
} else if (ao instanceof Field) {
return ((Field) ao).get(scope);
}
} catch (InvocationTargetException ie) {
throw new MustacheException("Failed to get " + name + " from " + scope.getClass(), ie);
} catch (IllegalAccessException iae) {
throw new MustacheException("Set accessible failed to get " + name + " from " + scope.getClass(), iae);
}
return NOT_FOUND;
}

@Override
public Wrapper find(final String name, final List<Object> scopes) {
public Wrapper find(String name, List<Object> scopes) {
return scopes1 -> {
for (int i = scopes1.size() - 1; i >= 0; i--) {
Object scope = scopes1.get(i);
if (scope != null) {
int index = name.indexOf(".");
if (index == -1) {
// Special case Maps
if (scope instanceof Map) {
Map map = (Map) scope;
if (map.containsKey(name)) {
return map.get(name);
} else if (!areMethodsAccessible(map)) {
continue; //don't check methods, move to next scope
}
}
// Check to see if there is a method or field that matches
try {
AccessibleObject ao = lookup(scope.getClass(), name);
if (ao instanceof Method) {
return ((Method) ao).invoke(scope);
} else if (ao instanceof Field) {
return ((Field) ao).get(scope);
}
} catch (InvocationTargetException ie) {
throw new MustacheException("Failed to get " + name + " from " + scope.getClass(), ie);
} catch (IllegalAccessException iae) {
throw new MustacheException("Set accessible failed to get " + name + " from " + scope.getClass(), iae);
Object result = get(name, scope);
if (result != NOT_FOUND) {
return result;
}
} else {
// Dig into the dot-notation through recursion
Expand Down Expand Up @@ -109,6 +117,7 @@ public boolean equals(Object obj) {

// Used to cache misses
private static AccessibleObject NONE;

static {
try {
NONE = SimpleObjectHandler.class.getDeclaredField("NONE");
Expand Down

0 comments on commit c4c5a6f

Please sign in to comment.