commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig McClanahan <craig...@apache.org>
Subject Re: [digester] instantiationException if using internal classes ?
Date Wed, 28 Dec 2005 18:20:29 GMT
On 12/28/05, Diogo Quintela (EF) <diogo.quintela@ef.pt> wrote:
>
> > -----Original Message-----
> > From: Thomas Dudziak [mailto:tomdzk@gmail.com]
> > Sent: quarta-feira, 28 de Dezembro de 2005 15:23
> > To: Jakarta Commons Users List
> > Subject: Re: [digester] instantiationException if using internal classes
> ?
> >
> > On 12/28/05, Diogo Quintela (EF) <diogo.quintela@ef.pt> wrote:
> >
> > > Non-static inner classes as in your example, despite having an empty
> > > constructor, are managed internally by java through a constructor with
> > an
> > > instance to containing class instance (as I believe), so it has direct
> > > access to the instance variables and methods of its enclosing
> instance.
> > >
> > > A brief lookout into reflection api, doesn't seem to be able to
> > instantiate
> > > this type of classes; as digester is trying to instantiate that class
> > though
> > > reflection you must have problems...
> >
> > Inner classes need an outer class instance for creation. If you for
> > instance have this class:
> >
> > public class A
> > {
> >   public class B
> >   {
> >     ...
> >   }
> > }
> >
> > then you can create an instance of B *outside* of A only with:
> >
> > A anInstanceOfA = ...
> > B anInstanceOfB = anInstanceOfA.new B();
> >
> > If I remember correctly, reflection works similar. The inner class
> > won't have a no-arg constructor but instead every of its constructors
> > (even the default one supplied by Java if none is defined)
> > automatically gets an additional first parameter of the type of the
> > enclosing class. So accessing the constructor via reflection would be
> > something like:
> >
> > Constructor constructor = A.B.class.getDeclaredConstructor(new Class[]
> > { A.class});
> >
> > anInstanceOfB = (A.B)constructor.new Instance(new Object[] {
> anInstanceOfA
> > });
>
> You surely put it much cleaner than I did.


Yep :-).

In conclusion, if digester supports any kind of mechanism to define the
> constructors to be used (using values from the stack) Valerio can use
> inner
> classes at will.


And Digester does support such a mechanism, although it's a little more work
on your part.  Using FactoryCreateRule, you can provide a factory class to
produce the object instance that will be pushed on to the stack.  Your
factory can use whatever kind of code it wants in doing so.

Craig

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message