commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Diogo Quintela (EF)" <diogo.quint...@ef.pt>
Subject RE: [digester] instantiationException if using internal classes ?
Date Wed, 28 Dec 2005 15:07:43 GMT
Well,
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...

If you really need to handle that type of cases, you may try to use a dumb
bean for digester, and then initialize or inner class with that bean.

http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html. 

-----------------------------------
Diogo Bacelar Quintela
EF - Tecnologias de Informação, Lda.
Av. António Serpa, 26 - 4º Dto.
1050-027 Lisboa, Portugal
Tel: (+351) 217 827 800
Fax: (+351) 217 827 830
Email: diogo.quintela@ef.pt
PGP: 0xF51A5AB9 

> -----Original Message-----
> From: Valerio Schiavoni [mailto:valerio.schiavoni@gmail.com]
> Sent: quarta-feira, 28 de Dezembro de 2005 14:48
> To: Jakarta Commons Users List
> Subject: Re: [digester] instantiationException if using internal classes ?
> 
> Hi Diogo,
> why is this ? does digester require static modifier on internal classes to
> be accessible while parsing the xml ?
> 
> i don't questionate about a possible workaround (still haven't tested) but
> about the principle behind it.
> 
> thanks,
> valerio
> 
> 2005/12/28, Diogo Quintela (EF) <diogo.quintela@ef.pt>:
> >
> > Maybe you should try setting your Student class into "static".
> >
> > Regards
> > Diogo
> >
> > -----------------------------------
> > Diogo Bacelar Quintela
> > EF - Tecnologias de Informação, Lda.
> > Av. António Serpa, 26 - 4º Dto.
> > 1050-027 Lisboa, Portugal
> > Tel: (+351) 217 827 800
> > Fax: (+351) 217 827 830
> > Email: diogo.quintela@ef.pt
> > PGP: 0xF51A5AB9
> >
> > > -----Original Message-----
> > > From: Valerio Schiavoni [mailto:valerio.schiavoni@gmail.com]
> > > Sent: quarta-feira, 28 de Dezembro de 2005 14:03
> > > To: Jakarta Commons Users List
> > > Subject: [digester] instantiationException if using internal classes ?
> > >
> > > Hello, i have the following testCase (inspired by some code found
> > online):
> > >
> > > what it does is to read the xml from string, create a digester, and
> load
> > > the
> > > object: very simple stuff, but i get the following stacktrace:
> > >
> > > 28-dic-2005 14.59.26 org.apache.commons.digester.Digester startElement
> > > GRAVE: Begin event threw exception
> > > java.lang.InstantiationException: jfrr.mapping.StudentDigester$Student
> > >     at java.lang.Class.newInstance0(Class.java:335)
> > >     at java.lang.Class.newInstance(Class.java:303)
> > >     at org.apache.commons.digester.ObjectCreateRule.begin(
> > > ObjectCreateRule.java:205)
> > >     at org.apache.commons.digester.Rule.begin(Rule.java:152)
> > >     at
> > > org.apache.commons.digester.Digester.startElement(Digester.java:1361)
> > >     at
> org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown
> > > Source)
> > >     at
> > > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement
> > (Unk
> > > nown
> > > Source)
> > >     at
> > >
> >
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispa
> > > tcher.dispatch(Unknown
> > > Source)
> > >     at
> > > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument
> > (Unknown
> > > Source)
> > >     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
> > Source)
> > >     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
> > Source)
> > >     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
> > >     at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
> Source)
> > >     at
> org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
> > > Source)
> > >     at org.apache.commons.digester.Digester.parse(Digester.java:1685)
> > >     at jfrr.mapping.StudentDigester$DigestStudents.digest(
> > > StudentDigester.java:53)
> > >     at jfrr.mapping.StudentDigester$DigestStudents.access$0(
> > > StudentDigester.java:32)
> > >     at jfrr.mapping.StudentDigester.testAddBeanPropertySetter(
> > > StudentDigester.java:21)
> > >     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > >     at sun.reflect.NativeMethodAccessorImpl.invoke(
> > > NativeMethodAccessorImpl.java:39)
> > >     at sun.reflect.DelegatingMethodAccessorImpl.invoke(
> > > DelegatingMethodAccessorImpl.java:25)
> > >     at java.lang.reflect.Method.invoke(Method.java:585)
> > >     at junit.framework.TestCase.runTest(TestCase.java:154)
> > >     at junit.framework.TestCase.runBare(TestCase.java:127)
> > >     at junit.framework.TestResult$1.protect(TestResult.java:106)
> > >     at junit.framework.TestResult.runProtected(TestResult.java:124)
> > >     at junit.framework.TestResult.run(TestResult.java:109)
> > >     at junit.framework.TestCase.run(TestCase.java:118)
> > >     at junit.framework.TestSuite.runTest(TestSuite.java:208)
> > >     at junit.framework.TestSuite.run(TestSuite.java:203)
> > >     at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> > > RemoteTestRunner.java:478)
> > >     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
> > > RemoteTestRunner.java:344)
> > >     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> > > RemoteTestRunner.java:196)
> > > java.lang.InstantiationException: jfrr.mapping.StudentDigester$Student
> > >     at
> > > org.apache.commons.digester.Digester.createSAXException(Digester.java
> > > :2919)
> > >     at
> > > org.apache.commons.digester.Digester.createSAXException(Digester.java
> > > :2945)
> > >     at
> > > org.apache.commons.digester.Digester.startElement(Digester.java:1364)
> > >     at
> org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown
> > > Source)
> > >     at
> > > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement
> > (Unk
> > > nown
> > > Source)
> > >     at
> > >
> >
> org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispa
> > > tcher.dispatch(Unknown
> > > Source)
> > >     at
> > > org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument
> > (Unknown
> > > Source)
> > >     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
> > Source)
> > >     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
> > Source)
> > >     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
> > >     at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
> Source)
> > >     at
> org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
> > > Source)
> > >     at org.apache.commons.digester.Digester.parse(Digester.java:1685)
> > >     at jfrr.mapping.StudentDigester$DigestStudents.digest(
> > > StudentDigester.java:53)
> > >     at jfrr.mapping.StudentDigester$DigestStudents.access$0(
> > > StudentDigester.java:32)
> > >     at jfrr.mapping.StudentDigester.testAddBeanPropertySetter(
> > > StudentDigester.java:21)
> > >     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> > >     at sun.reflect.NativeMethodAccessorImpl.invoke(
> > > NativeMethodAccessorImpl.java:39)
> > >     at sun.reflect.DelegatingMethodAccessorImpl.invoke(
> > > DelegatingMethodAccessorImpl.java:25)
> > >     at java.lang.reflect.Method.invoke(Method.java:585)
> > >     at junit.framework.TestCase.runTest(TestCase.java:154)
> > >     at junit.framework.TestCase.runBare(TestCase.java:127)
> > >     at junit.framework.TestResult$1.protect(TestResult.java:106)
> > >     at junit.framework.TestResult.runProtected(TestResult.java:124)
> > >     at junit.framework.TestResult.run(TestResult.java:109)
> > >     at junit.framework.TestCase.run(TestCase.java:118)
> > >     at junit.framework.TestSuite.runTest(TestSuite.java:208)
> > >     at junit.framework.TestSuite.run(TestSuite.java:203)
> > >     at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> > > RemoteTestRunner.java:478)
> > >     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
> > > RemoteTestRunner.java:344)
> > >     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> > > RemoteTestRunner.java:196)
> > >
> > >
> > > The code I used is the following:
> > >
> > > import java.io.StringReader;
> > > import java.util.ArrayList;
> > > import java.util.List;
> > >
> > > import junit.framework.TestCase;
> > >
> > > import org.apache.commons.digester.Digester;
> > > import org.apache.commons.digester.ExtendedBaseRules;
> > >
> > > public class StudentDigester extends TestCase {
> > >
> > >     protected String studentsXML = "<?xml version=\"1.0\"?>"
> > >             + "<students><student>" + "<name>Java Boy</name>"
> > >             + "<course>JSP</course>" + "<age>10</age>"
+ "</student>"
> +
> > > "</students>";
> > >
> > >     public void testAddBeanPropertySetter() {
> > >
> > >         DigestStudents ds = new DigestStudents();
> > >         ds.digest();
> > >
> > >     }
> > >
> > >     private class DigestStudents {
> > >         List<Student> students;
> > >
> > >         public DigestStudents() {
> > >             students = new ArrayList<Student>();
> > >         }
> > >
> > >         private void digest() {
> > >             try {
> > >                 Digester digester = new Digester();
> > >                 digester.setRules(new ExtendedBaseRules());
> > >                 //Push the current object onto the stack
> > >                 digester.push(this);
> > >
> > >                 //Creates a new instance of the Student class
> > >                 digester.addObjectCreate("students/student",
> > > Student.class);
> > >
> > >                 //Uses setName method of the Student instance
> > >                 //Uses tag name as the property name
> > >                 digester.addBeanPropertySetter("students/student/?");
> > >
> > >                 //Uses setCourse method of the Student instance
> > >                 //Explicitly specify property name as 'course'
> > >                 //digester.addBeanPropertySetter(
> > > "students/student/course",
> > > "course" );
> > >
> > >                 //Move to next student
> > >                 digester.addSetNext("students/student", "addStudent");
> > >
> > >                 DigestStudents ds = (DigestStudents)
> digester.parse(new
> > > StringReader(
> > >                         studentsXML));
> > >
> > >                 //Print the contents of the Vector
> > >                 System.out.println("Students List " + ds.students);
> > >             } catch (Exception ex) {
> > >                 ex.printStackTrace();
> > >             }
> > >         }
> > >
> > >         public void addStudent(Student stud) {
> > >             //Add a new Student instance to the Vector
> > >             students.add(stud);
> > >         }
> > >     }
> > >     public class Student {
> > >     private String name;
> > >     private String course;
> > >     private int age;
> > >
> > >     public Student() {
> > >     }
> > >
> > >     public String getName() {
> > >         return name;
> > >     }
> > >
> > >     public void setName(String newName) {
> > >         name = newName;
> > >     }
> > >
> > >     public String getCourse() {
> > >         return course;
> > >     }
> > >
> > >     public void setCourse(String newCourse) {
> > >         course = newCourse;
> > >     }
> > >     public String toString() {
> > >         return("Name="+this.name + " & Course=" +  this.course +" &
> > > Age="+this.age);
> > >     }
> > >
> > >         public int getAge() {
> > >             return age;
> > >         }
> > >
> > >         public void setAge(int age) {
> > >             this.age = age;
> > >         }
> > > }
> > > }
> > >
> > > If i move the public internal class Student outside the testcase
> class,
> > it
> > > works as supposed. IS this normal behaviour ?
> > > thanks,
> > > valerio
> > > --
> > > To Iterate is Human, to Recurse, Divine
> > > James O. Coplien, Bell Labs
> >
> >
> >
> 
> 
> --
> To Iterate is Human, to Recurse, Divine
> James O. Coplien, Bell Labs

Mime
View raw message