org.zodiac.template.velocity.impl.CustomizedUberspectImpl Maven / Gradle / Ivy
package org.zodiac.template.velocity.impl;
import org.apache.velocity.runtime.parser.node.AbstractExecutor;
import org.apache.velocity.runtime.parser.node.BooleanPropertyExecutor;
import org.apache.velocity.runtime.parser.node.GetExecutor;
import org.apache.velocity.runtime.parser.node.MapGetExecutor;
import org.apache.velocity.runtime.parser.node.PropertyExecutor;
import org.apache.velocity.util.introspection.Info;
import org.apache.velocity.util.introspection.VelPropertyGet;
/**
*
* 修改velocity默认的uberspect,改变默认的get property方法的顺序:
*
* getFoo()
或getfoo()
。
* isFoo()
或isfoo()
。
* Map.get(String)
。
* AnyType.get(String)
。
*
*
*
*/
public class CustomizedUberspectImpl extends org.apache.velocity.util.introspection.UberspectImpl {
public CustomizedUberspectImpl() {
super();
}
@Override
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) {
if (obj == null) {
return null;
}
Class> claz = obj.getClass();
/*
* First try for a getFoo() type of property (also getfoo() ) .
*/
AbstractExecutor executor = new PropertyExecutor(log, introspector, claz, identifier);
/*
* If that didn't work, look for boolean isFoo() .
*/
if (!executor.isAlive()) {
executor = new BooleanPropertyExecutor(log, introspector, claz, identifier);
}
/*
* Let's see if we are a map.
*/
if (!executor.isAlive()) {
executor = new MapGetExecutor(log, claz, identifier);
}
/*
* Finally, look for get("foo") .
*/
if (!executor.isAlive()) {
executor = new GetExecutor(log, introspector, claz, identifier);
}
return executor.isAlive() ? new VelGetterImpl(executor) : null;
}
}