package org.stringtemplate.v4.misc;
import org.stringtemplate.v4.Interpreter;
import org.stringtemplate.v4.ModelAdaptor;
import org.stringtemplate.v4.ST;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class ObjectModelAdaptor implements ModelAdaptor {
protected static final Member INVALID_MEMBER;
static {
Member invalidMember;
try {
invalidMember = ObjectModelAdaptor.class.getDeclaredField("INVALID_MEMBER");
} catch (NoSuchFieldException ex) {
invalidMember = null;
} catch (SecurityException ex) {
invalidMember = null;
}
INVALID_MEMBER = invalidMember;
}
protected static final Map<Class<?>, Map<String, Member>> membersCache =
new HashMap<Class<?>, Map<String, Member>>();
@Override
public synchronized Object getProperty(Interpreter interp, ST self, Object o, Object property, String propertyName)
throws STNoSuchPropertyException
{
if (o == null) {
throw new NullPointerException("o");
}
Class<?> c = o.getClass();
if ( property==null ) {
return throwNoSuchProperty(c, propertyName, null);
}
Member member = findMember(c, propertyName);
if ( member!=null ) {
try {
if (member instanceof Method) {
return ((Method)member).invoke(o);
}
else if (member instanceof Field) {
return ((Field)member).get(o);
}
}
catch (Exception e) {
throwNoSuchProperty(c, propertyName, e);
}
}
return throwNoSuchProperty(c, propertyName, null);
}
protected static Member findMember(Class<?> clazz, String memberName) {
if (clazz == null) {
throw new NullPointerException("clazz");
}
if (memberName == null) {
throw new NullPointerException("memberName");
}
synchronized (membersCache) {
Map<String, Member> members = membersCache.get(clazz);
Member member;
if (members != null) {
member = members.get(memberName);
if (member != null) {
return member != INVALID_MEMBER ? member : null;
}
}
else {
members = new HashMap<String, Member>();
membersCache.put(clazz, members);
}
String methodSuffix = Character.toUpperCase(memberName.charAt(0)) +
memberName.substring(1, memberName.length());
member = tryGetMethod(clazz, "get" + methodSuffix);
if (member == null) {
member = tryGetMethod(clazz, "is" + methodSuffix);
if (member == null) {
member = tryGetMethod(clazz, "has" + methodSuffix);
}
}
if (member == null) {
member = tryGetField(clazz, memberName);
}
members.put(memberName, member != null ? member : INVALID_MEMBER);
return member;
}
}
protected static Method tryGetMethod(Class<?> clazz, String methodName) {
try {
Method method = clazz.getMethod(methodName);
if (method != null) {
method.setAccessible(true);
}
return method;
} catch (NoSuchMethodException ex) {
} catch (SecurityException ex) {
}
return null;
}
protected static Field tryGetField(Class<?> clazz, String fieldName) {
try {
Field field = clazz.getField(fieldName);
if (field != null) {
field.setAccessible(true);
}
return field;
} catch (NoSuchFieldException ex) {
} catch (SecurityException ex) {
}
return null;
}
protected Object throwNoSuchProperty(Class<?> clazz, String propertyName, Exception cause) {
throw new STNoSuchPropertyException(cause, null, clazz.getName() + "." + propertyName);
}
}