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 Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,431 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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) break; 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 + @Override 1.206 + protected void link(JAXBContextImpl grammar) { 1.207 + if(uriProperties!=null) 1.208 + return; // avoid linking twice 1.209 + 1.210 + super.link(grammar); 1.211 + 1.212 + if(superClazz!=null) 1.213 + superClazz.link(grammar); 1.214 + 1.215 + getLoader(grammar,true); // make sure to build the loader if we haven't done so. 1.216 + 1.217 + // propagate values from super class 1.218 + if(superClazz!=null) { 1.219 + if(idProperty==null) 1.220 + idProperty = superClazz.idProperty; 1.221 + 1.222 + if(!superClazz.hasElementOnlyContentModel()) 1.223 + hasElementOnlyContentModel(false); 1.224 + } 1.225 + 1.226 + // create a list of attribute/URI handlers 1.227 + List<AttributeProperty> attProps = new FinalArrayList<AttributeProperty>(); 1.228 + List<Property> uriProps = new FinalArrayList<Property>(); 1.229 + for (ClassBeanInfoImpl bi = this; bi != null; bi = bi.superClazz) { 1.230 + for (int i = 0; i < bi.properties.length; i++) { 1.231 + Property p = bi.properties[i]; 1.232 + if(p instanceof AttributeProperty) 1.233 + attProps.add((AttributeProperty) p); 1.234 + if(p.hasSerializeURIAction()) 1.235 + uriProps.add(p); 1.236 + } 1.237 + } 1.238 + if(grammar.c14nSupport) 1.239 + Collections.sort(attProps); 1.240 + 1.241 + if(attProps.isEmpty()) 1.242 + attributeProperties = EMPTY_PROPERTIES; 1.243 + else 1.244 + attributeProperties = attProps.toArray(new AttributeProperty[attProps.size()]); 1.245 + 1.246 + if(uriProps.isEmpty()) 1.247 + uriProperties = EMPTY_PROPERTIES; 1.248 + else 1.249 + uriProperties = uriProps.toArray(new Property[uriProps.size()]); 1.250 + } 1.251 + 1.252 + @Override 1.253 + public void wrapUp() { 1.254 + for (Property p : properties) 1.255 + p.wrapUp(); 1.256 + ci = null; 1.257 + super.wrapUp(); 1.258 + } 1.259 + 1.260 + public String getElementNamespaceURI(BeanT bean) { 1.261 + return tagName.nsUri; 1.262 + } 1.263 + 1.264 + public String getElementLocalName(BeanT bean) { 1.265 + return tagName.localName; 1.266 + } 1.267 + 1.268 + public BeanT createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException, SAXException { 1.269 + 1.270 + BeanT bean = null; 1.271 + if (factoryMethod == null){ 1.272 + bean = ClassFactory.create0(jaxbType); 1.273 + }else { 1.274 + Object o = ClassFactory.create(factoryMethod); 1.275 + if( jaxbType.isInstance(o) ){ 1.276 + bean = (BeanT)o; 1.277 + } else { 1.278 + throw new InstantiationException("The factory method didn't return a correct object"); 1.279 + } 1.280 + } 1.281 + 1.282 + if(xmlLocatorField!=null) 1.283 + // need to copy because Locator is mutable 1.284 + try { 1.285 + xmlLocatorField.set(bean,new LocatorImpl(context.getLocator())); 1.286 + } catch (AccessorException e) { 1.287 + context.handleError(e); 1.288 + } 1.289 + return bean; 1.290 + } 1.291 + 1.292 + public boolean reset(BeanT bean, UnmarshallingContext context) throws SAXException { 1.293 + try { 1.294 + if(superClazz!=null) 1.295 + superClazz.reset(bean,context); 1.296 + for( Property<BeanT> p : properties ) 1.297 + p.reset(bean); 1.298 + return true; 1.299 + } catch (AccessorException e) { 1.300 + context.handleError(e); 1.301 + return false; 1.302 + } 1.303 + } 1.304 + 1.305 + public String getId(BeanT bean, XMLSerializer target) throws SAXException { 1.306 + if(idProperty!=null) { 1.307 + try { 1.308 + return idProperty.getIdValue(bean); 1.309 + } catch (AccessorException e) { 1.310 + target.reportError(null,e); 1.311 + } 1.312 + } 1.313 + return null; 1.314 + } 1.315 + 1.316 + public void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.317 + if(tagName==null) { 1.318 + Class beanClass = bean.getClass(); 1.319 + String message; 1.320 + if (beanClass.isAnnotationPresent(XmlRootElement.class)) { 1.321 + message = Messages.UNABLE_TO_MARSHAL_UNBOUND_CLASS.format(beanClass.getName()); 1.322 + } else { 1.323 + message = Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(beanClass.getName()); 1.324 + } 1.325 + target.reportError(new ValidationEventImpl(ValidationEvent.ERROR,message,null, null)); 1.326 + } else { 1.327 + target.startElement(tagName,bean); 1.328 + target.childAsSoleContent(bean,null); 1.329 + target.endElement(); 1.330 + if (retainPropertyInfo) { 1.331 + target.currentProperty.remove(); 1.332 + } 1.333 + } 1.334 + } 1.335 + 1.336 + public void serializeBody(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.337 + if (superClazz != null) { 1.338 + superClazz.serializeBody(bean, target); 1.339 + } 1.340 + try { 1.341 + for (Property<BeanT> p : properties) { 1.342 + if (retainPropertyInfo) { 1.343 + target.currentProperty.set(p); 1.344 + } 1.345 + if (!(p.isHiddenByOverride() && !bean.getClass().equals(jaxbType))) { 1.346 + p.serializeBody(bean, target, null); 1.347 + } 1.348 + } 1.349 + } catch (AccessorException e) { 1.350 + target.reportError(null, e); 1.351 + } 1.352 + } 1.353 + 1.354 + public void serializeAttributes(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException { 1.355 + for( AttributeProperty<BeanT> p : attributeProperties ) 1.356 + try { 1.357 + if (retainPropertyInfo) { 1.358 + final Property parentProperty = target.getCurrentProperty(); 1.359 + target.currentProperty.set(p); 1.360 + p.serializeAttributes(bean,target); 1.361 + target.currentProperty.set(parentProperty); 1.362 + } else { 1.363 + p.serializeAttributes(bean,target); 1.364 + } 1.365 + if (p.attName.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE, "nil")) { 1.366 + isNilIncluded = true; 1.367 + } 1.368 + } catch (AccessorException e) { 1.369 + target.reportError(null,e); 1.370 + } 1.371 + 1.372 + try { 1.373 + if(inheritedAttWildcard!=null) { 1.374 + Map<QName,String> map = inheritedAttWildcard.get(bean); 1.375 + target.attWildcardAsAttributes(map,null); 1.376 + } 1.377 + } catch (AccessorException e) { 1.378 + target.reportError(null,e); 1.379 + } 1.380 + } 1.381 + 1.382 + public void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException { 1.383 + try { 1.384 + if (retainPropertyInfo) { 1.385 + final Property parentProperty = target.getCurrentProperty(); 1.386 + for( Property<BeanT> p : uriProperties ) { 1.387 + target.currentProperty.set(p); 1.388 + p.serializeURIs(bean,target); 1.389 + } 1.390 + target.currentProperty.set(parentProperty); 1.391 + } else { 1.392 + for( Property<BeanT> p : uriProperties ) { 1.393 + p.serializeURIs(bean,target); 1.394 + } 1.395 + } 1.396 + if(inheritedAttWildcard!=null) { 1.397 + Map<QName,String> map = inheritedAttWildcard.get(bean); 1.398 + target.attWildcardAsURIs(map,null); 1.399 + } 1.400 + } catch (AccessorException e) { 1.401 + target.reportError(null,e); 1.402 + } 1.403 + } 1.404 + 1.405 + public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) { 1.406 + if(loader==null) { 1.407 + // these variables have to be set before they are initialized, 1.408 + // because the initialization may build other loaders and they may refer to this. 1.409 + StructureLoader sl = new StructureLoader(this); 1.410 + loader = sl; 1.411 + if(ci.hasSubClasses()) 1.412 + loaderWithTypeSubst = new XsiTypeLoader(this); 1.413 + else 1.414 + // optimization. we know there can be no @xsi:type 1.415 + loaderWithTypeSubst = loader; 1.416 + 1.417 + 1.418 + sl.init(context,this,ci.getAttributeWildcard()); 1.419 + } 1.420 + if(typeSubstitutionCapable) 1.421 + return loaderWithTypeSubst; 1.422 + else 1.423 + return loader; 1.424 + } 1.425 + 1.426 + public Transducer<BeanT> getTransducer() { 1.427 + return xducer; 1.428 + } 1.429 + 1.430 + private static final AttributeProperty[] EMPTY_PROPERTIES = new AttributeProperty[0]; 1.431 + 1.432 + private static final Logger logger = Util.getClassLogger(); 1.433 + 1.434 +}