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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 450
b0c2840e2513
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial