src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/RuntimeClassInfoImpl.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/RuntimeClassInfoImpl.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,403 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.model.impl;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.lang.annotation.Annotation;
    1.33 +import java.lang.reflect.Field;
    1.34 +import java.lang.reflect.Method;
    1.35 +import java.lang.reflect.Modifier;
    1.36 +import java.lang.reflect.Type;
    1.37 +import java.util.List;
    1.38 +import java.util.Map;
    1.39 +
    1.40 +import javax.xml.bind.JAXBException;
    1.41 +import javax.xml.namespace.QName;
    1.42 +import javax.xml.stream.XMLStreamException;
    1.43 +
    1.44 +import com.sun.istack.internal.NotNull;
    1.45 +import com.sun.xml.internal.bind.AccessorFactory;
    1.46 +import com.sun.xml.internal.bind.AccessorFactoryImpl;
    1.47 +import com.sun.xml.internal.bind.InternalAccessorFactory;
    1.48 +import com.sun.xml.internal.bind.XmlAccessorFactory;
    1.49 +import com.sun.xml.internal.bind.annotation.XmlLocation;
    1.50 +import com.sun.xml.internal.bind.api.AccessorException;
    1.51 +import com.sun.xml.internal.bind.v2.ClassFactory;
    1.52 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
    1.53 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
    1.54 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeClassInfo;
    1.55 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElement;
    1.56 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
    1.57 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeValuePropertyInfo;
    1.58 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
    1.59 +import com.sun.xml.internal.bind.v2.runtime.Location;
    1.60 +import com.sun.xml.internal.bind.v2.runtime.Name;
    1.61 +import com.sun.xml.internal.bind.v2.runtime.Transducer;
    1.62 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.63 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
    1.64 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
    1.65 +import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
    1.66 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
    1.67 +
    1.68 +import org.xml.sax.Locator;
    1.69 +import org.xml.sax.SAXException;
    1.70 +
    1.71 +/**
    1.72 + * @author Kohsuke Kawaguchi (kk@kohsuke.org)
    1.73 + */
    1.74 +class RuntimeClassInfoImpl extends ClassInfoImpl<Type,Class,Field,Method>
    1.75 +        implements RuntimeClassInfo, RuntimeElement {
    1.76 +
    1.77 +    /**
    1.78 +     * If this class has a property annotated with {@link XmlLocation},
    1.79 +     * this field will get the accessor for it.
    1.80 +     *
    1.81 +     * TODO: support method based XmlLocation
    1.82 +     */
    1.83 +    private Accessor<?,Locator> xmlLocationAccessor;
    1.84 +
    1.85 +    private AccessorFactory accessorFactory;
    1.86 +
    1.87 +    private boolean supressAccessorWarnings = false;
    1.88 +
    1.89 +    public RuntimeClassInfoImpl(RuntimeModelBuilder modelBuilder, Locatable upstream, Class clazz) {
    1.90 +        super(modelBuilder, upstream, clazz);
    1.91 +        accessorFactory = createAccessorFactory(clazz);
    1.92 +    }
    1.93 +
    1.94 +    protected AccessorFactory createAccessorFactory(Class clazz) {
    1.95 +        XmlAccessorFactory factoryAnn;
    1.96 +        AccessorFactory accFactory = null;
    1.97 +
    1.98 +        // user providing class to be used.
    1.99 +        JAXBContextImpl context = ((RuntimeModelBuilder) builder).context;
   1.100 +        if (context!=null) {
   1.101 +            this.supressAccessorWarnings = context.supressAccessorWarnings;
   1.102 +            if (context.xmlAccessorFactorySupport) {
   1.103 +                factoryAnn = findXmlAccessorFactoryAnnotation(clazz);
   1.104 +                if (factoryAnn != null) {
   1.105 +                    try {
   1.106 +                        accFactory = factoryAnn.value().newInstance();
   1.107 +                    } catch (InstantiationException e) {
   1.108 +                        builder.reportError(new IllegalAnnotationException(
   1.109 +                                Messages.ACCESSORFACTORY_INSTANTIATION_EXCEPTION.format(
   1.110 +                                factoryAnn.getClass().getName(), nav().getClassName(clazz)), this));
   1.111 +                    } catch (IllegalAccessException e) {
   1.112 +                        builder.reportError(new IllegalAnnotationException(
   1.113 +                                Messages.ACCESSORFACTORY_ACCESS_EXCEPTION.format(
   1.114 +                                factoryAnn.getClass().getName(), nav().getClassName(clazz)),this));
   1.115 +                    }
   1.116 +                }
   1.117 +            }
   1.118 +        }
   1.119 +
   1.120 +
   1.121 +        // Fall back to local AccessorFactory when no
   1.122 +        // user not providing one or as error recovery.
   1.123 +        if (accFactory == null){
   1.124 +            accFactory = AccessorFactoryImpl.getInstance();
   1.125 +        }
   1.126 +        return accFactory;
   1.127 +    }
   1.128 +
   1.129 +    protected XmlAccessorFactory findXmlAccessorFactoryAnnotation(Class clazz) {
   1.130 +        XmlAccessorFactory factoryAnn = reader().getClassAnnotation(XmlAccessorFactory.class,clazz,this);
   1.131 +        if (factoryAnn == null) {
   1.132 +            factoryAnn = reader().getPackageAnnotation(XmlAccessorFactory.class,clazz,this);
   1.133 +        }
   1.134 +        return factoryAnn;
   1.135 +    }
   1.136 +
   1.137 +
   1.138 +    public Method getFactoryMethod(){
   1.139 +        return super.getFactoryMethod();
   1.140 +    }
   1.141 +
   1.142 +    public final RuntimeClassInfoImpl getBaseClass() {
   1.143 +        return (RuntimeClassInfoImpl)super.getBaseClass();
   1.144 +    }
   1.145 +
   1.146 +    @Override
   1.147 +    protected ReferencePropertyInfoImpl createReferenceProperty(PropertySeed<Type,Class,Field,Method> seed) {
   1.148 +        return new RuntimeReferencePropertyInfoImpl(this,seed);
   1.149 +    }
   1.150 +
   1.151 +    @Override
   1.152 +    protected AttributePropertyInfoImpl createAttributeProperty(PropertySeed<Type,Class,Field,Method> seed) {
   1.153 +        return new RuntimeAttributePropertyInfoImpl(this,seed);
   1.154 +    }
   1.155 +
   1.156 +    @Override
   1.157 +    protected ValuePropertyInfoImpl createValueProperty(PropertySeed<Type,Class,Field,Method> seed) {
   1.158 +        return new RuntimeValuePropertyInfoImpl(this,seed);
   1.159 +    }
   1.160 +
   1.161 +    @Override
   1.162 +    protected ElementPropertyInfoImpl createElementProperty(PropertySeed<Type,Class,Field,Method> seed) {
   1.163 +        return new RuntimeElementPropertyInfoImpl(this,seed);
   1.164 +    }
   1.165 +
   1.166 +    @Override
   1.167 +    protected MapPropertyInfoImpl createMapProperty(PropertySeed<Type,Class,Field,Method> seed) {
   1.168 +        return new RuntimeMapPropertyInfoImpl(this,seed);
   1.169 +    }
   1.170 +
   1.171 +
   1.172 +    @Override
   1.173 +    public List<? extends RuntimePropertyInfo> getProperties() {
   1.174 +        return (List<? extends RuntimePropertyInfo>)super.getProperties();
   1.175 +    }
   1.176 +
   1.177 +    @Override
   1.178 +    public RuntimePropertyInfo getProperty(String name) {
   1.179 +        return (RuntimePropertyInfo)super.getProperty(name);
   1.180 +    }
   1.181 +
   1.182 +
   1.183 +    public void link() {
   1.184 +        getTransducer();    // populate the transducer
   1.185 +        super.link();
   1.186 +    }
   1.187 +
   1.188 +    private Accessor<?,Map<QName,String>> attributeWildcardAccessor;
   1.189 +
   1.190 +    public <B> Accessor<B,Map<QName,String>> getAttributeWildcard() {
   1.191 +        for( RuntimeClassInfoImpl c=this; c!=null; c=c.getBaseClass() ) {
   1.192 +            if(c.attributeWildcard!=null) {
   1.193 +                if(c.attributeWildcardAccessor==null)
   1.194 +                    c.attributeWildcardAccessor = c.createAttributeWildcardAccessor();
   1.195 +                return (Accessor<B,Map<QName,String>>)c.attributeWildcardAccessor;
   1.196 +            }
   1.197 +        }
   1.198 +        return null;
   1.199 +    }
   1.200 +
   1.201 +    private boolean computedTransducer = false;
   1.202 +    private Transducer xducer = null;
   1.203 +
   1.204 +    public Transducer getTransducer() {
   1.205 +        if(!computedTransducer) {
   1.206 +            computedTransducer = true;
   1.207 +            xducer = calcTransducer();
   1.208 +        }
   1.209 +        return xducer;
   1.210 +    }
   1.211 +
   1.212 +    /**
   1.213 +     * Creates a transducer if this class is bound to a text in XML.
   1.214 +     */
   1.215 +    private Transducer calcTransducer() {
   1.216 +        RuntimeValuePropertyInfo valuep=null;
   1.217 +        if(hasAttributeWildcard())
   1.218 +            return null;        // has attribute wildcard. Can't be handled as a leaf
   1.219 +        for (RuntimeClassInfoImpl ci = this; ci != null; ci = ci.getBaseClass()) {
   1.220 +            for( RuntimePropertyInfo pi : ci.getProperties() )
   1.221 +                if(pi.kind()==PropertyKind.VALUE) {
   1.222 +                    valuep = (RuntimeValuePropertyInfo)pi;
   1.223 +                } else {
   1.224 +                    // this bean has something other than a value
   1.225 +                    return null;
   1.226 +                }
   1.227 +        }
   1.228 +        if(valuep==null)
   1.229 +            return null;
   1.230 +        if( !valuep.getTarget().isSimpleType() )
   1.231 +            return null;    // if there's an error, recover from it by returning null.
   1.232 +
   1.233 +        return new TransducerImpl(getClazz(),TransducedAccessor.get(
   1.234 +                ((RuntimeModelBuilder)builder).context,valuep));
   1.235 +    }
   1.236 +
   1.237 +    /**
   1.238 +     * Creates
   1.239 +     */
   1.240 +    private Accessor<?,Map<QName,String>> createAttributeWildcardAccessor() {
   1.241 +        assert attributeWildcard!=null;
   1.242 +        return ((RuntimePropertySeed)attributeWildcard).getAccessor();
   1.243 +    }
   1.244 +
   1.245 +    @Override
   1.246 +    protected RuntimePropertySeed createFieldSeed(Field field) {
   1.247 +       final boolean readOnly = Modifier.isStatic(field.getModifiers());
   1.248 +        Accessor acc;
   1.249 +        try {
   1.250 +            if (supressAccessorWarnings) {
   1.251 +                acc = ((InternalAccessorFactory)accessorFactory).createFieldAccessor(clazz, field, readOnly, supressAccessorWarnings);
   1.252 +            } else {
   1.253 +                acc = accessorFactory.createFieldAccessor(clazz, field, readOnly);
   1.254 +            }
   1.255 +        } catch(JAXBException e) {
   1.256 +            builder.reportError(new IllegalAnnotationException(
   1.257 +                    Messages.CUSTOM_ACCESSORFACTORY_FIELD_ERROR.format(
   1.258 +                    nav().getClassName(clazz), e.toString()), this ));
   1.259 +            acc = Accessor.getErrorInstance(); // error recovery
   1.260 +        }
   1.261 +        return new RuntimePropertySeed(super.createFieldSeed(field), acc );
   1.262 +    }
   1.263 +
   1.264 +    @Override
   1.265 +    public RuntimePropertySeed createAccessorSeed(Method getter, Method setter) {
   1.266 +        Accessor acc;
   1.267 +        try {
   1.268 +            acc = accessorFactory.createPropertyAccessor(clazz, getter, setter);
   1.269 +        } catch(JAXBException e) {
   1.270 +            builder.reportError(new IllegalAnnotationException(
   1.271 +                Messages.CUSTOM_ACCESSORFACTORY_PROPERTY_ERROR.format(
   1.272 +                nav().getClassName(clazz), e.toString()), this ));
   1.273 +            acc = Accessor.getErrorInstance(); // error recovery
   1.274 +        }
   1.275 +        return new RuntimePropertySeed( super.createAccessorSeed(getter,setter),
   1.276 +          acc );
   1.277 +    }
   1.278 +
   1.279 +    @Override
   1.280 +    protected void checkFieldXmlLocation(Field f) {
   1.281 +        if(reader().hasFieldAnnotation(XmlLocation.class,f))
   1.282 +            // TODO: check for XmlLocation signature
   1.283 +            // TODO: check a collision with the super class
   1.284 +            xmlLocationAccessor = new Accessor.FieldReflection<Object,Locator>(f);
   1.285 +    }
   1.286 +
   1.287 +    public Accessor<?,Locator> getLocatorField() {
   1.288 +        return xmlLocationAccessor;
   1.289 +    }
   1.290 +
   1.291 +    static final class RuntimePropertySeed implements PropertySeed<Type,Class,Field,Method> {
   1.292 +        /**
   1.293 +         * @see #getAccessor()
   1.294 +         */
   1.295 +        private final Accessor acc;
   1.296 +
   1.297 +        private final PropertySeed<Type,Class,Field,Method> core;
   1.298 +
   1.299 +        public RuntimePropertySeed(PropertySeed<Type,Class,Field,Method> core, Accessor acc) {
   1.300 +            this.core = core;
   1.301 +            this.acc = acc;
   1.302 +        }
   1.303 +
   1.304 +        public String getName() {
   1.305 +            return core.getName();
   1.306 +        }
   1.307 +
   1.308 +        public <A extends Annotation> A readAnnotation(Class<A> annotationType) {
   1.309 +            return core.readAnnotation(annotationType);
   1.310 +        }
   1.311 +
   1.312 +        public boolean hasAnnotation(Class<? extends Annotation> annotationType) {
   1.313 +            return core.hasAnnotation(annotationType);
   1.314 +        }
   1.315 +
   1.316 +        public Type getRawType() {
   1.317 +            return core.getRawType();
   1.318 +        }
   1.319 +
   1.320 +        public Location getLocation() {
   1.321 +            return core.getLocation();
   1.322 +        }
   1.323 +
   1.324 +        public Locatable getUpstream() {
   1.325 +            return core.getUpstream();
   1.326 +        }
   1.327 +
   1.328 +        public Accessor getAccessor() {
   1.329 +            return acc;
   1.330 +        }
   1.331 +    }
   1.332 +
   1.333 +
   1.334 +
   1.335 +    /**
   1.336 +     * {@link Transducer} implementation used when this class maps to PCDATA in XML.
   1.337 +     *
   1.338 +     * TODO: revisit the exception handling
   1.339 +     */
   1.340 +    private static final class TransducerImpl<BeanT> implements Transducer<BeanT> {
   1.341 +        private final TransducedAccessor<BeanT> xacc;
   1.342 +        private final Class<BeanT> ownerClass;
   1.343 +
   1.344 +        public TransducerImpl(Class<BeanT> ownerClass,TransducedAccessor<BeanT> xacc) {
   1.345 +            this.xacc = xacc;
   1.346 +            this.ownerClass = ownerClass;
   1.347 +        }
   1.348 +
   1.349 +        public boolean useNamespace() {
   1.350 +            return xacc.useNamespace();
   1.351 +        }
   1.352 +
   1.353 +        public boolean isDefault() {
   1.354 +            return false;
   1.355 +        }
   1.356 +
   1.357 +        public void declareNamespace(BeanT bean, XMLSerializer w) throws AccessorException {
   1.358 +            try {
   1.359 +                xacc.declareNamespace(bean,w);
   1.360 +            } catch (SAXException e) {
   1.361 +                throw new AccessorException(e);
   1.362 +            }
   1.363 +        }
   1.364 +
   1.365 +        public @NotNull CharSequence print(BeanT o) throws AccessorException {
   1.366 +            try {
   1.367 +                CharSequence value = xacc.print(o);
   1.368 +                if(value==null)
   1.369 +                    throw new AccessorException(Messages.THERE_MUST_BE_VALUE_IN_XMLVALUE.format(o));
   1.370 +                return value;
   1.371 +            } catch (SAXException e) {
   1.372 +                throw new AccessorException(e);
   1.373 +            }
   1.374 +        }
   1.375 +
   1.376 +        public BeanT parse(CharSequence lexical) throws AccessorException, SAXException {
   1.377 +            UnmarshallingContext ctxt = UnmarshallingContext.getInstance();
   1.378 +            BeanT inst;
   1.379 +            if(ctxt!=null)
   1.380 +                inst = (BeanT)ctxt.createInstance(ownerClass);
   1.381 +            else
   1.382 +                // when this runs for parsing enum constants,
   1.383 +                // there's no UnmarshallingContext.
   1.384 +                inst = ClassFactory.create(ownerClass);
   1.385 +
   1.386 +            xacc.parse(inst,lexical);
   1.387 +            return inst;
   1.388 +        }
   1.389 +
   1.390 +        public void writeText(XMLSerializer w, BeanT o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
   1.391 +            if(!xacc.hasValue(o))
   1.392 +                throw new AccessorException(Messages.THERE_MUST_BE_VALUE_IN_XMLVALUE.format(o));
   1.393 +            xacc.writeText(w,o,fieldName);
   1.394 +        }
   1.395 +
   1.396 +        public void writeLeafElement(XMLSerializer w, Name tagName, BeanT o, String fieldName) throws IOException, SAXException, XMLStreamException, AccessorException {
   1.397 +            if(!xacc.hasValue(o))
   1.398 +                throw new AccessorException(Messages.THERE_MUST_BE_VALUE_IN_XMLVALUE.format(o));
   1.399 +            xacc.writeLeafElement(w,tagName,o,fieldName);
   1.400 +        }
   1.401 +
   1.402 +        public QName getTypeName(BeanT instance) {
   1.403 +            return null;
   1.404 +        }
   1.405 +    }
   1.406 +}

mercurial