commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: Digester Problem related to Bean Classname
Date Thu, 06 Feb 2003 16:40:25 GMT


On Thu, 6 Feb 2003, Maher Martin wrote:

> Date: Thu, 6 Feb 2003 16:20:58 +0100
> From: Maher Martin <MMaher@Webasto.de>
> Reply-To: Jakarta Commons Users List <commons-user@jakarta.apache.org>
> To: "'commons-user@jakarta.apache.org'" <commons-user@jakarta.apache.org>
> Subject: Digester Problem related to Bean Classname
>
> Hi,
>
> I've encountered a strange problem with Digester which is related to the
> name of the java bean class. If I name the Java class "x.y.Component" then
> the <set-properties-rule /> fails to call the setter methods for the bean
> class. However if I rename the class to "x.y.Component2" all setters are
> invoked by Digester when parsing the XML.
>
> Can anybody explain what's going on here? I've included examples below.
>

This is going to take a few paragraphs, but here goes ...

Digester (and beanutils, for that matter) use Java's introspection
capabilities (java.beans.Introspector) to find the available methods and
properties of bean classes.  Normally, properties are identified by the
naming pattern used on the methods (getFoo() and setFoo()), but you can
also override this if you provide a BeanInfo class associated with your
bean class (see the Introspector Javadocs and the JavaBeans Specification
for more info).

Now, there are two ways to associate a BeanInfo class with your bean:

* Put it in the same package as your bean class, with the same
  classname + BeanInfo on the end (for a bean MyBean, the corresponding
  class would be MyBeanBeanInfo).

* Tell the introspector to use a particular "search path" (set of
  package names) to search in for BeanInfo classes.

Unfortunately, the default search path in the JDK includes a directory
that contains a class named ComponentBeanInfo -- which tells the
introspector that any class named "Component" will use this BeanInfo class
to identify its properties, rather than looking at the methods of your
class.  Thus, *your* properties are not recognized.

There's two workarounds:

* Name your class anything other than "Component".  (This seems
  to be the only BeanInfo class available in the default JDK list)

* Call Introspector.setBeanInfoSearchPath() to set a different
  set of search directories, before using Digester.

> Best Regards
>
> Martin Maher
>

Craig McClanahan


> ------ Component Class --------
>
> package x.y
>
> public class Component {
>
>   private int _number;
>   private String _type;
>
>   public Component() {
>   }
>
>   public void setNumber(int number) {
>     _number = number;
>   }
>
>   public void setType(String type) {
>     _type = type;
>   }
>
>   public int getNumber() {
>     return _number;
>   }
>
>   public String getType() {
>     return _type;
>   }
>
>   public String toString() {
>     String newline = System.getProperty("line.separator");
>     StringBuffer sBuf = new StringBuffer();
>
>     sBuf.append("Number:").append(_number).append(newline);
>     sBuf.append("Type:").append(_type).append(newline);
>
>     return sBuf.toString();
>   }
>
> }
>
> ------INPUT XML FILE--------
>
> <?xml version="1.0"?>
>
> <component number="1" type="a">
> </component>
>
> ------ XML RULES --------
>
> <?xml version="1.0"?>
>
> <digester-rules>
>
> 	<pattern value="component">
> 		<object-create-rule classname="x.y.Component" />
> 		<set-properties-rule />
> 	</pattern>
>
> </digester-rules>
> --------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>

Mime
View raw message