org.glassfish.jaxb.runtime.v2.model.impl.RuntimeClassInfoImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaxb-impl Show documentation
Show all versions of jaxb-impl Show documentation
Old JAXB Runtime module. Contains sources required for runtime processing.
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.glassfish.jaxb.runtime.v2.model.impl;
import com.sun.istack.NotNull;
import org.glassfish.jaxb.core.annotation.XmlLocation;
import org.glassfish.jaxb.runtime.AccessorFactory;
import org.glassfish.jaxb.runtime.AccessorFactoryImpl;
import org.glassfish.jaxb.runtime.InternalAccessorFactory;
import org.glassfish.jaxb.runtime.XmlAccessorFactory;
import org.glassfish.jaxb.runtime.api.AccessorException;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.Name;
import org.glassfish.jaxb.runtime.v2.runtime.Transducer;
import org.glassfish.jaxb.runtime.v2.runtime.XMLSerializer;
import org.glassfish.jaxb.core.v2.ClassFactory;
import org.glassfish.jaxb.core.v2.model.annotation.Locatable;
import org.glassfish.jaxb.core.v2.model.core.PropertyKind;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimeClassInfo;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimeElement;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimePropertyInfo;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimeValuePropertyInfo;
import org.glassfish.jaxb.core.v2.runtime.IllegalAnnotationException;
import org.glassfish.jaxb.core.v2.runtime.Location;
import org.glassfish.jaxb.runtime.v2.runtime.reflect.Accessor;
import org.glassfish.jaxb.runtime.v2.runtime.reflect.TransducedAccessor;
import org.glassfish.jaxb.runtime.v2.runtime.unmarshaller.UnmarshallingContext;
import jakarta.xml.bind.JAXBException;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
/**
* @author Kohsuke Kawaguchi ([email protected])
*/
class RuntimeClassInfoImpl extends ClassInfoImpl
implements RuntimeClassInfo, RuntimeElement {
/**
* If this class has a property annotated with {@link XmlLocation},
* this field will get the accessor for it.
*
* TODO: support method based XmlLocation
*/
private Accessor,Locator> xmlLocationAccessor;
private AccessorFactory accessorFactory;
private boolean supressAccessorWarnings = false;
public RuntimeClassInfoImpl(RuntimeModelBuilder modelBuilder, Locatable upstream, Class clazz) {
super(modelBuilder, upstream, clazz);
accessorFactory = createAccessorFactory(clazz);
}
protected AccessorFactory createAccessorFactory(Class clazz) {
XmlAccessorFactory factoryAnn;
AccessorFactory accFactory = null;
// user providing class to be used.
JAXBContextImpl context = ((RuntimeModelBuilder) builder).context;
if (context!=null) {
this.supressAccessorWarnings = context.supressAccessorWarnings;
if (context.xmlAccessorFactorySupport) {
factoryAnn = findXmlAccessorFactoryAnnotation(clazz);
if (factoryAnn != null) {
try {
accFactory = factoryAnn.value().newInstance();
} catch (InstantiationException e) {
builder.reportError(new IllegalAnnotationException(
Messages.ACCESSORFACTORY_INSTANTIATION_EXCEPTION.format(
factoryAnn.getClass().getName(), nav().getClassName(clazz)), this));
} catch (IllegalAccessException e) {
builder.reportError(new IllegalAnnotationException(
Messages.ACCESSORFACTORY_ACCESS_EXCEPTION.format(
factoryAnn.getClass().getName(), nav().getClassName(clazz)),this));
}
}
}
}
// Fall back to local AccessorFactory when no
// user not providing one or as error recovery.
if (accFactory == null){
accFactory = AccessorFactoryImpl.getInstance();
}
return accFactory;
}
protected XmlAccessorFactory findXmlAccessorFactoryAnnotation(Class clazz) {
XmlAccessorFactory factoryAnn = reader().getClassAnnotation(XmlAccessorFactory.class,clazz,this);
if (factoryAnn == null) {
factoryAnn = reader().getPackageAnnotation(XmlAccessorFactory.class,clazz,this);
}
return factoryAnn;
}
public Method getFactoryMethod(){
return super.getFactoryMethod();
}
public final RuntimeClassInfoImpl getBaseClass() {
return (RuntimeClassInfoImpl)super.getBaseClass();
}
@Override
protected ReferencePropertyInfoImpl createReferenceProperty(PropertySeed seed) {
return new RuntimeReferencePropertyInfoImpl(this,seed);
}
@Override
protected AttributePropertyInfoImpl createAttributeProperty(PropertySeed seed) {
return new RuntimeAttributePropertyInfoImpl(this,seed);
}
@Override
protected ValuePropertyInfoImpl createValueProperty(PropertySeed seed) {
return new RuntimeValuePropertyInfoImpl(this,seed);
}
@Override
protected ElementPropertyInfoImpl createElementProperty(PropertySeed seed) {
return new RuntimeElementPropertyInfoImpl(this,seed);
}
@Override
protected MapPropertyInfoImpl createMapProperty(PropertySeed seed) {
return new RuntimeMapPropertyInfoImpl(this,seed);
}
@Override
public List extends RuntimePropertyInfo> getProperties() {
return (List extends RuntimePropertyInfo>)super.getProperties();
}
@Override
public RuntimePropertyInfo getProperty(String name) {
return (RuntimePropertyInfo)super.getProperty(name);
}
public void link() {
getTransducer(); // populate the transducer
super.link();
}
private Accessor,Map> attributeWildcardAccessor;
public Accessor> getAttributeWildcard() {
for( RuntimeClassInfoImpl c=this; c!=null; c=c.getBaseClass() ) {
if(c.attributeWildcard!=null) {
if(c.attributeWildcardAccessor==null)
c.attributeWildcardAccessor = c.createAttributeWildcardAccessor();
return (Accessor>)c.attributeWildcardAccessor;
}
}
return null;
}
private boolean computedTransducer = false;
private Transducer xducer = null;
public Transducer getTransducer() {
if(!computedTransducer) {
computedTransducer = true;
xducer = calcTransducer();
}
return xducer;
}
/**
* Creates a transducer if this class is bound to a text in XML.
*/
private Transducer calcTransducer() {
RuntimeValuePropertyInfo valuep=null;
if(hasAttributeWildcard())
return null; // has attribute wildcard. Can't be handled as a leaf
for (RuntimeClassInfoImpl ci = this; ci != null; ci = ci.getBaseClass()) {
for( RuntimePropertyInfo pi : ci.getProperties() )
if(pi.kind()==PropertyKind.VALUE) {
valuep = (RuntimeValuePropertyInfo)pi;
} else {
// this bean has something other than a value
return null;
}
}
if(valuep==null)
return null;
if( !valuep.getTarget().isSimpleType() )
return null; // if there's an error, recover from it by returning null.
return new TransducerImpl(getClazz(), TransducedAccessor.get(
((RuntimeModelBuilder)builder).context,valuep));
}
/**
* Creates
*/
private Accessor,Map> createAttributeWildcardAccessor() {
assert attributeWildcard!=null;
return ((RuntimePropertySeed)attributeWildcard).getAccessor();
}
@Override
protected RuntimePropertySeed createFieldSeed(Field field) {
final boolean readOnly = Modifier.isStatic(field.getModifiers());
Accessor acc;
try {
if (supressAccessorWarnings) {
acc = ((InternalAccessorFactory)accessorFactory).createFieldAccessor(clazz, field, readOnly, supressAccessorWarnings);
} else {
acc = accessorFactory.createFieldAccessor(clazz, field, readOnly);
}
} catch(JAXBException e) {
builder.reportError(new IllegalAnnotationException(
Messages.CUSTOM_ACCESSORFACTORY_FIELD_ERROR.format(
nav().getClassName(clazz), e.toString()), this ));
acc = Accessor.getErrorInstance(); // error recovery
}
return new RuntimePropertySeed(super.createFieldSeed(field), acc );
}
@Override
public RuntimePropertySeed createAccessorSeed(Method getter, Method setter) {
Accessor acc;
try {
acc = accessorFactory.createPropertyAccessor(clazz, getter, setter);
} catch(JAXBException e) {
builder.reportError(new IllegalAnnotationException(
Messages.CUSTOM_ACCESSORFACTORY_PROPERTY_ERROR.format(
nav().getClassName(clazz), e.toString()), this ));
acc = Accessor.getErrorInstance(); // error recovery
}
return new RuntimePropertySeed( super.createAccessorSeed(getter,setter),
acc );
}
@Override
protected void checkFieldXmlLocation(Field f) {
if(reader().hasFieldAnnotation(XmlLocation.class,f))
// TODO: check for XmlLocation signature
// TODO: check a collision with the super class
xmlLocationAccessor = new Accessor.FieldReflection