Author: henning
Date: Wed Sep 6 07:32:36 2006
New Revision: 440724
URL: http://svn.apache.org/viewvc?view=rev&rev=440724
Log:
Uh, that whole thing was screaming for some clean up. Make sure that
null / "" were caught correctly, don't blindly modify some chars in
StringBuffers when there is an empty property name passed in.
Also some style fixes, unnecessary object creations and strange code
pathes reworked. Even cleaned up the error messages. ;-)
Modified:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/AbstractExecutor.java
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/GetExecutor.java
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
Modified:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/AbstractExecutor.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/AbstractExecutor.java?view=diff&rev=440724&r1=440723&r2=440724
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/AbstractExecutor.java
(original)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/AbstractExecutor.java
Wed Sep 6 07:32:36 2006
@@ -38,7 +38,7 @@
/**
* Method to be executed.
*/
- protected Method method = null;
+ private Method method = null;
/**
* Execute method against context.
@@ -58,5 +58,10 @@
public Method getMethod()
{
return method;
+ }
+
+ protected void setMethod(final Method method)
+ {
+ this.method = method;
}
}
Modified:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java?view=diff&rev=440724&r1=440723&r2=440724
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
(original)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/BooleanPropertyExecutor.java
Wed Sep 6 07:32:36 2006
@@ -35,55 +35,58 @@
*/
public class BooleanPropertyExecutor extends PropertyExecutor
{
- public BooleanPropertyExecutor(Log log, Introspector is, Class clazz,
String property)
+ public BooleanPropertyExecutor(final Log log, final Introspector
introspector,
+ final Class clazz, final String property)
{
- super(log, is, clazz, property);
+ super(log, introspector, clazz, property);
}
/**
* @deprecated RuntimeLogger is deprecated. Use the other constructor.
*/
- public BooleanPropertyExecutor(RuntimeLogger rlog, Introspector is, Class
clazz, String property)
+ public BooleanPropertyExecutor(final RuntimeLogger rlog, final
Introspector introspector,
+ final Class clazz, final String property)
{
- super(new RuntimeLoggerLog(rlog), is, clazz, property);
+ super(new RuntimeLoggerLog(rlog), introspector, clazz, property);
}
- protected void discover(Class clazz, String property)
+ protected void discover(final Class clazz, final String property)
{
try
{
- char c;
- StringBuffer sb;
+ Object [] params = {};
- Object[] params = { };
-
- /*
- * now look for a boolean isFoo
- */
-
- sb = new StringBuffer("is");
+ StringBuffer sb = new StringBuffer("is");
sb.append(property);
- c = sb.charAt(2);
-
- if (Character.isLowerCase(c))
- {
- sb.setCharAt(2, Character.toUpperCase(c));
- }
-
- methodUsed = sb.toString();
- method = introspector.getMethod(clazz, methodUsed, params);
+ setMethod(getIntrospector().getMethod(clazz, sb.toString(),
params));
- if (method != null)
+ if (!isAlive())
{
/*
- * now, this has to return a boolean
+ * now the convenience, flip the 1st character
*/
- if (method.getReturnType() == Boolean.TYPE)
- return;
+ char c = sb.charAt(2);
+
+ if (Character.isLowerCase(c))
+ {
+ sb.setCharAt(2, Character.toUpperCase(c));
+ }
+ else
+ {
+ sb.setCharAt(2, Character.toLowerCase(c));
+ }
- method = null;
+ setMethod(getIntrospector().getMethod(clazz, sb.toString(),
params));
+ }
+
+ if (isAlive())
+ {
+ if (getMethod().getReturnType() != Boolean.TYPE)
+ {
+ setMethod(null); // That case is rare but not unknown
+ }
}
}
/**
@@ -95,7 +98,7 @@
}
catch(Exception e)
{
- log.error("PROGRAMMER ERROR : BooleanPropertyExector()", e);
+ log.error("While looking for boolean property getter for '" +
property + "':", e);
}
}
}
Modified:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/GetExecutor.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/GetExecutor.java?view=diff&rev=440724&r1=440723&r2=440724
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/GetExecutor.java
(original)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/GetExecutor.java
Wed Sep 6 07:32:36 2006
@@ -37,80 +37,65 @@
*/
public class GetExecutor extends AbstractExecutor
{
- /**
- * Container to hold the 'key' part of
- * get(key).
- */
- private Object[] args = new Object[1];
+ private Introspector introspector = null;
+
+ private Object [] params = {};
- public GetExecutor(Log log, Introspector ispect, Class c, String key)
- throws Exception
+ public GetExecutor(final Log log, final Introspector introspector,
+ final Class clazz, final String property)
{
this.log = log;
- args[0] = key;
- method = ispect.getMethod(c, "get", args);
+ this.introspector = introspector;
+
+ // If you passed in null as property, we don't use the value
+ // for parameter lookup. Instead we just look for get() without
+ // any parameters.
+ //
+ // In any other case, the following condition will set up an array
+ // for looking up get(String) on the class.
+
+ if (property != null)
+ {
+ this.params = new Object[1];
+ this.params[0] = property;
+ }
+ discover(clazz);
}
-
+
/**
* @deprecated RuntimeLogger is deprecated. Use the other constructor.
*/
- public GetExecutor(RuntimeLogger r, Introspector ispect, Class c, String
key)
- throws Exception
+ public GetExecutor(final RuntimeLogger rlog, final Introspector
introspector,
+ final Class clazz, final String property)
{
- this(new RuntimeLoggerLog(r), ispect, c, key);
+ this(new RuntimeLoggerLog(rlog), introspector, clazz, property);
}
- /**
- * Execute method against context.
- */
- public Object execute(Object o)
- throws IllegalAccessException, InvocationTargetException
+ protected void discover(final Class clazz)
{
- if (method == null)
- return null;
-
- return method.invoke(o, args);
- }
-
- /**
- * Execute method against context.
- */
- public Object OLDexecute(Object o, InternalContextAdapter context)
- throws IllegalAccessException, MethodInvocationException
- {
- if (method == null)
- return null;
-
- try
+ try
{
- return method.invoke(o, args);
+ setMethod(introspector.getMethod(clazz, "get", params));
}
- catch(InvocationTargetException ite)
+ /**
+ * pass through application level runtime exceptions
+ */
+ catch( RuntimeException e )
{
- /*
- * the method we invoked threw an exception.
- * package and pass it up
- */
-
- throw new MethodInvocationException(
- "Invocation of method 'get(\"" + args[0] + "\")'"
- + " in " + o.getClass()
- + " threw exception "
- + ite.getTargetException().getClass(),
- ite.getTargetException(), "get");
+ throw e;
}
- catch(IllegalArgumentException iae)
+ catch(Exception e)
{
- return null;
+ log.error("While looking for get('" + params[0] + "') method:", e);
}
}
-}
-
-
-
-
-
-
-
-
+ /**
+ * Execute method against context.
+ */
+ public Object execute(final Object o)
+ throws IllegalAccessException, InvocationTargetException
+ {
+ return isAlive() ? getMethod().invoke(o, params) : null;
+ }
+}
Modified:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java?view=diff&rev=440724&r1=440723&r2=440724
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
(original)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
Wed Sep 6 07:32:36 2006
@@ -17,6 +17,7 @@
import java.lang.reflect.InvocationTargetException;
+import org.apache.commons.lang.StringUtils;
import org.apache.velocity.runtime.RuntimeLogger;
import org.apache.velocity.runtime.log.Log;
import org.apache.velocity.runtime.log.RuntimeLoggerLog;
@@ -27,29 +28,38 @@
*/
public class PropertyExecutor extends AbstractExecutor
{
- protected Introspector introspector = null;
+ private Introspector introspector = null;
- protected String methodUsed = null;
-
- public PropertyExecutor(Log log, Introspector ispctr,
- Class clazz, String property)
+ public PropertyExecutor(final Log log, final Introspector introspector,
+ final Class clazz, final String property)
{
this.log = log;
- introspector = ispctr;
+ this.introspector = introspector;
- discover(clazz, property);
+ // Don't allow passing in the empty string or null because
+ // it will either fail with a StringIndexOutOfBounds error
+ // or the introspector will get confused.
+ if (StringUtils.isNotEmpty(property))
+ {
+ discover(clazz, property);
+ }
}
/**
* @deprecated RuntimeLogger is deprecated. Use the other constructor.
*/
- public PropertyExecutor(RuntimeLogger r, Introspector ispctr,
- Class clazz, String property)
+ public PropertyExecutor(final RuntimeLogger r, final Introspector
introspector,
+ final Class clazz, final String property)
{
- this(new RuntimeLoggerLog(r), ispctr, clazz, property);
+ this(new RuntimeLoggerLog(r), introspector, clazz, property);
}
- protected void discover(Class clazz, String property)
+ protected Introspector getIntrospector()
+ {
+ return this.introspector;
+ }
+
+ protected void discover(final Class clazz, final String property)
{
/*
* this is gross and linear, but it keeps it straightforward.
@@ -57,54 +67,32 @@
try
{
- char c;
- StringBuffer sb;
-
- Object[] params = { };
+ Object [] params = {};
- /*
- * start with get<property>
- * this leaves the property name
- * as is...
- */
- sb = new StringBuffer("get");
+ StringBuffer sb = new StringBuffer("get");
sb.append(property);
- methodUsed = sb.toString();
+ setMethod(introspector.getMethod(clazz, sb.toString(), params));
- method = introspector.getMethod(clazz, methodUsed, params);
-
- if (method != null)
+ if (!isAlive())
{
- return;
- }
-
- /*
- * now the convenience, flip the 1st character
- */
-
- sb = new StringBuffer("get");
- sb.append(property);
-
- c = sb.charAt(3);
+ /*
+ * now the convenience, flip the 1st character
+ */
+
+ char c = sb.charAt(3);
+
+ if (Character.isLowerCase(c))
+ {
+ sb.setCharAt(3, Character.toUpperCase(c));
+ }
+ else
+ {
+ sb.setCharAt(3, Character.toLowerCase(c));
+ }
- if (Character.isLowerCase(c))
- {
- sb.setCharAt(3, Character.toUpperCase(c));
- }
- else
- {
- sb.setCharAt(3, Character.toLowerCase(c));
- }
-
- methodUsed = sb.toString();
- method = introspector.getMethod(clazz, methodUsed, params);
-
- if (method != null)
- {
- return;
+ setMethod(introspector.getMethod(clazz, sb.toString(),
params));
}
-
}
/**
* pass through application level runtime exceptions
@@ -115,22 +103,16 @@
}
catch(Exception e)
{
- log.error("PROGRAMMER ERROR : PropertyExector()", e);
+ log.error("While looking for property getter for '" + property +
"':", e);
}
}
-
/**
* Execute method against context.
*/
public Object execute(Object o)
throws IllegalAccessException, InvocationTargetException
{
- if (method == null)
- {
- return null;
- }
-
- return method.invoke(o, ((Object []) null));
+ return isAlive() ? getMethod().invoke(o, ((Object []) null)) : null;
}
}
Modified:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java?view=diff&rev=440724&r1=440723&r2=440724
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
(original)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java
Wed Sep 6 07:32:36 2006
@@ -154,18 +154,17 @@
throws Exception
{
if (obj == null)
+ {
return null;
+ }
- AbstractExecutor executor;
-
Class claz = obj.getClass();
/*
* first try for a getFoo() type of property
* (also getfoo() )
*/
-
- executor = new PropertyExecutor(log,introspector, claz, identifier);
+ AbstractExecutor executor = new PropertyExecutor(log,introspector,
claz, identifier);
/*
* if that didn't work, look for get("foo")
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]