You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2002/07/29 23:05:15 UTC
cvs commit: jakarta-commons/digester/src/test/org/apache/commons/digester Employee.java RuleTestCase.java Test5.xml
rdonkin 2002/07/29 14:05:15
Modified: digester/src/java/org/apache/commons/digester
CallMethodRule.java Digester.java
digester/src/test/org/apache/commons/digester Employee.java
RuleTestCase.java Test5.xml
Log:
Added support for primitive method calls to CallMethodRule.
Revision Changes Path
1.18 +54 -9 jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java
Index: CallMethodRule.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/CallMethodRule.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- CallMethodRule.java 5 Jun 2002 21:23:23 -0000 1.17
+++ CallMethodRule.java 29 Jul 2002 21:05:15 -0000 1.18
@@ -72,13 +72,22 @@
/**
- * Rule implementation that calls a method on the top (parent)
+ * <p>Rule implementation that calls a method on the top (parent)
* object, passing arguments collected from subsequent
* <code>CallParamRule</code> rules or from the body of this
* element.
* By using {@link #CallMethodRule(String methodName)}
* a method call can be made to a method which accepts no
- * arguments.
+ * arguments.</p>
+ *
+ * <p>This rule now uses {@link MethodUtils#invokeMethod} by default.
+ * This increases the kinds of methods successfully and allows primitives
+ * to be matched by passing in wrapper classes.
+ * There are rare cases when {@link MethodUtils#invokeExactMethod}
+ * (the old defualt) is required.
+ * This method is much stricter in it's reflection.
+ * Setting the <code>UseExactMatch</code> to true reverts to the use of this
+ * method.</p>
*
* @author Craig McClanahan
* @author Scott Sanders
@@ -306,7 +315,29 @@
*/
private String paramClassNames[] = null;
+ /**
+ * Should <code>MethodUtils.invokeExactMethod</code> be used for reflection.
+ */
+ protected boolean useExactMatch = false;
+
// --------------------------------------------------------- Public Methods
+
+ /**
+ * Should <code>MethodUtils.invokeExactMethod</code>
+ * be used for the reflection.
+ */
+ public boolean getUseExactMatch() {
+ return useExactMatch;
+ }
+
+ /**
+ * Set whether <code>MethodUtils.invokeExactMethod</code>
+ * should be used for the reflection.
+ */
+ public void setUseExactMatch(boolean useExactMatch)
+ {
+ this.useExactMatch = useExactMatch;
+ }
/**
* Set the associated digester.
@@ -375,7 +406,13 @@
if (paramCount > 0) {
parameters = (String[]) digester.popParams();
-
+
+ if (digester.log.isTraceEnabled()) {
+ for (int i=0,size=parameters.length;i<size;i++) {
+ digester.log.trace("[CallMethodRule](" + i + ")" + parameters[i]) ;
+ }
+ }
+
// In the case where the parameter for the method
// is taken from an attribute, and that attribute
// isn't actually defined in the source XML file,
@@ -443,9 +480,17 @@
sb.append(")");
digester.log.debug(sb.toString());
}
- MethodUtils.invokeExactMethod(top, methodName,
+
+ if (useExactMatch) {
+ // invoke using exact match
+ MethodUtils.invokeExactMethod(top, methodName,
paramValues, paramTypes);
-
+
+ } else {
+ // invoke using fuzzier match
+ MethodUtils.invokeMethod(top, methodName,
+ paramValues, paramTypes);
+ }
}
1.56 +10 -5 jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java
Index: Digester.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/java/org/apache/commons/digester/Digester.java,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -r1.55 -r1.56
--- Digester.java 16 Jul 2002 21:23:27 -0000 1.55
+++ Digester.java 29 Jul 2002 21:05:15 -0000 1.56
@@ -2044,6 +2044,9 @@
Object popParams() {
try {
+ if (log.isTraceEnabled()) {
+ log.trace("Popping params");
+ }
return (params.pop());
} catch (EmptyStackException e) {
log.warn("Empty stack (returning null)");
@@ -2062,7 +2065,9 @@
* @param object The new object
*/
void pushParams(Object object) {
-
+ if (log.isTraceEnabled()) {
+ log.trace("Pushing params");
+ }
params.push(object);
}
1.5 +26 -4 jakarta-commons/digester/src/test/org/apache/commons/digester/Employee.java
Index: Employee.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/Employee.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Employee.java 8 Jul 2002 18:26:13 -0000 1.4
+++ Employee.java 29 Jul 2002 21:05:15 -0000 1.5
@@ -124,7 +124,9 @@
// this is to allow testing of primitive convertion
private int age;
-
+ private boolean active;
+ private float salary;
+
public int getAge()
{
return age;
@@ -133,6 +135,26 @@
public void setAge(int age)
{
this.age = age;
+ }
+
+ public boolean isActive()
+ {
+ return active;
+ }
+
+ public void setActive(boolean active)
+ {
+ this.active = active;
+ }
+
+ public float getSalary()
+ {
+ return salary;
+ }
+
+ public void setSalary(float salary)
+ {
+ this.salary = salary;
}
public String toString() {
1.18 +66 -12 jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java
Index: RuleTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/RuleTestCase.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- RuleTestCase.java 16 Jul 2002 21:23:28 -0000 1.17
+++ RuleTestCase.java 29 Jul 2002 21:05:15 -0000 1.18
@@ -667,19 +667,16 @@
* even methods with no arguments.
*/
public void testCallMethod2() throws Exception {
- /*
- I was preparing this test case to fix another bug
- i'll uncomment it once i've fixed it
+
+ //I was preparing this test case to fix another bug
+ // i'll uncomment it once i've fixed it
// Configure the digester as required
digester.addObjectCreate("employee", Employee.class);
// try all syntax permutations
digester.addCallMethod("employee", "setLastName", 1, new String[] {"java.lang.String"});
digester.addCallParam("employee/lastName", 0);
- // try out primitive convertion
- digester.addCallMethod("employee", "setAge", 1, new Class[] {Integer.class});
- digester.addCallParam("employee/age", 0);
-
+
// Parse our test input
Object root1 = null;
try {
@@ -687,13 +684,70 @@
root1 = digester.parse(getInputStream("Test5.xml"));
Employee employee = (Employee) root1;
assertEquals("Failed to call Employee.setLastName", "Last Name", employee.getLastName());
+
+ } catch (Throwable t) {
+ // this means that the method can't be found and so the test fails
+ fail("Digester threw Exception: " + t);
+ }
+
+
+ digester = new Digester();
+ // Configure the digester as required
+ digester.addObjectCreate("employee", Employee.class);
+ // try out primitive convertion
+ digester.addCallMethod("employee", "setAge", 1, new Class[] {Integer.class});
+ digester.addCallParam("employee/age", 0);
+
+ // Parse our test input
+ root1 = null;
+ try {
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
+ Employee employee = (Employee) root1;
assertEquals("Failed to call Employee.setAge", 21, employee.getAge());
} catch (Throwable t) {
// this means that the method can't be found and so the test fails
fail("Digester threw Exception: " + t);
}
- */
+
+ digester = new Digester();
+ // Configure the digester as required
+ digester.addObjectCreate("employee", Employee.class);
+ digester.addCallMethod("employee", "setActive", 1, new Class[] {Boolean.class});
+ digester.addCallParam("employee/active", 0);
+
+ // Parse our test input
+ root1 = null;
+ try {
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
+ Employee employee = (Employee) root1;
+ assertEquals("Failed to call Employee.setActive", true, employee.isActive());
+
+ } catch (Throwable t) {
+ // this means that the method can't be found and so the test fails
+ fail("Digester threw Exception: " + t);
+ }
+
+ digester = new Digester();
+ // Configure the digester as required
+ digester.addObjectCreate("employee", Employee.class);
+ digester.addCallMethod("employee", "setSalary", 1, new Class[] {Float.class});
+ digester.addCallParam("employee/salary", 0);
+
+ // Parse our test input
+ root1 = null;
+ try {
+ // an exception will be thrown if the method can't be found
+ root1 = digester.parse(getInputStream("Test5.xml"));
+ Employee employee = (Employee) root1;
+ assertEquals("Failed to call Employee.setSalary", 1000000.0f, employee.getSalary(), 0.1f);
+
+ } catch (Throwable t) {
+ // this means that the method can't be found and so the test fails
+ fail("Digester threw Exception: " + t);
+ }
}
/**
1.3 +1 -1 jakarta-commons/digester/src/test/org/apache/commons/digester/Test5.xml
Index: Test5.xml
===================================================================
RCS file: /home/cvs/jakarta-commons/digester/src/test/org/apache/commons/digester/Test5.xml,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Test5.xml 8 Jul 2002 18:26:14 -0000 1.2
+++ Test5.xml 29 Jul 2002 21:05:15 -0000 1.3
@@ -1 +1 @@
-<?xml version="1.0"?>
<employee>
<firstName> First Name </firstName>
<lastName> Last Name </lastName>
<age>21</age>
<address>
<type>home</type>
<street>Home Street</street>
<city>Home City</city>
<state>HS</state>
<zipCode>HmZip</zipCode>
</address>
<address>
<type>office</type>
<street>Office Street</street>
<city>Office City</city>
<state>OS</state>
<zipCode>OfZip</zipCode>
</address>
</employee>
\ No newline at end of file
+<?xml version="1.0"?>
<employee>
<firstName> First Name </firstName>
<lastName> Last Name </lastName>
<age>21</age>
<salary>1000000</salary>
<active>true</active>
<address>
<type>home</type>
<street>Home Street</street>
<city>Home City</city>
<state>HS</state>
<zipCode>HmZip</zipCode>
</address>
<address>
<type>office</type>
<street>Office Street</street>
<city>Office City</city>
<state>OS</state>
<zipCode>OfZip</zipCode>
</address>
</employee>
\ No newline at end of file
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>