325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.bind.v2.runtime;
325N/A
325N/Aimport java.io.IOException;
325N/Aimport java.lang.reflect.InvocationTargetException;
325N/Aimport java.lang.reflect.Method;
325N/Aimport java.util.Arrays;
325N/Aimport java.util.Collection;
325N/Aimport java.util.Collections;
325N/Aimport java.util.logging.Level;
325N/Aimport java.util.logging.Logger;
325N/A
325N/Aimport javax.xml.bind.JAXBContext;
325N/Aimport javax.xml.bind.Marshaller;
325N/Aimport javax.xml.bind.Unmarshaller;
325N/Aimport javax.xml.datatype.XMLGregorianCalendar;
325N/Aimport javax.xml.namespace.QName;
325N/Aimport javax.xml.stream.XMLStreamException;
325N/A
325N/Aimport com.sun.istack.internal.NotNull;
325N/Aimport com.sun.xml.internal.bind.Util;
325N/Aimport com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
325N/Aimport com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
325N/A
325N/Aimport org.xml.sax.SAXException;
325N/A
325N/A/**
325N/A * Encapsulates various JAXB operations on objects bound by JAXB.
325N/A * Immutable and thread-safe.
325N/A *
325N/A * <p>
325N/A * Each JAXB-bound class has a corresponding {@link JaxBeanInfo} object,
325N/A * which performs all the JAXB related operations on behalf of
325N/A * the JAXB-bound object.
325N/A *
325N/A * <p>
325N/A * Given a class, the corresponding {@link JaxBeanInfo} can be located
325N/A * via {@link JAXBContextImpl#getBeanInfo(Class,boolean)}.
325N/A *
325N/A * <p>
325N/A * Typically, {@link JaxBeanInfo} implementations should be generated
325N/A * by XJC/JXC. Those impl classes will register themselves to their
325N/A * master <tt>ObjectFactory</tt> class.
325N/A *
325N/A * <p>
325N/A * The type parameter BeanT is the Java class of the bean that this represents.
325N/A *
325N/A * @author
325N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
325N/A */
325N/Apublic abstract class JaxBeanInfo<BeanT> {
325N/A
325N/A protected boolean isNilIncluded = false;
325N/A
325N/A /**
325N/A * For {@link JaxBeanInfo} that has multiple type names.
325N/A */
325N/A protected JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, QName[] typeNames, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) {
325N/A this(grammar,rti,jaxbType,(Object)typeNames,isElement,isImmutable,hasLifecycleEvents);
325N/A }
325N/A
325N/A /**
325N/A * For {@link JaxBeanInfo} that has one type name.
325N/A */
325N/A protected JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, QName typeName, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) {
325N/A this(grammar,rti,jaxbType,(Object)typeName,isElement,isImmutable,hasLifecycleEvents);
325N/A }
325N/A
325N/A /**
325N/A * For {@link JaxBeanInfo} that has no type names.
325N/A */
325N/A protected JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) {
325N/A this(grammar,rti,jaxbType,(Object)null,isElement,isImmutable,hasLifecycleEvents);
325N/A }
325N/A
325N/A private JaxBeanInfo(JAXBContextImpl grammar, RuntimeTypeInfo rti, Class<BeanT> jaxbType, Object typeName, boolean isElement,boolean isImmutable, boolean hasLifecycleEvents) {
325N/A grammar.beanInfos.put(rti,this);
325N/A
325N/A this.jaxbType = jaxbType;
325N/A this.typeName = typeName;
325N/A this.flag = (short)((isElement?FLAG_IS_ELEMENT:0)
325N/A |(isImmutable?FLAG_IS_IMMUTABLE:0)
325N/A |(hasLifecycleEvents?FLAG_HAS_LIFECYCLE_EVENTS:0));
325N/A }
325N/A
325N/A /**
325N/A * Various boolean flags combined into one field to improve memory footprint.
325N/A */
325N/A protected short flag;
325N/A
325N/A private static final short FLAG_IS_ELEMENT = 1;
325N/A private static final short FLAG_IS_IMMUTABLE = 2;
325N/A private static final short FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL = 4;
325N/A private static final short FLAG_HAS_BEFORE_UNMARSHAL_METHOD = 8;
325N/A private static final short FLAG_HAS_AFTER_UNMARSHAL_METHOD = 16;
325N/A private static final short FLAG_HAS_BEFORE_MARSHAL_METHOD = 32;
325N/A private static final short FLAG_HAS_AFTER_MARSHAL_METHOD = 64;
325N/A private static final short FLAG_HAS_LIFECYCLE_EVENTS = 128;
325N/A
325N/A /** cache of lifecycle methods */
325N/A private LifecycleMethods lcm = null;
325N/A
325N/A /**
325N/A * True if {@link #jaxbType} has the lifecycle method.
325N/A */
325N/A public final boolean hasBeforeUnmarshalMethod() {
325N/A return (flag&FLAG_HAS_BEFORE_UNMARSHAL_METHOD) != 0;
325N/A }
325N/A
325N/A /**
325N/A * True if {@link #jaxbType} has the lifecycle method.
325N/A */
325N/A public final boolean hasAfterUnmarshalMethod() {
325N/A return (flag&FLAG_HAS_AFTER_UNMARSHAL_METHOD) != 0;
325N/A }
325N/A
325N/A /**
325N/A * True if {@link #jaxbType} has the lifecycle method.
325N/A */
325N/A public final boolean hasBeforeMarshalMethod() {
325N/A return (flag&FLAG_HAS_BEFORE_MARSHAL_METHOD) != 0;
325N/A }
325N/A
325N/A /**
325N/A * True if {@link #jaxbType} has the lifecycle method.
325N/A */
325N/A public final boolean hasAfterMarshalMethod() {
325N/A return (flag&FLAG_HAS_AFTER_MARSHAL_METHOD) != 0;
325N/A }
325N/A
325N/A /**
325N/A * Gets the JAXB bound class type that this {@link JaxBeanInfo}
325N/A * handles.
325N/A *
325N/A * <p>
325N/A * IOW, when a bean info object is requested for T,
325N/A * sometimes the bean info for one of its base classes might be
325N/A * returned.
325N/A */
325N/A public final Class<BeanT> jaxbType;
325N/A
325N/A /**
325N/A * Returns true if the bean is mapped to/from an XML element.
325N/A *
325N/A * <p>
325N/A * When this method returns true, {@link #getElementNamespaceURI(Object)}
325N/A * and {@link #getElementLocalName(Object)} returns the element name of
325N/A * the bean.
325N/A */
325N/A public final boolean isElement() {
325N/A return (flag&FLAG_IS_ELEMENT)!=0;
325N/A }
325N/A
325N/A /**
325N/A * Returns true if the bean is immutable.
325N/A *
325N/A * <p>
325N/A * If this is true, Binder won't try to ueuse this object, and the unmarshaller
325N/A * won't create a new instance of it before it starts.
325N/A */
325N/A public final boolean isImmutable() {
325N/A return (flag&FLAG_IS_IMMUTABLE)!=0;
325N/A }
325N/A
325N/A /**
325N/A * True if this bean has an element-only content model.
325N/A * <p>
325N/A * If this flag is true, the unmarshaller can work
325N/A * faster by ignoring whitespaces more efficiently.
325N/A */
325N/A public final boolean hasElementOnlyContentModel() {
325N/A return (flag&FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL)!=0;
325N/A }
325N/A
325N/A /**
325N/A * True if this bean has an element-only content model.
325N/A * <p>
325N/A * Should be considered immutable, though I can't mark it final
325N/A * because it cannot be computed in this constructor.
325N/A */
325N/A protected final void hasElementOnlyContentModel(boolean value) {
325N/A if(value)
325N/A flag |= FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL;
325N/A else
325N/A flag &= ~FLAG_HAS_ELEMENT_ONLY_CONTENTMODEL;
325N/A }
325N/A
325N/A public boolean isNilIncluded() {
325N/A return isNilIncluded;
325N/A }
325N/A
325N/A /**
325N/A * This method is used to determine which of the sub-classes should be
325N/A * interrogated for the existence of lifecycle methods.
325N/A *
325N/A * @return true if the un|marshaller should look for lifecycle methods
325N/A * on this beanInfo, false otherwise.
325N/A */
325N/A public boolean lookForLifecycleMethods() {
325N/A return (flag&FLAG_HAS_LIFECYCLE_EVENTS)!=0;
325N/A }
325N/A
325N/A /**
325N/A * Returns the namespace URI portion of the element name,
325N/A * if the bean that this class represents is mapped from/to
325N/A * an XML element.
325N/A *
325N/A * @throws UnsupportedOperationException
325N/A * if {@link #isElement} is false.
325N/A */
325N/A public abstract String getElementNamespaceURI(BeanT o);
325N/A
325N/A /**
325N/A * Returns the local name portion of the element name,
325N/A * if the bean that this class represents is mapped from/to
325N/A * an XML element.
325N/A *
325N/A * @throws UnsupportedOperationException
325N/A * if {@link #isElement} is false.
325N/A */
325N/A public abstract String getElementLocalName(BeanT o);
325N/A
325N/A /**
325N/A * Type names associated with this {@link JaxBeanInfo}.
325N/A *
325N/A * @see #getTypeNames()
325N/A */
325N/A private final Object typeName; // either null, QName, or QName[]. save memory since most of them have just one.
325N/A
325N/A /**
325N/A * Returns XML Schema type names if the bean is mapped from
325N/A * a complex/simple type of XML Schema.
325N/A *
325N/A * <p>
325N/A * This is an ugly necessity to correctly handle
325N/A * the type substitution semantics of XML Schema.
325N/A *
325N/A * <p>
325N/A * A single Java class maybe mapped to more than one
325N/A * XML types. All the types listed here are recognized
325N/A * when we are unmarshalling XML.
325N/A *
325N/A * <p>
325N/A * null if the class is not bound to a named schema type.
325N/A *
325N/A * <p>
325N/A */
325N/A public Collection<QName> getTypeNames() {
325N/A if(typeName==null) return Collections.emptyList();
325N/A if(typeName instanceof QName) return Collections.singletonList((QName)typeName);
325N/A return Arrays.asList((QName[])typeName);
325N/A }
325N/A
325N/A /**
325N/A * Returns the XML type name to be used to marshal the specified instance.
325N/A *
325N/A * <P>
325N/A * Most of the times the type can be determined regardless of the actual
325N/A * instance, but there's a few exceptions (most notably {@link XMLGregorianCalendar}),
325N/A * so as a general rule we need an instance to determine it.
325N/A */
325N/A public QName getTypeName(@NotNull BeanT instance) {
325N/A if(typeName==null) return null;
325N/A if(typeName instanceof QName) return (QName)typeName;
325N/A return ((QName[])typeName)[0];
325N/A }
325N/A
325N/A /**
325N/A * Creates a new instance of the bean.
325N/A *
325N/A * <p>
325N/A * This operation is only supported when {@link #isImmutable} is false.
325N/A *
325N/A * @param context
325N/A * Sometimes the created bean remembers the corresponding source location,
325N/A */
325N/A public abstract BeanT createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException, SAXException;
325N/A
325N/A /**
325N/A * Resets the object to the initial state, as if the object
325N/A * is created fresh.
325N/A *
325N/A * <p>
325N/A * This is used to reuse an existing object for unmarshalling.
325N/A *
325N/A * @param context
325N/A * used for reporting any errors.
325N/A *
325N/A * @return
325N/A * true if the object was successfuly resetted.
325N/A * False if the object is not resettable, in which case the object will be
325N/A * discarded and new one will be created.
325N/A * <p>
325N/A * If the object is resettable but failed by an error, it should be reported to the context,
325N/A * then return false. If the object is not resettable to begin with, do not report an error.
325N/A *
325N/A * @throws SAXException
325N/A * as a result of reporting an error, the context may throw a {@link SAXException}.
325N/A */
325N/A public abstract boolean reset( BeanT o, UnmarshallingContext context ) throws SAXException;
325N/A
325N/A /**
325N/A * Gets the ID value of the given bean, if it has an ID value.
325N/A * Otherwise return null.
325N/A */
325N/A public abstract String getId(BeanT o, XMLSerializer target) throws SAXException;
325N/A
325N/A /**
325N/A * Serializes child elements and texts into the specified target.
325N/A */
325N/A public abstract void serializeBody( BeanT o, XMLSerializer target ) throws SAXException, IOException, XMLStreamException;
325N/A
325N/A /**
325N/A * Serializes attributes into the specified target.
325N/A */
325N/A public abstract void serializeAttributes( BeanT o, XMLSerializer target ) throws SAXException, IOException, XMLStreamException;
325N/A
325N/A /**
325N/A * Serializes the bean as the root element.
325N/A *
325N/A * <p>
325N/A * In the java-to-schema binding, an object might marshal in two different
325N/A * ways depending on whether it is used as the root of the graph or not.
325N/A * In the former case, an object could marshal as an element, whereas
325N/A * in the latter case, it marshals as a type.
325N/A *
325N/A * <p>
325N/A * This method is used to marshal the root of the object graph to allow
325N/A * this semantics to be implemented.
325N/A *
325N/A * <p>
325N/A * It is doubtful to me if it's a good idea for an object to marshal
325N/A * in two ways depending on the context.
325N/A *
325N/A * <p>
325N/A * For schema-to-java, this is equivalent to {@link #serializeBody(Object, XMLSerializer)}.
325N/A */
325N/A public abstract void serializeRoot( BeanT o, XMLSerializer target ) throws SAXException, IOException, XMLStreamException;
325N/A
325N/A /**
325N/A * Declares all the namespace URIs this object is using at
325N/A * its top-level scope into the specified target.
325N/A */
325N/A public abstract void serializeURIs( BeanT o, XMLSerializer target ) throws SAXException;
325N/A
325N/A /**
325N/A * Gets the {@link Loader} that will unmarshall the given object.
325N/A *
325N/A * @param context
325N/A * The {@link JAXBContextImpl} object that governs this object.
325N/A * This object is taken as a parameter so that {@link JaxBeanInfo} doesn't have
325N/A * to store them on its own.
325N/A *
325N/A * When this method is invoked from within the unmarshaller, tihs parameter can be
325N/A * null (because the loader is constructed already.)
325N/A *
325N/A * @param typeSubstitutionCapable
325N/A * If true, the returned {@link Loader} is capable of recognizing @xsi:type (if necessary)
325N/A * and unmarshals a subtype. This allowes an optimization where this bean info
325N/A * is guaranteed not to have a type substitution.
325N/A * If false, the returned {@link Loader} doesn't look for @xsi:type.
325N/A * @return
325N/A * must return non-null valid object
325N/A */
325N/A public abstract Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable);
325N/A
325N/A /**
325N/A * If the bean's representation in XML is just a text,
325N/A * this method return a {@link Transducer} that lets you convert
325N/A * values between the text and the bean.
325N/A */
325N/A public abstract Transducer<BeanT> getTransducer();
325N/A
325N/A
325N/A /**
325N/A * Called after all the {@link JaxBeanInfo}s are created.
325N/A * @param grammar
325N/A */
325N/A protected void link(JAXBContextImpl grammar) {
325N/A }
325N/A
325N/A /**
325N/A * Called at the end of the {@link JAXBContext} initialization phase
325N/A * to clean up any unnecessary references.
325N/A */
325N/A public void wrapUp() {}
325N/A
325N/A
325N/A private static final Class[] unmarshalEventParams = { Unmarshaller.class, Object.class };
325N/A private static Class[] marshalEventParams = { Marshaller.class };
325N/A
325N/A /**
325N/A * use reflection to determine which of the 4 object lifecycle methods exist on
325N/A * the JAXB bound type.
325N/A */
325N/A protected final void setLifecycleFlags() {
325N/A try {
325N/A Class<BeanT> jt = jaxbType;
325N/A
325N/A if (lcm == null) {
325N/A lcm = new LifecycleMethods();
325N/A }
325N/A
325N/A while (jt != null) {
325N/A for (Method m : jt.getDeclaredMethods()) {
325N/A String name = m.getName();
325N/A
325N/A if (lcm.beforeUnmarshal == null) {
325N/A if (name.equals("beforeUnmarshal")) {
325N/A if (match(m, unmarshalEventParams)) {
325N/A cacheLifecycleMethod(m, FLAG_HAS_BEFORE_UNMARSHAL_METHOD);
325N/A }
325N/A }
325N/A }
325N/A
325N/A if (lcm.afterUnmarshal == null) {
325N/A if (name.equals("afterUnmarshal")) {
325N/A if (match(m, unmarshalEventParams)) {
325N/A cacheLifecycleMethod(m, FLAG_HAS_AFTER_UNMARSHAL_METHOD);
325N/A }
325N/A }
325N/A }
325N/A
325N/A if (lcm.beforeMarshal == null) {
325N/A if (name.equals("beforeMarshal")) {
325N/A if (match(m, marshalEventParams)) {
325N/A cacheLifecycleMethod(m, FLAG_HAS_BEFORE_MARSHAL_METHOD);
325N/A }
325N/A }
325N/A }
325N/A
325N/A if (lcm.afterMarshal == null) {
325N/A if (name.equals("afterMarshal")) {
325N/A if (match(m, marshalEventParams)) {
325N/A cacheLifecycleMethod(m, FLAG_HAS_AFTER_MARSHAL_METHOD);
325N/A }
325N/A }
325N/A }
325N/A }
325N/A jt = (Class<BeanT>) jt.getSuperclass();
325N/A }
325N/A } catch (SecurityException e) {
325N/A // this happens when we don't have enough permission.
325N/A logger.log(Level.WARNING, Messages.UNABLE_TO_DISCOVER_EVENTHANDLER.format(
325N/A jaxbType.getName(), e));
325N/A }
325N/A }
325N/A
325N/A private boolean match(Method m, Class[] params) {
325N/A return Arrays.equals(m.getParameterTypes(),params);
325N/A }
325N/A
325N/A /**
325N/A * Cache a reference to the specified lifecycle method for the jaxbType
325N/A * associated with this beanInfo.
325N/A *
325N/A * @param m Method reference
325N/A * @param lifecycleFlag byte representing which of the 4 lifecycle methods
325N/A * is being cached
325N/A */
325N/A private void cacheLifecycleMethod(Method m, short lifecycleFlag) {
325N/A //LifecycleMethods lcm = getLifecycleMethods();
325N/A if(lcm==null) {
325N/A lcm = new LifecycleMethods();
325N/A //lcmCache.put(jaxbType, lcm);
325N/A }
325N/A
325N/A m.setAccessible(true);
325N/A
325N/A flag |= lifecycleFlag;
325N/A
325N/A switch (lifecycleFlag) {
325N/A case FLAG_HAS_BEFORE_UNMARSHAL_METHOD:
325N/A lcm.beforeUnmarshal = m;
325N/A break;
325N/A case FLAG_HAS_AFTER_UNMARSHAL_METHOD:
325N/A lcm.afterUnmarshal = m;
325N/A break;
325N/A case FLAG_HAS_BEFORE_MARSHAL_METHOD:
325N/A lcm.beforeMarshal = m;
325N/A break;
325N/A case FLAG_HAS_AFTER_MARSHAL_METHOD:
325N/A lcm.afterMarshal = m;
325N/A break;
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Return the LifecycleMethods cache for this ClassBeanInfo's corresponding
325N/A * jaxbType if it exists, else return null.
325N/A *
325N/A */
325N/A public final LifecycleMethods getLifecycleMethods() {
325N/A return lcm;
325N/A }
325N/A
325N/A /**
325N/A * Invokes the beforeUnmarshal method if applicable.
325N/A */
325N/A public final void invokeBeforeUnmarshalMethod(UnmarshallerImpl unm, Object child, Object parent) throws SAXException {
325N/A Method m = getLifecycleMethods().beforeUnmarshal;
325N/A invokeUnmarshallCallback(m, child, unm, parent);
325N/A }
325N/A
325N/A /**
325N/A * Invokes the afterUnmarshal method if applicable.
325N/A */
325N/A public final void invokeAfterUnmarshalMethod(UnmarshallerImpl unm, Object child, Object parent) throws SAXException {
325N/A Method m = getLifecycleMethods().afterUnmarshal;
325N/A invokeUnmarshallCallback(m, child, unm, parent);
325N/A }
325N/A
325N/A private void invokeUnmarshallCallback(Method m, Object child, UnmarshallerImpl unm, Object parent) throws SAXException {
325N/A try {
325N/A m.invoke(child,unm,parent);
325N/A } catch (IllegalAccessException e) {
325N/A UnmarshallingContext.getInstance().handleError(e, false);
325N/A } catch (InvocationTargetException e) {
325N/A UnmarshallingContext.getInstance().handleError(e, false);
325N/A }
325N/A }
325N/A
325N/A private static final Logger logger = Util.getClassLogger();
325N/A}