Difference between revisions of "EclipseLink/Release/2.4.0/JAXB RI Extensions/XML Accessor Factory"

From Eclipsepedia

Jump to: navigation, search
Line 33: Line 33:
 
public class MyAccessorFactory implements AccessorFactory {
 
public class MyAccessorFactory implements AccessorFactory {
  
  public Accessor createFieldAccessor(Class type, Field field, boolean isReadOnly) throws JAXBException {
+
    public Accessor createFieldAccessor(Class type, Field field, boolean isReadOnly) throws JAXBException {
      return new MyFieldAccessor(type);
+
        return new MyFieldAccessor(type, field);
  }
+
    }
  
  public Accessor createPropertyAccessor(Class type, Method getMethod, Method setMethod) throws JAXBException {
+
    public Accessor createPropertyAccessor(Class type, Method getMethod, Method setMethod) throws JAXBException {
      return new MyPropertyAccessor(type);
+
        return new MyPropertyAccessor(type);
  }
+
    }
  
 
}
 
}
Line 45: Line 45:
 
</div>
 
</div>
  
Next,  
+
Next, define one or more subclasses of '''com.sun.xml.bind.v2.runtime.reflect.Accessor''', to be returned from the '''AccessorFactory'''.
 +
 
 +
<div style="width:950px">
 +
<source lang="java">
 +
import java.lang.reflect.Field;
 +
 
 +
import com.sun.xml.bind.api.AccessorException;
 +
import com.sun.xml.bind.v2.runtime.reflect.Accessor;
 +
 
 +
public class MyFieldAccessor extends Accessor {
 +
 
 +
    private Field field;
 +
 
 +
    protected MyFieldAccessor(Class type, Field f) {
 +
        super(type);
 +
        this.field = f;
 +
    }
 +
 
 +
    public Object get(Object obj) throws AccessorException {
 +
 +
    }
 +
 
 +
    public void set(Object obj, Object value) throws AccessorException {
 +
    }
 +
 
 +
}
 +
</source>
 +
</div>
  
 
= Examples =
 
= Examples =

Revision as of 14:38, 4 June 2012

Design Documentation: XML Accessor Factory

ER 372403

In the current JAXB RI, developed by Sun, there are a series of "proprietary" JAXB extensions that are available to provide advanced JAXB functionality outside of the JAXB spec (these extension classes reside in the com.sun.xml.bind package).

By providing implementations of Accessor and AccessorFactory classes, the user can provide their own logic for getting and setting property values, both at the Class and Package level.

This document will outline EclipseLink MOXY's support to this extension.


Behaviour

Classes (or packages) annotated with XmlAccessorFactory will use the supplied AccessorFactory subclass to create Accessors to set and get the values of JAXB properties. By implementing custom Accessor classes, the user will have complete control over the setting and getting of property values. This could be used, for example, to implement a custom lazy-loading strategy.


Configuration

First, define an implementation of the com.sun.xml.bind.AccessorFactory interface, which specifies two methods: createFieldAccessor() and createPropertyAccessor(). This allows you to provide different functionality for Field access and Property access by defining separate Accessor classes.

import java.lang.reflect.Field;
import java.lang.reflect.Method;
 
import javax.xml.bind.JAXBException;
 
import com.sun.xml.bind.AccessorFactory;
import com.sun.xml.bind.v2.runtime.reflect.Accessor;
 
public class MyAccessorFactory implements AccessorFactory {
 
    public Accessor createFieldAccessor(Class type, Field field, boolean isReadOnly) throws JAXBException {
        return new MyFieldAccessor(type, field);
    }
 
    public Accessor createPropertyAccessor(Class type, Method getMethod, Method setMethod) throws JAXBException {
        return new MyPropertyAccessor(type);
    }
 
}

Next, define one or more subclasses of com.sun.xml.bind.v2.runtime.reflect.Accessor, to be returned from the AccessorFactory.

import java.lang.reflect.Field;
 
import com.sun.xml.bind.api.AccessorException;
import com.sun.xml.bind.v2.runtime.reflect.Accessor;
 
public class MyFieldAccessor extends Accessor {
 
    private Field field;
 
    protected MyFieldAccessor(Class type, Field f) {
        super(type);
        this.field = f;
    }
 
    public Object get(Object obj) throws AccessorException {
 
    }
 
    public void set(Object obj, Object value) throws AccessorException {
    }
 
}

Examples