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

Sun, 18 Jun 2017 23:18:45 +0100

author
aefimov
date
Sun, 18 Jun 2017 23:18:45 +0100
changeset 1491
d5c5a205d7fb
parent 450
b0c2840e2513
child 637
9c07ef4934dd
permissions
-rw-r--r--

8172297: In java 8, the marshalling with JAX-WS does not escape carriage return
Reviewed-by: lancea

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;
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) {
alanb@368 193 if (superProperty != null) {
alanb@368 194 String spName = superProperty.getFieldName();
alanb@368 195 if ((spName != null) && (spName.equals(p.getFieldName()))) {
alanb@368 196 superProperty.setHiddenByOverride(true);
alanb@368 197 }
ohair@286 198 }
ohair@286 199 }
ohair@286 200 }
ohair@286 201 }
ohair@286 202
ohair@286 203 @Override
ohair@286 204 protected void link(JAXBContextImpl grammar) {
ohair@286 205 if(uriProperties!=null)
ohair@286 206 return; // avoid linking twice
ohair@286 207
ohair@286 208 super.link(grammar);
ohair@286 209
ohair@286 210 if(superClazz!=null)
ohair@286 211 superClazz.link(grammar);
ohair@286 212
ohair@286 213 getLoader(grammar,true); // make sure to build the loader if we haven't done so.
ohair@286 214
ohair@286 215 // propagate values from super class
ohair@286 216 if(superClazz!=null) {
ohair@286 217 if(idProperty==null)
ohair@286 218 idProperty = superClazz.idProperty;
ohair@286 219
ohair@286 220 if(!superClazz.hasElementOnlyContentModel())
ohair@286 221 hasElementOnlyContentModel(false);
ohair@286 222 }
ohair@286 223
ohair@286 224 // create a list of attribute/URI handlers
ohair@286 225 List<AttributeProperty> attProps = new FinalArrayList<AttributeProperty>();
ohair@286 226 List<Property> uriProps = new FinalArrayList<Property>();
ohair@286 227 for (ClassBeanInfoImpl bi = this; bi != null; bi = bi.superClazz) {
ohair@286 228 for (int i = 0; i < bi.properties.length; i++) {
ohair@286 229 Property p = bi.properties[i];
ohair@286 230 if(p instanceof AttributeProperty)
ohair@286 231 attProps.add((AttributeProperty) p);
ohair@286 232 if(p.hasSerializeURIAction())
ohair@286 233 uriProps.add(p);
ohair@286 234 }
ohair@286 235 }
ohair@286 236 if(grammar.c14nSupport)
ohair@286 237 Collections.sort(attProps);
ohair@286 238
ohair@286 239 if(attProps.isEmpty())
ohair@286 240 attributeProperties = EMPTY_PROPERTIES;
ohair@286 241 else
ohair@286 242 attributeProperties = attProps.toArray(new AttributeProperty[attProps.size()]);
ohair@286 243
ohair@286 244 if(uriProps.isEmpty())
ohair@286 245 uriProperties = EMPTY_PROPERTIES;
ohair@286 246 else
ohair@286 247 uriProperties = uriProps.toArray(new Property[uriProps.size()]);
ohair@286 248 }
ohair@286 249
ohair@286 250 @Override
ohair@286 251 public void wrapUp() {
ohair@286 252 for (Property p : properties)
ohair@286 253 p.wrapUp();
ohair@286 254 ci = null;
ohair@286 255 super.wrapUp();
ohair@286 256 }
ohair@286 257
ohair@286 258 public String getElementNamespaceURI(BeanT bean) {
ohair@286 259 return tagName.nsUri;
ohair@286 260 }
ohair@286 261
ohair@286 262 public String getElementLocalName(BeanT bean) {
ohair@286 263 return tagName.localName;
ohair@286 264 }
ohair@286 265
ohair@286 266 public BeanT createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException, SAXException {
ohair@286 267
ohair@286 268 BeanT bean = null;
ohair@286 269 if (factoryMethod == null){
ohair@286 270 bean = ClassFactory.create0(jaxbType);
ohair@286 271 }else {
ohair@286 272 Object o = ClassFactory.create(factoryMethod);
ohair@286 273 if( jaxbType.isInstance(o) ){
ohair@286 274 bean = (BeanT)o;
ohair@286 275 } else {
ohair@286 276 throw new InstantiationException("The factory method didn't return a correct object");
ohair@286 277 }
ohair@286 278 }
ohair@286 279
ohair@286 280 if(xmlLocatorField!=null)
ohair@286 281 // need to copy because Locator is mutable
ohair@286 282 try {
ohair@286 283 xmlLocatorField.set(bean,new LocatorImpl(context.getLocator()));
ohair@286 284 } catch (AccessorException e) {
ohair@286 285 context.handleError(e);
ohair@286 286 }
ohair@286 287 return bean;
ohair@286 288 }
ohair@286 289
ohair@286 290 public boolean reset(BeanT bean, UnmarshallingContext context) throws SAXException {
ohair@286 291 try {
ohair@286 292 if(superClazz!=null)
ohair@286 293 superClazz.reset(bean,context);
ohair@286 294 for( Property<BeanT> p : properties )
ohair@286 295 p.reset(bean);
ohair@286 296 return true;
ohair@286 297 } catch (AccessorException e) {
ohair@286 298 context.handleError(e);
ohair@286 299 return false;
ohair@286 300 }
ohair@286 301 }
ohair@286 302
ohair@286 303 public String getId(BeanT bean, XMLSerializer target) throws SAXException {
ohair@286 304 if(idProperty!=null) {
ohair@286 305 try {
ohair@286 306 return idProperty.getIdValue(bean);
ohair@286 307 } catch (AccessorException e) {
ohair@286 308 target.reportError(null,e);
ohair@286 309 }
ohair@286 310 }
ohair@286 311 return null;
ohair@286 312 }
ohair@286 313
ohair@286 314 public void serializeRoot(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 315 if(tagName==null) {
ohair@286 316 Class beanClass = bean.getClass();
ohair@286 317 String message;
ohair@286 318 if (beanClass.isAnnotationPresent(XmlRootElement.class)) {
ohair@286 319 message = Messages.UNABLE_TO_MARSHAL_UNBOUND_CLASS.format(beanClass.getName());
ohair@286 320 } else {
ohair@286 321 message = Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(beanClass.getName());
ohair@286 322 }
ohair@286 323 target.reportError(new ValidationEventImpl(ValidationEvent.ERROR,message,null, null));
ohair@286 324 } else {
ohair@286 325 target.startElement(tagName,bean);
ohair@286 326 target.childAsSoleContent(bean,null);
ohair@286 327 target.endElement();
ohair@286 328 if (retainPropertyInfo) {
ohair@286 329 target.currentProperty.remove();
ohair@286 330 }
ohair@286 331 }
ohair@286 332 }
ohair@286 333
ohair@286 334 public void serializeBody(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 335 if (superClazz != null) {
ohair@286 336 superClazz.serializeBody(bean, target);
ohair@286 337 }
ohair@286 338 try {
ohair@286 339 for (Property<BeanT> p : properties) {
ohair@286 340 if (retainPropertyInfo) {
ohair@286 341 target.currentProperty.set(p);
ohair@286 342 }
alanb@368 343 boolean isThereAnOverridingProperty = p.isHiddenByOverride();
alanb@368 344 if (!isThereAnOverridingProperty || bean.getClass().equals(jaxbType)) {
ohair@286 345 p.serializeBody(bean, target, null);
alanb@368 346 } else if (isThereAnOverridingProperty) {
alanb@368 347 // 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 348 Class beanClass = bean.getClass();
mkos@450 349 if (Utils.REFLECTION_NAVIGATOR.getDeclaredField(beanClass, p.getFieldName()) == null) {
alanb@368 350 p.serializeBody(bean, target, null);
alanb@368 351 }
ohair@286 352 }
ohair@286 353 }
ohair@286 354 } catch (AccessorException e) {
ohair@286 355 target.reportError(null, e);
ohair@286 356 }
ohair@286 357 }
ohair@286 358
ohair@286 359 public void serializeAttributes(BeanT bean, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
ohair@286 360 for( AttributeProperty<BeanT> p : attributeProperties )
ohair@286 361 try {
ohair@286 362 if (retainPropertyInfo) {
ohair@286 363 final Property parentProperty = target.getCurrentProperty();
ohair@286 364 target.currentProperty.set(p);
ohair@286 365 p.serializeAttributes(bean,target);
ohair@286 366 target.currentProperty.set(parentProperty);
ohair@286 367 } else {
ohair@286 368 p.serializeAttributes(bean,target);
ohair@286 369 }
ohair@286 370 if (p.attName.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE, "nil")) {
ohair@286 371 isNilIncluded = true;
ohair@286 372 }
ohair@286 373 } catch (AccessorException e) {
ohair@286 374 target.reportError(null,e);
ohair@286 375 }
ohair@286 376
ohair@286 377 try {
ohair@286 378 if(inheritedAttWildcard!=null) {
ohair@286 379 Map<QName,String> map = inheritedAttWildcard.get(bean);
ohair@286 380 target.attWildcardAsAttributes(map,null);
ohair@286 381 }
ohair@286 382 } catch (AccessorException e) {
ohair@286 383 target.reportError(null,e);
ohair@286 384 }
ohair@286 385 }
ohair@286 386
ohair@286 387 public void serializeURIs(BeanT bean, XMLSerializer target) throws SAXException {
ohair@286 388 try {
ohair@286 389 if (retainPropertyInfo) {
ohair@286 390 final Property parentProperty = target.getCurrentProperty();
ohair@286 391 for( Property<BeanT> p : uriProperties ) {
ohair@286 392 target.currentProperty.set(p);
ohair@286 393 p.serializeURIs(bean,target);
ohair@286 394 }
ohair@286 395 target.currentProperty.set(parentProperty);
ohair@286 396 } else {
ohair@286 397 for( Property<BeanT> p : uriProperties ) {
ohair@286 398 p.serializeURIs(bean,target);
ohair@286 399 }
ohair@286 400 }
ohair@286 401 if(inheritedAttWildcard!=null) {
ohair@286 402 Map<QName,String> map = inheritedAttWildcard.get(bean);
ohair@286 403 target.attWildcardAsURIs(map,null);
ohair@286 404 }
ohair@286 405 } catch (AccessorException e) {
ohair@286 406 target.reportError(null,e);
ohair@286 407 }
ohair@286 408 }
ohair@286 409
ohair@286 410 public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
ohair@286 411 if(loader==null) {
ohair@286 412 // these variables have to be set before they are initialized,
ohair@286 413 // because the initialization may build other loaders and they may refer to this.
ohair@286 414 StructureLoader sl = new StructureLoader(this);
ohair@286 415 loader = sl;
ohair@286 416 if(ci.hasSubClasses())
ohair@286 417 loaderWithTypeSubst = new XsiTypeLoader(this);
ohair@286 418 else
ohair@286 419 // optimization. we know there can be no @xsi:type
ohair@286 420 loaderWithTypeSubst = loader;
ohair@286 421
ohair@286 422
ohair@286 423 sl.init(context,this,ci.getAttributeWildcard());
ohair@286 424 }
ohair@286 425 if(typeSubstitutionCapable)
ohair@286 426 return loaderWithTypeSubst;
ohair@286 427 else
ohair@286 428 return loader;
ohair@286 429 }
ohair@286 430
ohair@286 431 public Transducer<BeanT> getTransducer() {
ohair@286 432 return xducer;
ohair@286 433 }
ohair@286 434
ohair@286 435 private static final AttributeProperty[] EMPTY_PROPERTIES = new AttributeProperty[0];
ohair@286 436
ohair@286 437 private static final Logger logger = Util.getClassLogger();
ohair@286 438
ohair@286 439 }

mercurial