You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/02/11 16:49:50 UTC

svn commit: r1069848 - in /commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3: internal/rulesbinder/CallMethodBuilderImpl.java rule/CallMethodRule.java rulesbinder/CallMethodBuilder.java

Author: simonetripodi
Date: Fri Feb 11 15:49:50 2011
New Revision: 1069848

URL: http://svn.apache.org/viewvc?rev=1069848&view=rev
Log:
associating a parameters syntax to semantic sometimes is not very useful, so added a shortcut methos to let users ware what they do, explicitly emphasized that the CallMethodRule can be invoked also using the matching element body as argument

Modified:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/CallMethodBuilderImpl.java
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/CallMethodRule.java
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/CallMethodBuilder.java

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/CallMethodBuilderImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/CallMethodBuilderImpl.java?rev=1069848&r1=1069847&r2=1069848&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/CallMethodBuilderImpl.java (original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/rulesbinder/CallMethodBuilderImpl.java Fri Feb 11 15:49:50 2011
@@ -38,7 +38,7 @@ final class CallMethodBuilderImpl
 
     private int paramCount = 0;
 
-    private Class<?>[] paramTypes;
+    private Class<?>[] paramTypes = new Class<?>[]{};
 
     private boolean useExactMatch = false;
 
@@ -54,13 +54,7 @@ final class CallMethodBuilderImpl
     }
 
     /**
-     * Sets the location of the target object.
-     *
-     * Positive numbers are relative to the top of the digester object stack.
-     * Negative numbers are relative to the bottom of the stack. Zero implies the top object on the stack.
-     *
-     * @param targetOffset location of the target object.
-     * @return this builder instance
+     * {@inheritDoc}
      */
     public CallMethodBuilderImpl withTargetOffset(int targetOffset) {
         this.targetOffset = targetOffset;
@@ -68,13 +62,7 @@ final class CallMethodBuilderImpl
     }
 
     /**
-     * Sets the Java classe names that represent the parameter types of the method arguments.
-     *
-     * If you wish to use a primitive type, specify the corresonding Java wrapper class instead,
-     * such as {@code java.lang.Boolean.TYPE} for a {@code boolean} parameter.
-     *
-     * @param The Java classe names that represent the parameter types of the method arguments
-     * @return this builder instance
+     * {@inheritDoc}
      */
     public CallMethodBuilderImpl withParamTypes(String...paramTypeNames) {
         if (paramTypeNames != null) {
@@ -94,13 +82,7 @@ final class CallMethodBuilderImpl
     }
 
     /**
-     * Sets the Java classes that represent the parameter types of the method arguments.
-     *
-     * If you wish to use a primitive type, specify the corresonding Java wrapper class instead,
-     * such as {@code java.lang.Boolean.TYPE} for a {@code boolean} parameter.
-     *
-     * @param paramTypes The Java classes that represent the parameter types of the method arguments
-     * @return this builder instance
+     * {@inheritDoc}
      */
     public CallMethodBuilderImpl withParamTypes(Class<?>...paramTypes) {
         this.paramTypes = paramTypes;
@@ -113,10 +95,7 @@ final class CallMethodBuilderImpl
     }
 
     /**
-     * Should <code>MethodUtils.invokeExactMethod</code> be used for the reflection.
-     *
-     * @param useExactMatch Flag to mark exact matching or not
-     * @return this builder instance
+     * {@inheritDoc}
      */
     public CallMethodBuilderImpl useExactMatch(boolean useExactMatch) {
         this.useExactMatch = useExactMatch;
@@ -124,11 +103,7 @@ final class CallMethodBuilderImpl
     }
 
     /**
-     * The number of parameters to collect, or zero for a single argument from the body of this element.
-     *
-     * @param paramCount The number of parameters to collect, or zero for a single argument
-     *        from the body of this element.
-     * @return this builder instance
+     * {@inheritDoc}
      */
     public CallMethodBuilderImpl withParamCount(int paramCount) {
         if (paramCount < 0) {
@@ -137,33 +112,33 @@ final class CallMethodBuilderImpl
         }
 
         this.paramCount = paramCount;
+        if (this.paramCount == 0) {
+            this.paramTypes = new Class<?>[] { String.class };
+        } else {
+            this.paramTypes = new Class<?>[this.paramCount];
+            for (int i = 0; i < paramTypes.length; i++) {
+                this.paramTypes[i] = String.class;
+            }
+        }
         return this;
     }
 
     /**
      * {@inheritDoc}
      */
+    public CallMethodBuilder usingElementBodyAsArgument() {
+        return this.withParamCount(0);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
     protected CallMethodRule createRule() {
-        Class<?>[] paramTypes = null;
-
-        if (this.paramTypes == null) {
-            if (this.paramCount == 0) {
-                paramTypes = new Class<?>[] { String.class };
-            } else {
-                paramTypes = new Class<?>[this.paramCount];
-                for (int i = 0; i < paramTypes.length; i++) {
-                    paramTypes[i] = String.class;
-                }
-            }
-        } else {
-            paramTypes = this.paramTypes;
-        }
-
         return new CallMethodRule(this.targetOffset,
                 this.methodName,
                 this.paramCount,
-                paramTypes,
+                this.paramTypes,
                 this.useExactMatch);
     }
 

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/CallMethodRule.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/CallMethodRule.java?rev=1069848&r1=1069847&r2=1069848&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/CallMethodRule.java (original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rule/CallMethodRule.java Fri Feb 11 15:49:50 2011
@@ -92,13 +92,15 @@ import org.xml.sax.Attributes;
  */
 public class CallMethodRule extends Rule {
 
-    /** 
-     * location of the target object for the call, relative to the
+    /**
+     * The location of the target object for the call, relative to the
      * top of the digester object stack. The default value of zero
      * means the target object is the one on top of the stack.
      */
     private final int targetOffset;
 
+    private final int paramCount;
+
     /**
      * The method name to call on the parent object.
      */
@@ -144,6 +146,7 @@ public class CallMethodRule extends Rule
     public CallMethodRule(int targetOffset, String methodName, int paramCount, Class<?>[] paramTypes, boolean useExactMatch) {
         this.targetOffset = targetOffset;
         this.methodName = methodName;
+        this.paramCount = paramCount;
 
         // copy the parameter class into an array
         this.paramTypes = new Class[paramTypes.length];
@@ -160,8 +163,8 @@ public class CallMethodRule extends Rule
     @Override
     public void begin(String namespace, String name, Attributes attributes) throws Exception {
         // Push an array to capture the parameter values if necessary
-        if (this.paramTypes.length > 0) {
-            Object parameters[] = new Object[this.paramTypes.length];
+        if (this.paramCount > 0) {
+            Object parameters[] = new Object[this.paramCount];
             for (int i = 0; i < parameters.length; i++) {
                 parameters[i] = null;
             }
@@ -176,7 +179,7 @@ public class CallMethodRule extends Rule
      */
     @Override
     public void body(String namespace, String name, String text) throws Exception {
-        if (this.paramTypes.length == 1) {
+        if (this.paramCount == 0) {
             this.bodyText = text.trim();
         }
     }
@@ -188,7 +191,7 @@ public class CallMethodRule extends Rule
     public void end(String namespace, String name) throws Exception {
         // Retrieve or construct the parameter values array
         Object parameters[] = null;
-        if (this.paramTypes.length > 0) {
+        if (this.paramCount > 0) {
 
             parameters = (Object[]) this.getDigester().popParams();
 
@@ -215,7 +218,7 @@ public class CallMethodRule extends Rule
             // only be overridden if data is present in the XML. I don't
             // know why this should only apply to methods taking *one*
             // parameter, but it always has been so we can't change it now.
-            if (this.paramTypes.length == 1 && parameters[0] == null) {
+            if (this.paramCount == 1 && parameters[0] == null) {
                 return;
             }
 

Modified: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/CallMethodBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/CallMethodBuilder.java?rev=1069848&r1=1069847&r2=1069848&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/CallMethodBuilder.java (original)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/rulesbinder/CallMethodBuilder.java Fri Feb 11 15:49:50 2011
@@ -36,7 +36,14 @@ public interface CallMethodBuilder exten
     CallMethodBuilder withTargetOffset(int targetOffset);
 
     /**
-     * Sets the Java classe names that represent the parameter types of the method arguments.
+     * Prepare the {@link CallMethodRule} to be invoked using the matching element body as argument.
+     *
+     * @return this builder instance
+     */
+    CallMethodBuilder usingElementBodyAsArgument();
+
+    /**
+     * Sets the Java class names that represent the parameter types of the method arguments.
      *
      * If you wish to use a primitive type, specify the corresonding Java wrapper class instead,
      * such as {@code java.lang.Boolean.TYPE} for a {@code boolean} parameter.