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/runtime/ClassBeanInfoImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,439 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, 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.runtime; 1.30 + 1.31 +import java.io.IOException; 1.32 +import java.lang.reflect.InvocationTargetException; 1.33 +import java.lang.reflect.Method; 1.34 +import java.lang.reflect.Modifier; 1.35 +import java.util.Collection; 1.36 +import java.util.Collections; 1.37 +import java.util.List; 1.38 +import java.util.Map; 1.39 +import java.util.logging.Level; 1.40 +import java.util.logging.Logger; 1.41 + 1.42 +import javax.xml.bind.ValidationEvent; 1.43 +import javax.xml.bind.annotation.XmlRootElement; 1.44 +import javax.xml.bind.helpers.ValidationEventImpl; 1.45 +import javax.xml.namespace.QName; 1.46 +import javax.xml.stream.XMLStreamException; 1.47 + 1.48 +import com.sun.istack.internal.FinalArrayList; 1.49 +import com.sun.xml.internal.bind.Util; 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.WellKnownNamespace; 1.53 +import com.sun.xml.internal.bind.v2.model.core.ID; 1.54 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeClassInfo; 1.55 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; 1.56 +import com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty; 1.57 +import com.sun.xml.internal.bind.v2.runtime.property.Property; 1.58 +import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory; 1.59 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 1.60 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 1.61 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader; 1.62 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; 1.63 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader; 1.64 + 1.65 +import org.xml.sax.Locator; 1.66 +import org.xml.sax.SAXException; 1.67 +import org.xml.sax.helpers.LocatorImpl; 1.68 + 1.69 +/** 1.70 + * {@link JaxBeanInfo} implementation for j2s bean. 1.71 + * 1.72 + * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 1.73 + */ 1.74 +public final class ClassBeanInfoImpl<BeanT> extends JaxBeanInfo<BeanT> implements AttributeAccessor<BeanT> { 1.75 + 1.76 + /** 1.77 + * Properties of this bean class but not its ancestor classes. 1.78 + */ 1.79 + public final Property<BeanT>[] properties; 1.80 + 1.81 + /** 1.82 + * Non-null if this bean has an ID property. 1.83 + */ 1.84 + private Property<? super BeanT> idProperty; 1.85 + 1.86 + /** 1.87 + * Immutable configured loader for this class. 1.88 + * 1.89 + * <p> 1.90 + * Set from the link method, but considered final. 1.91 + */ 1.92 + private Loader loader; 1.93 + private Loader loaderWithTypeSubst; 1.94 + 1.95 + /** 1.96 + * Set only until the link phase to avoid leaking memory. 1.97 + */ 1.98 + private RuntimeClassInfo ci; 1.99 + 1.100 + private final Accessor<? super BeanT,Map<QName,String>> inheritedAttWildcard; 1.101 + private final Transducer<BeanT> xducer; 1.102 + 1.103 + /** 1.104 + * {@link ClassBeanInfoImpl} that represents the super class of {@link #jaxbType}. 1.105 + */ 1.106 + public final ClassBeanInfoImpl<? super BeanT> superClazz; 1.107 + 1.108 + private final Accessor<? super BeanT,Locator> xmlLocatorField; 1.109 + 1.110 + private final Name tagName; 1.111 + 1.112 + private boolean retainPropertyInfo = false; 1.113 + 1.114 + /** 1.115 + * The {@link AttributeProperty}s for this type and all its ancestors. 1.116 + * If {@link JAXBContextImpl#c14nSupport} is true, this is sorted alphabetically. 1.117 + */ 1.118 + private /*final*/ AttributeProperty<BeanT>[] attributeProperties; 1.119 + 1.120 + /** 1.121 + * {@link Property}s that need to receive {@link Property#serializeURIs(Object, XMLSerializer)} callback. 1.122 + */ 1.123 + private /*final*/ Property<BeanT>[] uriProperties; 1.124 + 1.125 + private final Method factoryMethod; 1.126 + 1.127 + /*package*/ ClassBeanInfoImpl(JAXBContextImpl owner, RuntimeClassInfo ci) { 1.128 + super(owner,ci,ci.getClazz(),ci.getTypeName(),ci.isElement(),false,true); 1.129 + 1.130 + this.ci = ci; 1.131 + this.inheritedAttWildcard = ci.getAttributeWildcard(); 1.132 + this.xducer = ci.getTransducer(); 1.133 + this.factoryMethod = ci.getFactoryMethod(); 1.134 + this.retainPropertyInfo = owner.retainPropertyInfo; 1.135 + 1.136 + // make the factory accessible 1.137 + if(factoryMethod!=null) { 1.138 + int classMod = factoryMethod.getDeclaringClass().getModifiers(); 1.139 + 1.140 + if(!Modifier.isPublic(classMod) || !Modifier.isPublic(factoryMethod.getModifiers())) { 1.141 + // attempt to make it work even if the constructor is not accessible 1.142 + try { 1.143 + factoryMethod.setAccessible(true); 1.144 + } catch(SecurityException e) { 1.145 + // but if we don't have a permission to do so, work gracefully. 1.146 + logger.log(Level.FINE,"Unable to make the method of "+factoryMethod+" accessible",e); 1.147 + throw e; 1.148 + } 1.149 + } 1.150 + } 1.151 + 1.152 + 1.153 + if(ci.getBaseClass()==null) 1.154 + this.superClazz = null; 1.155 + else 1.156 + this.superClazz = owner.getOrCreate(ci.getBaseClass()); 1.157 + 1.158 + if(superClazz!=null && superClazz.xmlLocatorField!=null) 1.159 + xmlLocatorField = superClazz.xmlLocatorField; 1.160 + else 1.161 + xmlLocatorField = ci.getLocatorField(); 1.162 + 1.163 + // create property objects 1.164 + Collection<? extends RuntimePropertyInfo> ps = ci.getProperties(); 1.165 + this.properties = new Property[ps.size()]; 1.166 + int idx=0; 1.167 + boolean elementOnly = true; 1.168 + for( RuntimePropertyInfo info : ps ) { 1.169 + Property p = PropertyFactory.create(owner,info); 1.170 + if(info.id()==ID.ID) 1.171 + idProperty = p; 1.172 + properties[idx++] = p; 1.173 + elementOnly &= info.elementOnlyContent(); 1.174 + checkOverrideProperties(p); 1.175 + } 1.176 + // super class' idProperty might not be computed at this point, 1.177 + // so check that later 1.178 + 1.179 + hasElementOnlyContentModel( elementOnly ); 1.180 + // again update this value later when we know that of the super class 1.181 + 1.182 + if(ci.isElement()) 1.183 + tagName = owner.nameBuilder.createElementName(ci.getElementName()); 1.184 + else 1.185 + tagName = null; 1.186 + 1.187 + setLifecycleFlags(); 1.188 + } 1.189 + 1.190 + private void checkOverrideProperties(Property p) { 1.191 + ClassBeanInfoImpl bi = this; 1.192 + while ((bi = bi.superClazz) != null) { 1.193 + Property[] props = bi.properties; 1.194 + if (props == null) break; 1.195 + for (Property superProperty : props) { 1.196 + if (superProperty != null) { 1.197 + String spName = superProperty.getFieldName(); 1.198 + if ((spName != null) && (spName.equals(p.getFieldName()))) { 1.199 + superProperty.setHiddenByOverride(true); 1.200 + } 1.201 + } 1.202 + } 1.203 + } 1.204 + } 1.205 + 1.206 + @Override 1.207 + protected void link(JAXBContextImpl grammar) { 1.208 + if(uriProperties!=null) 1.209 + return; // avoid linking twice 1.210 + 1.211 + super.link(grammar); 1.212 + 1.213 + if(superClazz!=null) 1.214 + superClazz.link(grammar); 1.215 + 1.216 + getLoader(grammar,true); // make sure to build the loader if we haven't done so. 1.217 + 1.218 + // propagate values from super class 1.219 + if(superClazz!=null) { 1.220 + if(idProperty==null) 1.221 + idProperty = superClazz.idProperty; 1.222 + 1.223 + if(!superClazz.hasElementOnlyContentModel()) 1.224 + hasElementOnlyContentModel(false); 1.225 + } 1.226 + 1.227 + // create a list of attribute/URI handlers 1.228 + List<AttributeProperty> attProps = new FinalArrayList<AttributeProperty>(); 1.229 + List<Property> uriProps = new FinalArrayList<Property>(); 1.230 + for (ClassBeanInfoImpl bi = this; bi != null; bi = bi.superClazz) { 1.231 + for (int i = 0; i < bi.properties.length; i++) { 1.232 + Property p = bi.properties[i]; 1.233 + if(p instanceof AttributeProperty) 1.234 + attProps.add((AttributeProperty) p); 1.235 + if(p.hasSerializeURIAction()) 1.236 + uriProps.add(p); 1.237 + } 1.238 + } 1.239 + if(grammar.c14nSupport) 1.240 + Collections.sort(attProps); 1.241 + 1.242 + if(attProps.isEmpty()) 1.243 + attributeProperties = EMPTY_PROPERTIES; 1.244 + else 1.245 + attributeProperties = attProps.toArray(new AttributeProperty[attProps.size()]); 1.246 + 1.247 + if(uriProps.isEmpty()) 1.248 + uriProperties = EMPTY_PROPERTIES; 1.249 + else 1.250 + uriProperties = uriProps.toArray(new Property[uriProps.size()]); 1.251 + } 1.252 + 1.253 + @Override 1.254 + public void wrapUp() { 1.255 + for (Property p : properties) 1.256 + p.wrapUp(); 1.257 + ci = null; 1.258 + super.wrapUp(); 1.259 + } 1.260 + 1.261 + public String getElementNamespaceURI(BeanT bean) { 1.262 + return tagName.nsUri; 1.263 + } 1.264 + 1.265 + public String getElementLocalName(BeanT bean) { 1.266 + return tagName.localName; 1.267 + } 1.268 + 1.269 + public BeanT createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException, SAXException { 1.270 + 1.271 + BeanT bean = null; 1.272 + if (factoryMethod == null){ 1.273 + bean = ClassFactory.create0(jaxbType); 1.274 + }else { 1.275 + Object o = ClassFactory.create(factoryMethod); 1.276 + if( jaxbType.isInstance(o) ){ 1.277 + bean = (BeanT)o; 1.278 + } else { 1.279 + throw new InstantiationException("The factory method didn't return a correct object"); 1.280 + } 1.281 + } 1.282 + 1.283 + if(xmlLocatorField!=null) 1.284 + // need to copy because Locator is mutable 1.285 + try { 1.286 + xmlLocatorField.set(bean,new LocatorImpl(context.getLocator())); 1.287 + } catch (AccessorException e) { 1.288 + context.handleError(e); 1.289 + } 1.290 + return bean; 1.291 + } 1.292 + 1.293 + public boolean reset(BeanT bean, UnmarshallingContext context) throws SAXException { 1.294 + try { 1.295 + if(superClazz!=null) 1.296 + superClazz.reset(bean,context); 1.297 + for( Property<BeanT> p : properties ) 1.298 + p.reset(bean); 1.299 + return true; 1.300 + } catch (AccessorException e) { 1.301 + context.handleError(e); 1.302 + return false; 1.303 + } 1.304 + } 1.305 + 1.306 + public String getId(BeanT bean, XMLSerializer target) throws SAXException { 1.307 + if(idProperty!=null) { 1.308 + try { 1.309 + return idProperty.getIdValue(bean); 1.310 + } catch (AccessorException e) { 1.311 + target.reportError(null,e); 1.312 + } 1.313 + } 1.314 + return null; 1.315 + } 1.316 + 1.317 + public void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.318 + if(tagName==null) { 1.319 + Class beanClass = bean.getClass(); 1.320 + String message; 1.321 + if (beanClass.isAnnotationPresent(XmlRootElement.class)) { 1.322 + message = Messages.UNABLE_TO_MARSHAL_UNBOUND_CLASS.format(beanClass.getName()); 1.323 + } else { 1.324 + message = Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(beanClass.getName()); 1.325 + } 1.326 + target.reportError(new ValidationEventImpl(ValidationEvent.ERROR,message,null, null)); 1.327 + } else { 1.328 + target.startElement(tagName,bean); 1.329 + target.childAsSoleContent(bean,null); 1.330 + target.endElement(); 1.331 + if (retainPropertyInfo) { 1.332 + target.currentProperty.remove(); 1.333 + } 1.334 + } 1.335 + } 1.336 + 1.337 + public void serializeBody(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.338 + if (superClazz != null) { 1.339 + superClazz.serializeBody(bean, target); 1.340 + } 1.341 + try { 1.342 + for (Property<BeanT> p : properties) { 1.343 + if (retainPropertyInfo) { 1.344 + target.currentProperty.set(p); 1.345 + } 1.346 + boolean isThereAnOverridingProperty = p.isHiddenByOverride(); 1.347 + if (!isThereAnOverridingProperty || bean.getClass().equals(jaxbType)) { 1.348 + p.serializeBody(bean, target, null); 1.349 + } else if (isThereAnOverridingProperty) { 1.350 + // need to double check the override - it should be safe to do after the model has been created because it's targeted to override properties only 1.351 + Class beanClass = bean.getClass(); 1.352 + if (Utils.REFLECTION_NAVIGATOR.getDeclaredField(beanClass, p.getFieldName()) == null) { 1.353 + p.serializeBody(bean, target, null); 1.354 + } 1.355 + } 1.356 + } 1.357 + } catch (AccessorException e) { 1.358 + target.reportError(null, e); 1.359 + } 1.360 + } 1.361 + 1.362 + public void serializeAttributes(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.363 + for( AttributeProperty<BeanT> p : attributeProperties ) 1.364 + try { 1.365 + if (retainPropertyInfo) { 1.366 + final Property parentProperty = target.getCurrentProperty(); 1.367 + target.currentProperty.set(p); 1.368 + p.serializeAttributes(bean,target); 1.369 + target.currentProperty.set(parentProperty); 1.370 + } else { 1.371 + p.serializeAttributes(bean,target); 1.372 + } 1.373 + if (p.attName.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE, "nil")) { 1.374 + isNilIncluded = true; 1.375 + } 1.376 + } catch (AccessorException e) { 1.377 + target.reportError(null,e); 1.378 + } 1.379 + 1.380 + try { 1.381 + if(inheritedAttWildcard!=null) { 1.382 + Map<QName,String> map = inheritedAttWildcard.get(bean); 1.383 + target.attWildcardAsAttributes(map,null); 1.384 + } 1.385 + } catch (AccessorException e) { 1.386 + target.reportError(null,e); 1.387 + } 1.388 + } 1.389 + 1.390 + public void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException { 1.391 + try { 1.392 + if (retainPropertyInfo) { 1.393 + final Property parentProperty = target.getCurrentProperty(); 1.394 + for( Property<BeanT> p : uriProperties ) { 1.395 + target.currentProperty.set(p); 1.396 + p.serializeURIs(bean,target); 1.397 + } 1.398 + target.currentProperty.set(parentProperty); 1.399 + } else { 1.400 + for( Property<BeanT> p : uriProperties ) { 1.401 + p.serializeURIs(bean,target); 1.402 + } 1.403 + } 1.404 + if(inheritedAttWildcard!=null) { 1.405 + Map<QName,String> map = inheritedAttWildcard.get(bean); 1.406 + target.attWildcardAsURIs(map,null); 1.407 + } 1.408 + } catch (AccessorException e) { 1.409 + target.reportError(null,e); 1.410 + } 1.411 + } 1.412 + 1.413 + public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { 1.414 + if(loader==null) { 1.415 + // these variables have to be set before they are initialized, 1.416 + // because the initialization may build other loaders and they may refer to this. 1.417 + StructureLoader sl = new StructureLoader(this); 1.418 + loader = sl; 1.419 + if(ci.hasSubClasses()) 1.420 + loaderWithTypeSubst = new XsiTypeLoader(this); 1.421 + else 1.422 + // optimization. we know there can be no @xsi:type 1.423 + loaderWithTypeSubst = loader; 1.424 + 1.425 + 1.426 + sl.init(context,this,ci.getAttributeWildcard()); 1.427 + } 1.428 + if(typeSubstitutionCapable) 1.429 + return loaderWithTypeSubst; 1.430 + else 1.431 + return loader; 1.432 + } 1.433 + 1.434 + public Transducer<BeanT> getTransducer() { 1.435 + return xducer; 1.436 + } 1.437 + 1.438 + private static final AttributeProperty[] EMPTY_PROPERTIES = new AttributeProperty[0]; 1.439 + 1.440 + private static final Logger logger = Util.getClassLogger(); 1.441 + 1.442 +}