[digester] instantiationException if using internal classes ?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[digester] instantiationException if using internal classes ?

Valerio
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(Unknown
Source)
    at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.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(Unknown
Source)
    at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.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
Reply | Threaded
Open this post in threaded view
|

RE: [digester] instantiationException if using internal classes ?

Diogo Quintela (EF)
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: [hidden email]
PGP: 0xF51A5AB9

> -----Original Message-----
> From: Valerio Schiavoni [mailto:[hidden email]]
> 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

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [digester] instantiationException if using internal classes ?

Valerio
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) <[hidden email]>:

>
> 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: [hidden email]
> PGP: 0xF51A5AB9
>
> > -----Original Message-----
> > From: Valerio Schiavoni [mailto:[hidden email]]
> > 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
Reply | Threaded
Open this post in threaded view
|

RE: [digester] instantiationException if using internal classes ?

Diogo Quintela (EF)
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: [hidden email]
PGP: 0xF51A5AB9

> -----Original Message-----
> From: Valerio Schiavoni [mailto:[hidden email]]
> 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) <[hidden email]>:
> >
> > 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: [hidden email]
> > PGP: 0xF51A5AB9
> >
> > > -----Original Message-----
> > > From: Valerio Schiavoni [mailto:[hidden email]]
> > > 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

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [digester] instantiationException if using internal classes ?

Thomas Dudziak
On 12/28/05, Diogo Quintela (EF) <[hidden email]> 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 });

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [digester] instantiationException if using internal classes ?

Diogo Quintela (EF)
> -----Original Message-----
> From: Thomas Dudziak [mailto:[hidden email]]
> 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) <[hidden email]> 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.

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.

>
> Tom
>

Diogo

> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]




---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] instantiationException if using internal classes ?

craigmcc
On 12/28/05, Diogo Quintela (EF) <[hidden email]> wrote:

>
> > -----Original Message-----
> > From: Thomas Dudziak [mailto:[hidden email]]
> > 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) <[hidden email]> 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