You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Jeanfrancois Arcand <jf...@apache.org> on 2003/01/11 04:24:59 UTC
[Digester] Unable to instanciate inner classes
Hi,
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?
Thanks,
-- Jeanfrancois
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Digester] Unable to instanciate inner classes
Posted by "Craig R. McClanahan" <cr...@apache.org>.
Harald is right about the underlying issue, but there is at least one
additional alternative to consider -- you can try using a
FactoryCreateRule instead of an ObjectCreateRule for creating an object.
The ObjectCreateRule implementation can do anything it wants to create the
object instance to be pushed on the stack.
Craig
On Sat, 11 Jan 2003, Harald Meyer wrote:
> Date: Sat, 11 Jan 2003 09:34:25 +0100
> From: Harald Meyer <ha...@rz.uni-karlsruhe.de>
> Reply-To: Jakarta Commons Developers List <co...@jakarta.apache.org>
> To: Jakarta Commons Developers List <co...@jakarta.apache.org>
> Subject: Re: [Digester] Unable to instanciate inner classes
>
> 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: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Digester] Unable to instanciate inner classes
Posted by Harald Meyer <ha...@rz.uni-karlsruhe.de>.
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: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Digester] Unable to instanciate inner classes
Posted by Jeanfrancois Arcand <jf...@apache.org>.
robert burrell donkin wrote:
> AFAIK only enclosing instances of enclosing classes are allowed to
> create instances of inner classes. (whether by reflection or directly.)
>
> so, if it's an inner class (as opposed to a nested static class) then
> one solution would be to use an FactoryCreateRule and an
> implementation of ObjectCreationFactory which is also an enclosing
> class for the inner class.
Thanks. I will read JLS next time before :-) That worked the way you
described.
-- Jeanfrancois
>
>
> - robert
>
> On Saturday, January 11, 2003, at 03:24 AM, Jeanfrancois Arcand wrote:
>
>> Hi,
>>
>> 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?
>>
>> Thanks,
>>
>> -- Jeanfrancois
>>
>>
>>
>>
>> --
>> To unsubscribe, e-mail:
>> <mailto:commons-dev-unsubscribe@jakarta.apache.
>> org>
>> For additional commands, e-mail:
>> <mailto:commons-dev-help@jakarta.apache.
>> org>
>>
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [Digester] Unable to instanciate inner classes
Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
AFAIK only enclosing instances of enclosing classes are allowed to create
instances of inner classes. (whether by reflection or directly.)
so, if it's an inner class (as opposed to a nested static class) then one
solution would be to use an FactoryCreateRule and an implementation of
ObjectCreationFactory which is also an enclosing class for the inner class.
- robert
On Saturday, January 11, 2003, at 03:24 AM, Jeanfrancois Arcand wrote:
> Hi,
>
> 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?
>
> Thanks,
>
> -- Jeanfrancois
>
>
>
>
> --
> To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@jakarta.apache.
> org>
> For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.
> org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>