commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject RE: [Digester] Can use inner classes?
Date Wed, 18 Oct 2006 08:18:48 GMT
Hi Eva,

Firstly, your code excerpt below isn't showing inner classes; it is
showing package-scope classes that happen to be in the same file:
  public class Parser {
  }
  class ClassA {
  }
In this case, ClassA is a normal class (NOT an inner class), but is
accessable only to code in the same package. If this is really what you
have, then digester obviously cannot create instances of it, as only
code in the same package is permitted to call the ClassA constructor.

I'm guessing from your description, however, that this is just a typo
and you really have:
  public class Parser {
    class ClassA {
    }
  }
In this case, the normal java inner-class rules mean that no java code
can create instances of ClassA except by first obtaining a reference to
some instance of class Parser, eg
  Object o = new Parser.ClassA()
is simply forbidden by the rules of inner classes. What is needed is
  Object o = new someParserInstance.ClassA().

In either case, you could probably use FactoryCreateRule to solve the
issue.

In the first case (package-scope classes), I think you can create an
ObjectCreationFactory class that is in the same package as your Parser
class. That factory should then have permissions to create instances of
ClassA etc.

In the second case (non-static inner class), your ObjectCreationFactory
would need to obtain a Parser instance from somewhere (that's your
problem) then it can create an instance of the desired inner class.


Using FactoryCreateRule/ObjectCreationFactory is pretty straightforward
when using the Digester API. As you're using that xmlrules stuff, you'll
also need to figure out how to access that functionality somehow.
Really, using the API is so much easier...

Regards,

Simon

On Tue, 2006-10-17 at 22:51 -0400, Allen,Eva wrote:
> That's what causes the illegal access exception.  The package is
> openurl.  Class A is Servers; Class B is Server.  The rule
> 
>    <object-create-rule classname="openurl.Server" />
> 
> causes a class not found" exception. The rule
> 
>    <object-create-rule classname="openurl.Parser$Server" />
> 
> causes the illegal access exception that I mentioned in my original
> email.  If I make the classes not static, I get an instantiation
> exception.
> 
> -----Original Message-----
> From: mfncooper@gmail.com [mailto:mfncooper@gmail.com] On Behalf Of
> Martin Cooper
> Sent: Tuesday, October 17, 2006 10:29 PM
> To: Jakarta Commons Users List
> Subject: Re: [Digester] Can use inner classes?
> 
> On 10/17/06, Allen,Eva <allene@oclc.org> wrote:
> >
> >
> > I'm attempting to write a small parser as part of another larger 
> > application.  I'd like to do something like the following:
> >
> > public class Parser {
> >
> > ...
> >         ClassA s = new ClassA();
> >         Digester d;
> >         d.push(s);
> >       ...
> >         d.parse(inputStream);
> >       ...
> > }
> > class ClassA {
> >    LinkedList ClassB;
> >    public void addClassB();
> > }
> > class ClassB {
> >   String data1;
> >   String data2;
> >   String data3;
> >   public getters and setters;
> > }
> >
> > I'm using an xmlrules file looks like
> > <digester-rules>
> > ...
> >    <pattern value="resolver">
> >         <object-create-rule classname="ClassB" />
> >         <set-properties-rule />
> >         <set-next-rule methodname="addClassB" />
> >
> >         <pattern value="data1">
> >                 <call-method-rule methodName="setData1" paramCount="0"
> > />
> >       </pattern>
> >         ...
> >    </pattern>
> > ...
> > </digester-rules>
> >
> > This all works fine as long as ClassA and ClassB are public classes.
> > But as soon as I try to make them inner classes of the Parser class 
> > this fails with an IllegalAccessException (Cannot access a member of 
> > class ClassB with modifiers "").
> 
> 
> This should be fine as long as your inner classes are both public and
> static.
> 
> --
> Martin Cooper
> 
> 
> The Digester Developer Guide mentions something about this as a known
> > limitation and says to ask on the mailing list if I think I'm 
> > experiencing the problem it mentions.  So I'm asking.  Is there a way 
> > around this or am I forced to have public classes for use with the 
> > digester?
> >
> > Since I originally wrote this I've found that I could use the 
> > FactoryCreateRule to create an object that doesn't have a no-argument 
> > constructor, but I haven't been able to find a proper ruleset that 
> > will allow me to do this.  Any suggestions?
> > --
> > Eva Allen
> > Consulting Software Engineer, OCLC, Inc.
> > 6565 Frantz Rd., Dublin, OH  43017
> > 614.764.6009 | allene@oclc.org
> > Views contained herein are my own; they do not necessarily reflect 
> > those of my employer
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


---------------------------------------------------------------------
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