commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Harald Meyer <harald.me...@rz.uni-karlsruhe.de>
Subject Re: [Digester] Unable to instanciate inner classes
Date Sat, 11 Jan 2003 08:34:25 GMT
Jeanfrancois Arcand wrote:

> before investigating further, I would like to see if somebody else 
> have seen this problem (or aware of...nothing in bugzilla). I have a 
> framework that use the digester this way:
>
> public class HandlerChainDispatcher{
>
> [...]
>
> protected void loadConfiguration(){   
>        Digester digester = new Digester();
>        digester.setValidating(false);
>     digester.addObjectCreate("dispatcher/protocol-mappings/protocol-mapping", 
> ProtocolMappingConfig.class);                                                    
> [...]   
>
> ProtocolMappingConfig is an inner class of HandlerChainDispatcher. 
> ProtocolMappingConfig has a default constructor and it is not abstract.
>
> I'm always having this exception:
>
>      [java] java.lang.InstantiationException: HandlerChainDispatcher. 
> $ProtocolMappingConfig
>      [java]     at      [java]     at 
> java.lang.Class.newInstance0(Class.java:291)
>      [java]     at java.lang.Class.newInstance(Class.java:259)
>      [java]     at 
> org.apache.commons.digester.ObjectCreateRule.begin(ObjectCreateRule.ja
> va:253)
>
> I have tried making the inner class final without success.
>
> My framework is very simple and doesn't use special classloader. I'm 
> trying to track down the problem (and fix it). Any ideas?

Bonjour Jeanfrancois,
I think your trouble is caused by the object generation mode that is 
used by the Digester.
The Digester tries to create your ProtocolMappingConfig instance via 
reflection,
more precisely: by calling the Class.newInstance() method. This object 
creation
fails for inner classes. I'm afraid you MUST change your sources so that
ProtocolMappingConfig is no inner class.  

The following tiny sample program demonstrates this behaviour:

public class Foo {

    private void say ( String s ) {
      System.err.println(s);
    }

    public Foo() throws Exception {
      Bar bar1 = new Bar();
        say("Created bar1: " + bar1);

      Class clazz = Bar.class;
      Bar bar2 = (Bar) clazz.newInstance();
      say("Created bar2: " + bar2);
    }

    public static void main(String[] args) throws Exception {
      new Foo();
      System.exit(0);
    }

    public class Bar {
      public Bar() {
          say("Hello, I'm the Bar constructor");
      }
    }
}

bar1 will be created successfully, but the attempt to create bar2 will 
fail.

Greetings,
Harald


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message