Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 332742 - Enhancement Request: Should not check that abstract classes have no-arg constructors
Summary: Enhancement Request: Should not check that abstract classes have no-arg cons...
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-16 09:19 EST by Blaise Doughan CLA
Modified: 2022-06-09 10:25 EDT (History)
1 user (show)

See Also:


Attachments
MOXy - Test Cases (4.08 KB, patch)
2010-12-16 16:45 EST, Blaise Doughan CLA
no flags Details | Diff
MOXy - Fix (843 bytes, patch)
2010-12-16 16:46 EST, Blaise Doughan CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Blaise Doughan CLA 2010-12-16 09:19:13 EST
The following example throws an error that ClassA does not have a no-arg constructor.  Since ClassA is abstract it doesn't need one.

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

public class JaxbTest {

    public static abstract class ClassA {
        public ClassA(String id) {
        }
    }

    @XmlRootElement
    @XmlJavaTypeAdapter(MyAdapter.class) // does not have an effect
    public static class ClassB extends ClassA {

        public String text;

        public ClassB() {
            super("");
        }
    }

    public static class ValB {
        public String text;
    }

    public static class MyAdapter extends XmlAdapter<ValB, ClassB> {

        @Override
        public ClassB unmarshal(ValB v) throws Exception {
            ClassB b = new ClassB();
            b.text = v.text;
            return b;
        }

        @Override
        public ValB marshal(ClassB v) throws Exception {
            ValB b = new ValB();
            b.text = v.text;
            return b;
        }

    }

    public static void main(String[] args) {
        try {
            JAXBContext context = JAXBContext.newInstance(ClassB.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            unmarshaller.setAdapter(new MyAdapter()); // does not have an effect
            ClassA a = (ClassA) unmarshaller.unmarshal(new File("test.xml"));
            // do somthing with a
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Comment 1 Blaise Doughan CLA 2010-12-16 16:45:55 EST
Created attachment 185374 [details]
MOXy - Test Cases
Comment 2 Blaise Doughan CLA 2010-12-16 16:46:12 EST
Created attachment 185375 [details]
MOXy - Fix
Comment 3 Blaise Doughan CLA 2010-12-17 09:58:54 EST
Fix checked into trunk at rev:

Code reviewed by:  David McCann

Fix Description:  8722
Removed check for no-arg constructor or factory method if the class is abstract.
Comment 4 Eclipse Webmaster CLA 2022-06-09 10:25:43 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink