Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 332742

Summary: Enhancement Request: Should not check that abstract classes have no-arg constructors
Product: z_Archived Reporter: Blaise Doughan <blaise.doughan>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: eclipselink.oxm-inbox
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
MOXy - Test Cases
none
MOXy - Fix none

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