Author: simonetripodi
Date: Thu Jun 23 15:04:01 2011
New Revision: 1138914
URL: http://svn.apache.org/viewvc?rev=1138914&view=rev
Log:
[DIGESTER-18] ObjectCreateRule shouldn't keep className as a field
this fix contains a little variant respect to the proposed suggestion: users
can still create classes by specifying just the class name, but when passed the
Class object in the constructor, it doesn't need to be load dynamically
Modified:
commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/ObjectCreateRule.java
Modified:
commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/ObjectCreateRule.java
URL:
http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/ObjectCreateRule.java?rev=1138914&r1=1138913&r2=1138914&view=diff
==============================================================================
---
commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/ObjectCreateRule.java
(original)
+++
commons/proper/digester/trunk/src/main/java/org/apache/commons/digester3/ObjectCreateRule.java
Thu Jun 23 15:04:01 2011
@@ -49,6 +49,7 @@ public class ObjectCreateRule
public ObjectCreateRule( Class<?> clazz )
{
this( clazz.getName(), (String) null );
+ this.clazz = clazz;
}
/**
@@ -73,6 +74,7 @@ public class ObjectCreateRule
public ObjectCreateRule( String attributeName, Class<?> clazz )
{
this( clazz.getName(), attributeName );
+ this.clazz = clazz;
}
// ----------------------------------------------------- Instance Variables
@@ -83,6 +85,11 @@ public class ObjectCreateRule
protected String attributeName = null;
/**
+ * The Java class of the object to be created.
+ */
+ protected Class<?> clazz = null;
+
+ /**
* The Java class name of the object to be created.
*/
protected String className = null;
@@ -96,24 +103,29 @@ public class ObjectCreateRule
public void begin( String namespace, String name, Attributes attributes )
throws Exception
{
- // Identify the name of the class to instantiate
- String realClassName = className;
- if ( attributeName != null )
+ Class<?> clazz = this.clazz;
+
+ if ( clazz == null )
{
- String value = attributes.getValue( attributeName );
- if ( value != null )
+ // Identify the name of the class to instantiate
+ String realClassName = className;
+ if ( attributeName != null )
{
- realClassName = value;
+ String value = attributes.getValue( attributeName );
+ if ( value != null )
+ {
+ realClassName = value;
+ }
+ }
+ if ( getDigester().getLogger().isDebugEnabled() )
+ {
+ getDigester().getLogger().debug( "[ObjectCreateRule]{" +
getDigester().getMatch() + "}New "
+ + realClassName );
}
- }
- if ( getDigester().getLogger().isDebugEnabled() )
- {
- getDigester().getLogger().debug( "[ObjectCreateRule]{" +
getDigester().getMatch() + "}New "
- + realClassName );
- }
- // Instantiate the new object and push it on the context stack
- Class<?> clazz = getDigester().getClassLoader().loadClass(
realClassName );
+ // Instantiate the new object and push it on the context stack
+ clazz = getDigester().getClassLoader().loadClass( realClassName );
+ }
Object instance = clazz.newInstance();
getDigester().push( instance );
}