You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ve...@jakarta.apache.org on 2004/11/06 13:38:51 UTC
[Jakarta-Velocity Wiki] Updated: PublicFieldUberspect
Date: 2004-11-06T04:38:51
Editor: ShinobuKawai <sh...@ieee.org>
Wiki: Jakarta-Velocity Wiki
Page: PublicFieldUberspect
URL: http://wiki.apache.org/jakarta-velocity/PublicFieldUberspect
Added array #clone() #length support.
Change Log:
------------------------------------------------------------------------------
@@ -21,6 +21,7 @@
import org.apache.velocity.util.introspection.Info;
import org.apache.velocity.util.introspection.UberspectImpl;
+import org.apache.velocity.util.introspection.VelMethod;
import org.apache.velocity.util.introspection.VelPropertyGet;
import org.apache.velocity.util.introspection.VelPropertySet;
@@ -49,6 +50,22 @@
}
/**
+ * Method
+ */
+ public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i)
+ throws Exception
+ {
+ Class clazz = obj.getClass();
+ boolean isArray = clazz.isArray();
+ boolean isClone = methodName.equals("clone") && (args.length == 0);
+ if (isArray && isClone) {
+ return new ArrayCloneMethod(clazz);
+ }
+
+ return super.getMethod(obj, methodName, args, i);
+ }
+
+ /**
* Property getter - returns VelPropertyGet appropos for #set($foo = $bar.woogie).
* <br />
* First tries the regular routine. If a getter was not found,
@@ -63,6 +80,13 @@
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i)
throws Exception
{
+ Class clazz = obj.getClass();
+ boolean isArray = clazz.isArray();
+ boolean isLength = identifier.equals("length");
+ if (isArray && isLength) {
+ return new ArrayLengthGetter();
+ }
+
VelPropertyGet getter = super.getPropertyGet(obj, identifier, i);
// there is no clean way to see if super succeeded
// @see http://issues.apache.org/bugzilla/show_bug.cgi?id=31742
@@ -169,6 +193,52 @@
}
}
+ protected class ArrayLengthGetter implements VelPropertyGet
+ {
+ public ArrayLengthGetter()
+ {
+ }
+
+ public Object invoke(Object o) throws Exception
+ {
+ if (o instanceof boolean[]) {
+ return new Integer(((boolean[]) o).length);
+ }
+ if (o instanceof byte[]) {
+ return new Integer(((byte[]) o).length);
+ }
+ if (o instanceof short[]) {
+ return new Integer(((short[]) o).length);
+ }
+ if (o instanceof int[]) {
+ return new Integer(((int[]) o).length);
+ }
+ if (o instanceof long[]) {
+ return new Integer(((long[]) o).length);
+ }
+ if (o instanceof float[]) {
+ return new Integer(((float[]) o).length);
+ }
+ if (o instanceof double[]) {
+ return new Integer(((double[]) o).length);
+ }
+ if (o instanceof Object[]) {
+ return new Integer(((Object[]) o).length);
+ }
+ return null;
+ }
+
+ public boolean isCacheable()
+ {
+ return true;
+ }
+
+ public String getMethodName()
+ {
+ return "array length getter";
+ }
+ }
+
/**
* Implemetetion of {@link VelPropertySet} that sets to public fields.
*
@@ -222,6 +292,61 @@
public String getMethodName()
{
return "public field setter";
+ }
+ }
+
+ public class ArrayCloneMethod implements VelMethod
+ {
+ private Class clazz = null;
+
+ public ArrayCloneMethod(Class clazz)
+ {
+ this.clazz = clazz;
+ }
+
+ public Object invoke(Object o, Object[] params)
+ throws Exception
+ {
+ if (o instanceof boolean[]) {
+ return ((boolean[]) o).clone();
+ }
+ if (o instanceof byte[]) {
+ return ((byte[]) o).clone();
+ }
+ if (o instanceof short[]) {
+ return ((short[]) o).clone();
+ }
+ if (o instanceof int[]) {
+ return ((int[]) o).clone();
+ }
+ if (o instanceof long[]) {
+ return ((long[]) o).clone();
+ }
+ if (o instanceof float[]) {
+ return ((float[]) o).clone();
+ }
+ if (o instanceof double[]) {
+ return ((double[]) o).clone();
+ }
+ if (o instanceof Object[]) {
+ return ((Object[]) o).clone();
+ }
+ return null;
+ }
+
+ public boolean isCacheable()
+ {
+ return true;
+ }
+
+ public String getMethodName()
+ {
+ return "clone";
+ }
+
+ public Class getReturnType()
+ {
+ return this.clazz;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org