Bug 392574 - Support adapting to collection interfaces with XmlAdapter
Summary: Support adapting to collection interfaces with XmlAdapter
Status: NEW
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-10-22 10:00 EDT by Michal Politowski CLA
Modified: 2022-06-09 10:02 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michal Politowski CLA 2012-10-22 10:00:34 EDT
Currently an attempt to marshal the following example model using an adapter results in errors both in MOXy and in the default JAXB implementation.

The errors are different.
MOXy fails because of a ClassCastException during marshaling. The JAXB in Java 7 fails earlier, with IllegalAnnotationException during context creation.

Since this case seems to go outside the spec by using interfaces, maybe it could be handled as an extension. The result would be as if the model had, in place of the property annotated with XmlJavaTypeAdapter, a collection property of the value type of the adapter?

This could be used eg. as a nicer solution to the problem from http://stackoverflow.com/questions/8403623/jaxb-xmljavatypeadapter-usage

Nicer in the sense that the name of the XML element that becomes a child of the model element would come from the property name or annotations in the model, not from the adapter value class.
This is especially good when the model has many similar properties that need to be adapted, since with this enhancement only one adapter class could be used, instead of one per property.

Example model and adapter follow:

package mpol.moxy;

import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Model {

    @XmlJavaTypeAdapter(NiceModelPropertyAdapter.class)
    public Map<String, String> property;
}

package mpol.moxy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class NiceModelPropertyAdapter extends XmlAdapter<List<NiceModelPropertyAdapter.AdaptedEntry>, Map<String, String>> {

    static class AdaptedEntry {
        AdaptedEntry(String l, String v) {
            label = l;
            value = v;
        }
        AdaptedEntry() { /* For JAXB */ }

        @XmlAttribute
        public String label;
        @XmlValue
        public String value;
    }

    @Override
    public Map<String, String> unmarshal(List<AdaptedEntry> ps) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        for (AdaptedEntry e : ps) {
            map.put(e.label, e.value);
        }
        return map;
    }

    @Override
    public List<AdaptedEntry> marshal(Map<String, String> map) throws Exception {
        ArrayList<AdaptedEntry> l = new ArrayList<AdaptedEntry>();
        for (String k : map.keySet()) {
            l.add(new AdaptedEntry(k, map.get(k)));
        }
        return l;
    }
}
Comment 1 Eclipse Webmaster CLA 2022-06-09 10:02:43 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink