src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/ClassBeanInfoImpl.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.bind.v2.runtime;
ohair@286 27
ohair@286 28 import java.io.IOException;
ohair@286 29 import java.lang.reflect.InvocationTargetException;
ohair@286 30 import java.lang.reflect.Method;
ohair@286 31 import java.lang.reflect.Modifier;
ohair@286 32 import java.util.Collection;
ohair@286 33 import java.util.Collections;
ohair@286 34 import java.util.List;
ohair@286 35 import java.util.Map;
ohair@286 36 import java.util.logging.Level;
ohair@286 37 import java.util.logging.Logger;
ohair@286 38
ohair@286 39 import javax.xml.bind.ValidationEvent;
ohair@286 40 import javax.xml.bind.annotation.XmlRootElement;
ohair@286 41 import javax.xml.bind.helpers.ValidationEventImpl;
ohair@286 42 import javax.xml.namespace.QName;
ohair@286 43 import javax.xml.stream.XMLStreamException;
ohair@286 44
ohair@286 45 import com.sun.istack.internal.FinalArrayList;
ohair@286 46 import com.sun.xml.internal.bind.Util;
ohair@286 47 import com.sun.xml.internal.bind.api.AccessorException;
ohair@286 48 import com.sun.xml.internal.bind.v2.ClassFactory;
ohair@286 49 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
ohair@286 50 import com.sun.xml.internal.bind.v2.model.core.ID;
ohair@286 51 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeClassInfo;
ohair@286 52 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
ohair@286 53 import com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty;
ohair@286 54 import com.sun.xml.internal.bind.v2.runtime.property.Property;
ohair@286 55 import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory;
ohair@286 56 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
ohair@286 57 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
ohair@286 58 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader;
ohair@286 59 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
ohair@286 60 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiTypeLoader;
ohair@286 61
ohair@286 62 import org.xml.sax.Locator;
ohair@286 63 import org.xml.sax.SAXException;
ohair@286 64 import org.xml.sax.helpers.LocatorImpl;
ohair@286 65
ohair@286 66 /**
ohair@286 67 * {@link JaxBeanInfo} implementation for j2s bean.
ohair@286 68 *
ohair@286 69 * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 70 */
ohair@286 71 public final class ClassBeanInfoImpl<BeanT> extends JaxBeanInfo<BeanT> implements AttributeAccessor<BeanT> {
ohair@286 72
ohair@286 73 /**
ohair@286 74 * Properties of this bean class but not its ancestor classes.
ohair@286 75 */
ohair@286 76 public final Property<BeanT>[] properties;
ohair@286 77
ohair@286 78 /**
ohair@286 79 * Non-null if this bean has an ID property.
ohair@286 80 */
ohair@286 81 private Property<? super BeanT> idProperty;
ohair@286 82
ohair@286 83 /**
ohair@286 84 * Immutable configured loader for this class.
ohair@286 85 *
ohair@286 86 * <p>
ohair@286 87 * Set from the link method, but considered final.
ohair@286 88 */
ohair@286 89 private Loader loader;
ohair@286 90 private Loader loaderWithTypeSubst;
ohair@286 91
ohair@286 92 /**
ohair@286 93 * Set only until the link phase to avoid leaking memory.
ohair@286 94 */
ohair@286 95 private RuntimeClassInfo ci;
ohair@286 96
ohair@286 97 private final Accessor<? super BeanT,Map<QName,String>> inheritedAttWildcard;
ohair@286 98 private final Transducer<BeanT> xducer;
ohair@286 99
ohair@286 100 /**
ohair@286 101 * {@link ClassBeanInfoImpl} that represents the super class of {@link #jaxbType}.
ohair@286 102 */
ohair@286 103 public final ClassBeanInfoImpl<? super BeanT> superClazz;
ohair@286 104
ohair@286 105 private final Accessor<? super BeanT,Locator> xmlLocatorField;
ohair@286 106
ohair@286 107 private final Name tagName;
ohair@286 108
ohair@286 109 private boolean retainPropertyInfo = false;
ohair@286 110
ohair@286 111 /**
ohair@286 112 * The {@link AttributeProperty}s for this type and all its ancestors.
ohair@286 113 * If {@link JAXBContextImpl#c14nSupport} is true, this is sorted alphabetically.
ohair@286 114 */
ohair@286 115 private /*final*/ AttributeProperty<BeanT>[] attributeProperties;
ohair@286 116
ohair@286 117 /**
ohair@286 118 * {@link Property}s that need to receive {@link Property#serializeURIs(Object, XMLSerializer)} callback.
ohair@286 119 */
ohair@286 120 private /*final*/ Property<BeanT>[] uriProperties;
ohair@286 121
ohair@286 122 private final Method factoryMethod;
ohair@286 123
ohair@286 124 /*package*/ ClassBeanInfoImpl(JAXBContextImpl owner, RuntimeClassInfo ci) {
ohair@286 125 super(owner,ci,ci.getClazz(),ci.getTypeName(),ci.isElement(),false,true);
ohair@286 126
ohair@286 127 this.ci = ci;
ohair@286 128 this.inheritedAttWildcard = ci.getAttributeWildcard();
ohair@286 129 this.xducer = ci.getTransducer();
ohair@286 130 this.factoryMethod = ci.getFactoryMethod();
ohair@286 131 this.retainPropertyInfo = owner.retainPropertyInfo;
ohair@286 132
ohair@286 133 // make the factory accessible
ohair@286 134 if(factoryMethod!=null) {
ohair@286 135 int classMod = factoryMethod.getDeclaringClass().getModifiers();
ohair@286 136
ohair@286 137 if(!Modifier.isPublic(classMod) || !Modifier.isPublic(factoryMethod.getModifiers())) {
ohair@286 138 // attempt to make it work even if the constructor is not accessible
ohair@286 139 try {
ohair@286 140 factoryMethod.setAccessible(true);
ohair@286 141 } catch(SecurityException e) {
ohair@286 142 // but if we don't have a permission to do so, work gracefully.
ohair@286 143 logger.log(Level.FINE,"Unable to make the method of "+factoryMethod+" accessible",e);
ohair@286 144 throw e;
ohair@286 145 }
ohair@286 146 }
ohair@286 147 }
ohair@286 148
ohair@286 149
ohair@286 150 if(ci.getBaseClass()==null)
ohair@286 151 this.superClazz = null;
ohair@286 152 else
ohair@286 153 this.superClazz = owner.getOrCreate(ci.getBaseClass());
ohair@286 154
ohair@286 155 if(superClazz!=null && superClazz.xmlLocatorField!=null)
ohair@286 156 xmlLocatorField = superClazz.xmlLocatorField;
ohair@286 157 else
ohair@286 158 xmlLocatorField = ci.getLocatorField();
ohair@286 159
ohair@286 160 // create property objects
ohair@286 161 Collection<? extends RuntimePropertyInfo> ps = ci.getProperties();
ohair@286 162 this.properties = new Property[ps.size()];
ohair@286 163 int idx=0;
ohair@286 164 boolean elementOnly = true;
ohair@286 165 for( RuntimePropertyInfo info : ps ) {
ohair@286 166 Property p = PropertyFactory.create(owner,info);
ohair@286 167 if(info.id()==ID.ID)
ohair@286 168 idProperty = p;
ohair@286 169 properties[idx++] = p;
ohair@286 170 elementOnly &= info.elementOnlyContent();
ohair@286 171 checkOverrideProperties(p);
ohair@286 172 }
ohair@286 173 // super class' idProperty might not be computed at this point,
ohair@286 174 // so check that later
ohair@286 175
ohair@286 176 hasElementOnlyContentModel( elementOnly );
ohair@286 177 // again update this value later when we know that of the super class
ohair@286 178
ohair@286 179 if(ci.isElement())
ohair@286 180 tagName = owner.nameBuilder.createElementName(ci.getElementName());
ohair@286 181 else
ohair@286 182 tagName = null;
ohair@286 183
ohair@286 184 setLifecycleFlags();
ohair@286 185 }
ohair@286 186
ohair@286 187 private void checkOverrideProperties(Property p) {
ohair@286 188 ClassBeanInfoImpl bi = this;
ohair@286 189 while ((bi = bi.superClazz) != null) {
ohair@286 190 Property[] props = bi.properties;
ohair@286 191 if (props == null) break;
ohair@286 192 for (Property superProperty : props) {
ohair@286 193 if (superProperty == null) break;
ohair@286 194 String spName = superProperty.getFieldName();
ohair@286 195 if ((spName != null) && (spName.equals(p.getFieldName()))) {
ohair@286 196 superProperty.setHiddenByOverride(true);
ohair@286 197 }
ohair@286 198 }
ohair@286 199 }
ohair@286 200 }
ohair@286 201
ohair@286 202 @Override
ohair@286 203 protected void link(JAXBContextImpl grammar) {
ohair@286 204 if(uriProperties!=null)
ohair@286 205 return; // avoid linking twice
ohair@286 206
ohair@286 207 super.link(grammar);
ohair@286 208
ohair@286 209 if(superClazz!=null)
ohair@286 210 superClazz.link(grammar);
ohair@286 211
ohair@286 212 getLoader(grammar,true); // make sure to build the loader if we haven't done so.
ohair@286 213
ohair@286 214 // propagate values from super class
ohair@286 215 if(superClazz!=null) {
ohair@286 216 if(idProperty==null)
ohair@286 217 idProperty = superClazz.idProperty;
ohair@286 218
ohair@286 219 if(!superClazz.hasElementOnlyContentModel())
ohair@286 220 hasElementOnlyContentModel(false);
ohair@286 221 }
ohair@286 222
ohair@286 223 // create a list of attribute/URI handlers
ohair@286 224 List<AttributeProperty> attProps = new FinalArrayList<AttributeProperty>();
ohair@286 225 List<Property> uriProps = new FinalArrayList<Property>();
ohair@286 226 for (ClassBeanInfoImpl bi = this; bi != null; bi = bi.superClazz) {
ohair@286 227 for (int i = 0; i < bi.properties.length; i++) {
ohair@286 228 Property p = bi.properties[i];
ohair@286 229 if(p instanceof AttributeProperty)
ohair@286 230 attProps.add((AttributeProperty) p);
ohair@286 231 if(p.hasSerializeURIAction())
ohair@286 232 uriProps.add(p);
ohair@286 233 }
ohair@286 234 }
ohair@286 235 if(grammar.c14nSupport)
ohair@286 236 Collections.sort(attProps);
ohair@286 237
ohair@286 238 if(attProps.isEmpty())
ohair@286 239 attributeProperties = EMPTY_PROPERTIES;
ohair@286 240 else
ohair@286 241 attributeProperties = attProps.toArray(new AttributeProperty[attProps.size()]);
ohair@286 242
ohair@286 243 if(uriProps.isEmpty())
ohair@286 244 uriProperties = EMPTY_PROPERTIES;
ohair@286 245 else
ohair@286 246 uriProperties = uriProps.toArray(new Property[uriProps.size()]);
ohair@286 247 }
ohair@286 248
ohair@286 249 @Override
ohair@286 250 public void wrapUp() {
ohair@286 251 for (Property p : properties)
ohair@286 252 p.wrapUp();
ohair@286 253 ci = null;
ohair@286 254 super.wrapUp();
ohair@286 255 }
ohair@286 256
ohair@286 257 public String getElementNamespaceURI(BeanT bean) {
ohair@286 258 return tagName.nsUri;
ohair@286 259 }
ohair@286 260
ohair@286 261 public String getElementLocalName(BeanT bean) {
ohair@286 262 return tagName.localName;
ohair@286 263 }
ohair@286 264
ohair@286 265 public BeanT createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException, SAXException {
ohair@286 266
ohair@286 267 BeanT bean = null;
ohair@286 268 if (factoryMethod == null){
ohair@286 269 bean = ClassFactory.create0(jaxbType);
ohair@286 270 }else {
ohair@286 271 Object o = ClassFactory.create(factoryMethod);
ohair@286 272 if( jaxbType.isInstance(o) ){
ohair@286 273 bean = (BeanT)o;
ohair@286 274 } else {
ohair@286 275 throw new InstantiationException("The factory method didn't return a correct object");
ohair@286 276 }
ohair@286 277 }
ohair@286 278
ohair@286 279 if(xmlLocatorField!=null)
ohair@286 280 // need to copy because Locator is mutable
ohair@286 281 try {
ohair@286 282 xmlLocatorField.set(bean,new LocatorImpl(context.getLocator()));
ohair@286 283 } catch (AccessorException e) {
ohair@286 284 context.handleError(e);
ohair@286 285 }
ohair@286 286 return bean;
ohair@286 287 }
ohair@286 288
ohair@286 289 public boolean reset(BeanT bean, UnmarshallingContext context) throws SAXException {
ohair@286 290 try {
ohair@286 291 if(superClazz!=null)
ohair@286 292 superClazz.reset(bean,context);
ohair@286 293 for( Property<BeanT> p : properties )
ohair@286 294 p.reset(bean);
ohair@286 295 return true;
ohair@286 296 } catch (AccessorException e) {
ohair@286 297 context.handleError(e);
ohair@286 298 return false;
ohair@286 299 }
ohair@286 300 }
ohair@286 301
ohair@286 302 public String getId(BeanT bean, XMLSerializer target) throws SAXException {
ohair@286 303 if(idProperty!=null) {
ohair@286 304 try {
ohair@286 305 return idProperty.getIdValue(bean);
ohair@286 306 } catch (AccessorException e) {
ohair@286 307 target.reportError(null,e);
ohair@286 308 }
ohair@286 309 }
ohair@286 310 return null;
ohair@286 311 }
ohair@286 312
ohair@286 313 public void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 314 if(tagName==null) {
ohair@286 315 Class beanClass = bean.getClass();
ohair@286 316 String message;
ohair@286 317 if (beanClass.isAnnotationPresent(XmlRootElement.class)) {
ohair@286 318 message = Messages.UNABLE_TO_MARSHAL_UNBOUND_CLASS.format(beanClass.getName());
ohair@286 319 } else {
ohair@286 320 message = Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(beanClass.getName());
ohair@286 321 }
ohair@286 322 target.reportError(new ValidationEventImpl(ValidationEvent.ERROR,message,null, null));
ohair@286 323 } else {
ohair@286 324 target.startElement(tagName,bean);
ohair@286 325 target.childAsSoleContent(bean,null);
ohair@286 326 target.endElement();
ohair@286 327 if (retainPropertyInfo) {
ohair@286 328 target.currentProperty.remove();
ohair@286 329 }
ohair@286 330 }
ohair@286 331 }
ohair@286 332
ohair@286 333 public void serializeBody(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 334 if (superClazz != null) {
ohair@286 335 superClazz.serializeBody(bean, target);
ohair@286 336 }
ohair@286 337 try {
ohair@286 338 for (Property<BeanT> p : properties) {
ohair@286 339 if (retainPropertyInfo) {
ohair@286 340 target.currentProperty.set(p);
ohair@286 341 }
ohair@286 342 if (!(p.isHiddenByOverride() && !bean.getClass().equals(jaxbType))) {
ohair@286 343 p.serializeBody(bean, target, null);
ohair@286 344 }
ohair@286 345 }
ohair@286 346 } catch (AccessorException e) {
ohair@286 347 target.reportError(null, e);
ohair@286 348 }
ohair@286 349 }
ohair@286 350
ohair@286 351 public void serializeAttributes(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 352 for( AttributeProperty<BeanT> p : attributeProperties )
ohair@286 353 try {
ohair@286 354 if (retainPropertyInfo) {
ohair@286 355 final Property parentProperty = target.getCurrentProperty();
ohair@286 356 target.currentProperty.set(p);
ohair@286 357 p.serializeAttributes(bean,target);
ohair@286 358 target.currentProperty.set(parentProperty);
ohair@286 359 } else {
ohair@286 360 p.serializeAttributes(bean,target);
ohair@286 361 }
ohair@286 362 if (p.attName.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE, "nil")) {
ohair@286 363 isNilIncluded = true;
ohair@286 364 }
ohair@286 365 } catch (AccessorException e) {
ohair@286 366 target.reportError(null,e);
ohair@286 367 }
ohair@286 368
ohair@286 369 try {
ohair@286 370 if(inheritedAttWildcard!=null) {
ohair@286 371 Map<QName,String> map = inheritedAttWildcard.get(bean);
ohair@286 372 target.attWildcardAsAttributes(map,null);
ohair@286 373 }
ohair@286 374 } catch (AccessorException e) {
ohair@286 375 target.reportError(null,e);
ohair@286 376 }
ohair@286 377 }
ohair@286 378
ohair@286 379 public void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException {
ohair@286 380 try {
ohair@286 381 if (retainPropertyInfo) {
ohair@286 382 final Property parentProperty = target.getCurrentProperty();
ohair@286 383 for( Property<BeanT> p : uriProperties ) {
ohair@286 384 target.currentProperty.set(p);
ohair@286 385 p.serializeURIs(bean,target);
ohair@286 386 }
ohair@286 387 target.currentProperty.set(parentProperty);
ohair@286 388 } else {
ohair@286 389 for( Property<BeanT> p : uriProperties ) {
ohair@286 390 p.serializeURIs(bean,target);
ohair@286 391 }
ohair@286 392 }
ohair@286 393 if(inheritedAttWildcard!=null) {
ohair@286 394 Map<QName,String> map = inheritedAttWildcard.get(bean);
ohair@286 395 target.attWildcardAsURIs(map,null);
ohair@286 396 }
ohair@286 397 } catch (AccessorException e) {
ohair@286 398 target.reportError(null,e);
ohair@286 399 }
ohair@286 400 }
ohair@286 401
ohair@286 402 public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
ohair@286 403 if(loader==null) {
ohair@286 404 // these variables have to be set before they are initialized,
ohair@286 405 // because the initialization may build other loaders and they may refer to this.
ohair@286 406 StructureLoader sl = new StructureLoader(this);
ohair@286 407 loader = sl;
ohair@286 408 if(ci.hasSubClasses())
ohair@286 409 loaderWithTypeSubst = new XsiTypeLoader(this);
ohair@286 410 else
ohair@286 411 // optimization. we know there can be no @xsi:type
ohair@286 412 loaderWithTypeSubst = loader;
ohair@286 413
ohair@286 414
ohair@286 415 sl.init(context,this,ci.getAttributeWildcard());
ohair@286 416 }
ohair@286 417 if(typeSubstitutionCapable)
ohair@286 418 return loaderWithTypeSubst;
ohair@286 419 else
ohair@286 420 return loader;
ohair@286 421 }
ohair@286 422
ohair@286 423 public Transducer<BeanT> getTransducer() {
ohair@286 424 return xducer;
ohair@286 425 }
ohair@286 426
ohair@286 427 private static final AttributeProperty[] EMPTY_PROPERTIES = new AttributeProperty[0];
ohair@286 428
ohair@286 429 private static final Logger logger = Util.getClassLogger();
ohair@286 430
ohair@286 431 }

mercurial