public VelPropertyGet getPropertyGet( Object object, String name, Info i ) throws Exception { VelPropertyGet getter = super.getPropertyGet( object, name, i ); try { getter.getMethodName(); return getter; } catch( NullPointerException notfound ) { final Field field = object.getClass().getField( name ); if( field != null ) { AbstractExecutor executor = new AbstractExecutor() { public Object execute( Object o ) throws IllegalAccessException { return field.get( o ); } }; return new VelGetterImpl( executor ); } } return null; }
public static Iterator<?> getStandardIterator(Object obj, Info i) { if (obj.getClass().isArray()) { return new ArrayIterator(obj); } else if (obj instanceof Collection) { return ((Collection<?>) obj).iterator(); } else if (obj instanceof Map) { return ((Map<?, ?>) obj).values().iterator(); } else if (obj instanceof Iterator) { return ((Iterator<?>) obj); } else if (obj instanceof Enumeration) { return new EnumerationIterator((Enumeration<?>) obj); } throw new VelocityParsingError("Could not determine type of iterator in " + "#foreach loop ", i); }
@Override public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception { AbstractExecutor executor; if (obj == null) { throw new VelocityParsingError("tried " + getPropertyText("null", identifier), i); } Class<?> type = obj.getClass(); // trying getFoo() executor = new PropertyExecutor(log, introspectorWithLog, type, identifier); if (!executor.isAlive()) { // trying get("foo") executor = new GetExecutor(log, introspectorWithLog, type, identifier); } if (!executor.isAlive()) { // trying isFoo() executor = new BooleanPropertyExecutor(log, introspectorWithLog, type, identifier); } if (!executor.isAlive()) { throw new VelocityParsingError("Did not find " + getPropertyText(obj.getClass().getName(), identifier), i); } return new VelGetterImpl(executor); }
@Override public void init(RuntimeServices rs, InternalContextAdapter context, Node node) { super.init(rs, context, node); final int n = node.jjtGetNumChildren() - 1; for (int i = 1; i < n; i++) { Node child = node.jjtGetChild(i); if (i == 1) { if (child.getType() == ParserTreeConstants.JJTREFERENCE) { this.var = ((ASTReference) child).getRootString(); } else { throw new TemplateInitException("Syntax error", getTemplateName(), getLine(), getColumn()); } } else if (child.getType() == ParserTreeConstants.JJTSTRINGLITERAL) { String value = (String) ((ASTStringLiteral) child).value(context); if (i == 2) { this.column = value; } } else { throw new TemplateInitException("Syntax error", getTemplateName(), getLine(), getColumn()); } } this.uberInfo = new Info(this.getTemplateName(), getLine(), getColumn()); }
/***********************************************************************/ public static Iterator getStandardIterator(Object obj, Info i) { if (obj.getClass().isArray()) { return new ArrayIterator(obj); } else if (obj instanceof Collection) { return ((Collection) obj).iterator(); } else if (obj instanceof Map) { return ((Map) obj).values().iterator(); } else if (obj instanceof Iterator) { return ((Iterator) obj); } else if (obj instanceof Enumeration) { return new EnumerationIterator((Enumeration) obj); } throw new VelocityParsingError("Could not determine type of iterator in " + "#foreach loop ", i); }
/***********************************************************************/ public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception { if (obj == null) { if(beKindToNulls) { return null; } else { throw new VelocityParsingError("tried " + getMethodText("null", methodName, args), i); } } Method m = introspector.getMethod(obj.getClass(), methodName, args); if (m == null) { throw new VelocityParsingError("Method " + getMethodText(obj.getClass().getName(), methodName, args) + " does not exist.", i); } return new VelMethodImpl(m); }
/***********************************************************************/ public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception { AbstractExecutor executor; if (obj == null) { throw new VelocityParsingError("tried " + getPropertyText("null", identifier), i); } Class<? extends Object> claz = obj.getClass(); // trying getFoo() executor = new PropertyExecutor(log, introspectorWithLog, claz, identifier); if (!executor.isAlive()) { // trying get("foo") executor = new GetExecutor(log, introspectorWithLog, claz, identifier); } if (!executor.isAlive()) { // trying isFoo() executor = new BooleanPropertyExecutor(log, introspectorWithLog, claz, identifier); } if (!executor.isAlive()) { throw new VelocityParsingError("Did not find " + getPropertyText(obj.getClass().getName(), identifier), i); } return new VelGetterImpl(executor); }
@Override public boolean invalidSetMethod(Context context, String leftreference, String rightreference, Info info) { logger.error("Invalid write: {left:{}, right:{}, template:{}}", new Object[] { leftreference, rightreference, info }); return false; }
@Override public Object invalidMethod(Context context, String reference, Object object, String method, Info info) { logger.error( "Invalid method: {ref:{}, obj:{}, method:{}, template:{}}", new Object[] { reference, object, method, info }); return null; }
@Override public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception { if (obj == null) { throw new VelocityParsingError("tried " + getMethodText("null", methodName, args), i); } Method m = introspector.getMethod(obj.getClass(), methodName, args); if (m == null) { throw new VelocityParsingError("Method " + getMethodText(obj.getClass().getName(), methodName, args) + " does not exist.", i); } return new VelMethodImpl(m); }
@Override public void init(RuntimeServices rs, InternalContextAdapter context, Node node) { super.init(rs, context, node); final int n = node.jjtGetNumChildren() - 1; for (int i = 1; i < n; i++) { Node child = node.jjtGetChild(i); if (i == 1) { if (child.getType() == ParserTreeConstants.JJTREFERENCE) { this.var = ((ASTReference) child).getRootString(); } else { throw new TemplateInitException("Syntax error", getTemplateName(), getLine(), getColumn()); } } else if (child.getType() == ParserTreeConstants.JJTSTRINGLITERAL) { String value = (String) ((ASTStringLiteral)child).value(context); switch (i) { case 2: this.separator = value; break; case 3: this.open = value; break; case 4: this.close = value; break; default: break; } } else { throw new TemplateInitException("Syntax error", getTemplateName(), getLine(), getColumn()); } } this.uberInfo = new Info(this.getTemplateName(), getLine(), getColumn()); }
/***********************************************************************/ private void assertInfoEqual(Info i, String name, int line, int column) { assertEquals("Template Name", name, i.getTemplateName()); assertEquals("Template Line", line, i.getLine()); assertEquals("Template Column", column, i.getColumn()); }
/***********************************************************************/ public Info getInfoFor(String velocity) { try { VelocityParser.parseString(velocity, this); fail("Testable Uberspect Should have thrown an error"); throw new Error("Shouldn't be able to reach this point"); } catch (VelocityParsingError t) { return t.getInfo(); } }
@Override public Object invalidGetMethod(Context context, String reference, Object object, String property, Info info) { return null; }
@Override public Iterator<?> getIterator(Object obj, Info i) throws Exception { return getStandardIterator(obj, i); }
public VelocityParsingError(String message, Info info) { this.message = message; this.info = info; }
public static String getInfoText(Info i) { return " at [" + i.getLine() + "," + i.getColumn() + "]" + " in template " + i.getTemplateName(); }
/***********************************************************************/ public Iterator getIterator(Object obj, Info i) throws Exception { return getStandardIterator(obj, i); }
/***********************************************************************/ public static String getInfoText(Info i) { return " at [" + i.getLine() + "," + i.getColumn() + "]" + " in template " + i.getTemplateName(); }
public Info getInfo() { return info; }
/***********************************************************************/ public void testInfoForField() throws Exception { Info i = getInfoFor("$main.unknownField"); assertInfoEqual(i, "$main.unknownField", 1, 7); }
/***********************************************************************/ public void testInfoForMethod() { Info i = getInfoFor("$main.unknownMethod()"); assertInfoEqual(i, "$main.unknownMethod()", 1, 7); }