about jaxb accessor:

jaxb ri implementation create inner class to acess faster to
properties/fields. In our case maybe the creation is longer than the
accesses (used only @ start up).

Supposing we are using the RI implementation of JAXB (i think we are) we can
maybe follow this idea:

// context instantiation
JAXBContext ctx = JAXBContext.newInstance("org.apache.openejb",
OpenEJB.class.getClassLoader(), new HashMap<String, Object>() {{
            put(JAXBRIContext.XMLACCESSORFACTORY_SUPPORT, true);
        }});

// package-info.java
@XmlAccessorFactory(AccessorFactoryImpl.class)
package org.apache.openejb; // to xml package of course


import com.sun.xml.bind.XmlAccessorFactory;

// and finally out AccessorFactoryImpl and Accessor implementations:
import com.sun.xml.bind.api.AccessorException;
import com.sun.xml.bind.v2.runtime.reflect.Accessor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AccessorFactoryImpl implements com.sun.xml.bind.AccessorFactory
{

    public Accessor createFieldAccessor(Class bean, Field field, boolean
readOnly) {
        throw new UnsupportedOperationException();
    }

    public Accessor createPropertyAccessor(Class bean, Method getter, Method
setter) {
        return new Accessor(bean) {
            @Override public Object get(Object o) throws AccessorException {
                // TODO
                return null;
            }

            @Override public void set(Object o, Object o1) throws
AccessorException {
                // TODO
            }
        };
    }
}


Doing it no more inner classes are generated.

any thoughts?

- Romain

2011/9/26 David Blevins <[email protected]>

>
> On Sep 26, 2011, at 2:28 AM, Fredrik Jonson wrote:
>
> > In <[email protected]> David Blevins wrote:
> >
> >> # JAXB Accessors
> >>
> >> One of the biggest polluters of the class space is JAXB as it generates
> >> accessors for all the fields.  First thought is, gee, I wonder if we can
> do
> >> that at build time and keep the class definitions.  If there's some way
> we
> >> could write down those classes, it could save us a lot of trouble at
> >> runtime.
> >
> > I'm sure I misunderstand you. Are you saying that openejb is generating
> > class-files from jaxb schemas in runtime?
>
> The JAXB RI itself generates byte code internally to eliminate the need for
> reflection by creating non-static inner classes that can get/set the private
> state (Foo$JaxbAccessor..).
>
>  people.apache.org/~dblevins/jaxb-accessors.png
>
> Unfortunately, heavy reflection is used in the process of creating these
> classes so it ends up being a "slow start, faster afterwards" kind of a
> trade-off.  If we could simply keep these classes and pack them in the jar
> at build time, we could eliminate the slow start part and just get straight
> to the "fast" part.
>
> Not sure if there is already some special flag to do that or if we'd need
> to hack something up.  But where there's a will there's a way.
>
> Worst case scenario is we find whatever class it is in the RI that calls
> 'Unsafe.defineClass(...)' and replace it with a version that also writes the
> bytes to disk.  We then put that class in the endorsed dir and wrap it up as
> some kind of maven plugin or command-line tool.
>
> Couple days of hacking for an awesome return.
>
>
> -David
>
>

Reply via email to