You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/08/21 21:17:53 UTC
svn commit: r568238 - in /velocity/tools/branches/2.x/src:
main/java/org/apache/velocity/tools/ClassUtils.java
main/java/org/apache/velocity/tools/config/Data.java
test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java
Author: nbubna
Date: Tue Aug 21 12:17:52 2007
New Revision: 568238
URL: http://svn.apache.org/viewvc?rev=568238&view=rev
Log:
support using public static field values as Data or Property values
Modified:
velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java
velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java
Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java?rev=568238&r1=568237&r2=568238&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/ClassUtils.java Tue Aug 21 12:17:52 2007
@@ -21,7 +21,9 @@
import java.io.InputStream;
import java.io.IOException;
+import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@@ -291,6 +293,30 @@
}
// otherwise, return null
return null;
+ }
+
+ public static Object getFieldValue(String fieldPath)
+ throws ClassNotFoundException, NoSuchFieldException,
+ SecurityException, IllegalAccessException
+ {
+ int lastDot = fieldPath.lastIndexOf('.');
+ String classname = fieldPath.substring(0, lastDot);
+ String fieldname = fieldPath.substring(lastDot + 1, fieldPath.length());
+
+ Class clazz = getClass(classname);
+ return getFieldValue(clazz, fieldname);
+ }
+
+ public static Object getFieldValue(Class clazz, String fieldname)
+ throws NoSuchFieldException, SecurityException, IllegalAccessException
+ {
+ Field field = clazz.getField(fieldname);
+ int mod = field.getModifiers();
+ if (!Modifier.isStatic(mod))
+ {
+ throw new UnsupportedOperationException("Field "+fieldname+" in class "+clazz.getName()+" is not static. Only static fields are supported.");
+ }
+ return field.get(null);
}
}
Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java?rev=568238&r1=568237&r2=568238&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java Tue Aug 21 12:17:52 2007
@@ -243,6 +243,11 @@
dc.setTarget(String.class);
dc.setConverter(new StringConverter());
}
+ else if (type.equals("field"))
+ {
+ dc.setTarget(Object.class);
+ dc.setConverter(new FieldConverter());
+ }
else
{
return null;
@@ -349,6 +354,22 @@
}
return convertedList;
}
+ }
+ }
+ }
+
+ protected static class FieldConverter implements Converter
+ {
+ public Object convert(Class type, Object value)
+ {
+ String fieldpath = (String)value;
+ try
+ {
+ return ClassUtils.getFieldValue(fieldpath);
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("Could not retrieve value for field at "+fieldpath, e);
}
}
}
Modified: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java?rev=568238&r1=568237&r2=568238&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java (original)
+++ velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java Tue Aug 21 12:17:52 2007
@@ -216,6 +216,16 @@
assertValid(datum);
assertEquals(datum.getConvertedValue(), Boolean.TRUE);
+ // check valid field type
+ datum.setValue("java.lang.Boolean.TRUE");
+ datum.setType("field");
+ assertValid(datum);
+ assertSame(datum.getConvertedValue(), Boolean.TRUE);
+
+ // check invalid field value
+ datum.setValue("blahblah");
+ assertInvalid(datum);
+
// check number type
datum.setValue("3.16");
datum.setType("number");
@@ -249,12 +259,17 @@
three.set(2, new Integer(3));
assertEquals(datum.getConvertedValue(), three);
- // and finally, a list of booleans
+ // and a list of booleans
datum.setType("list.boolean");
datum.setValue("true,false");
List two = new ArrayList(2);
two.add(Boolean.TRUE);
two.add(Boolean.FALSE);
+ assertEquals(datum.getConvertedValue(), two);
+
+ // and a list of fields
+ datum.setType("list.field");
+ datum.setValue("java.lang.Boolean.TRUE,java.lang.Boolean.FALSE");
assertEquals(datum.getConvertedValue(), two);
//TODO: test converter/target class stuff