src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/ElementInfoImpl.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.model.impl;
aoqi@0 27
aoqi@0 28 import java.lang.annotation.Annotation;
aoqi@0 29 import java.util.Collection;
aoqi@0 30 import java.util.Collections;
aoqi@0 31 import java.util.List;
aoqi@0 32
aoqi@0 33 import javax.activation.MimeType;
aoqi@0 34 import javax.xml.bind.JAXBElement;
aoqi@0 35 import javax.xml.bind.annotation.XmlAttachmentRef;
aoqi@0 36 import javax.xml.bind.annotation.XmlElementDecl;
aoqi@0 37 import javax.xml.bind.annotation.XmlID;
aoqi@0 38 import javax.xml.bind.annotation.XmlIDREF;
aoqi@0 39 import javax.xml.bind.annotation.XmlInlineBinaryData;
aoqi@0 40 import javax.xml.bind.annotation.XmlSchema;
aoqi@0 41 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
aoqi@0 42 import javax.xml.namespace.QName;
aoqi@0 43
aoqi@0 44 import com.sun.istack.internal.FinalArrayList;
aoqi@0 45 import com.sun.xml.internal.bind.v2.TODO;
aoqi@0 46 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationSource;
aoqi@0 47 import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
aoqi@0 48 import com.sun.xml.internal.bind.v2.model.core.Adapter;
aoqi@0 49 import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
aoqi@0 50 import com.sun.xml.internal.bind.v2.model.core.ElementInfo;
aoqi@0 51 import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo;
aoqi@0 52 import com.sun.xml.internal.bind.v2.model.core.ID;
aoqi@0 53 import com.sun.xml.internal.bind.v2.model.core.NonElement;
aoqi@0 54 import com.sun.xml.internal.bind.v2.model.core.PropertyInfo;
aoqi@0 55 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
aoqi@0 56 import com.sun.xml.internal.bind.v2.model.core.TypeInfo;
aoqi@0 57 import com.sun.xml.internal.bind.v2.model.core.TypeRef;
aoqi@0 58 import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
aoqi@0 59 import com.sun.xml.internal.bind.v2.runtime.Location;
aoqi@0 60 import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapter;
aoqi@0 61
aoqi@0 62 /**
aoqi@0 63 * {@link ElementInfo} implementation.
aoqi@0 64 *
aoqi@0 65 * @author Kohsuke Kawaguchi
aoqi@0 66 */
aoqi@0 67 class ElementInfoImpl<T,C,F,M> extends TypeInfoImpl<T,C,F,M> implements ElementInfo<T,C> {
aoqi@0 68
aoqi@0 69 private final QName tagName;
aoqi@0 70
aoqi@0 71 private final NonElement<T,C> contentType;
aoqi@0 72
aoqi@0 73 private final T tOfJAXBElementT;
aoqi@0 74
aoqi@0 75 private final T elementType;
aoqi@0 76
aoqi@0 77 private final ClassInfo<T,C> scope;
aoqi@0 78
aoqi@0 79 /**
aoqi@0 80 * Annotation that controls the binding.
aoqi@0 81 */
aoqi@0 82 private final XmlElementDecl anno;
aoqi@0 83
aoqi@0 84 /**
aoqi@0 85 * If this element can substitute another element, the element name.
aoqi@0 86 * @see #link()
aoqi@0 87 */
aoqi@0 88 private ElementInfoImpl<T,C,F,M> substitutionHead;
aoqi@0 89
aoqi@0 90 /**
aoqi@0 91 * Lazily constructed list of {@link ElementInfo}s that can substitute this element.
aoqi@0 92 * This could be null.
aoqi@0 93 * @see #link()
aoqi@0 94 */
aoqi@0 95 private FinalArrayList<ElementInfoImpl<T,C,F,M>> substitutionMembers;
aoqi@0 96
aoqi@0 97 /**
aoqi@0 98 * The factory method from which this mapping was created.
aoqi@0 99 */
aoqi@0 100 private final M method;
aoqi@0 101
aoqi@0 102 /**
aoqi@0 103 * If the content type is adapter, return that adapter.
aoqi@0 104 */
aoqi@0 105 private final Adapter<T,C> adapter;
aoqi@0 106
aoqi@0 107 private final boolean isCollection;
aoqi@0 108
aoqi@0 109 private final ID id;
aoqi@0 110
aoqi@0 111 private final PropertyImpl property;
aoqi@0 112 private final MimeType expectedMimeType;
aoqi@0 113 private final boolean inlineBinary;
aoqi@0 114 private final QName schemaType;
aoqi@0 115
aoqi@0 116 /**
aoqi@0 117 * Singleton instance of {@link ElementPropertyInfo} for this element.
aoqi@0 118 */
aoqi@0 119 protected class PropertyImpl implements
aoqi@0 120 ElementPropertyInfo<T,C>,
aoqi@0 121 TypeRef<T,C>,
aoqi@0 122 AnnotationSource {
aoqi@0 123 //
aoqi@0 124 // TypeRef impl
aoqi@0 125 //
aoqi@0 126 public NonElement<T,C> getTarget() {
aoqi@0 127 return contentType;
aoqi@0 128 }
aoqi@0 129 public QName getTagName() {
aoqi@0 130 return tagName;
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 public List<? extends TypeRef<T,C>> getTypes() {
aoqi@0 134 return Collections.singletonList(this);
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 public List<? extends NonElement<T,C>> ref() {
aoqi@0 138 return Collections.singletonList(contentType);
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 public QName getXmlName() {
aoqi@0 142 return tagName;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 public boolean isCollectionRequired() {
aoqi@0 146 return false;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 public boolean isCollectionNillable() {
aoqi@0 150 return true;
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 public boolean isNillable() {
aoqi@0 154 return true;
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 public String getDefaultValue() {
aoqi@0 158 String v = anno.defaultValue();
aoqi@0 159 if(v.equals("\u0000"))
aoqi@0 160 return null;
aoqi@0 161 else
aoqi@0 162 return v;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 public ElementInfoImpl<T,C,F,M> parent() {
aoqi@0 166 return ElementInfoImpl.this;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 public String getName() {
aoqi@0 170 return "value";
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 public String displayName() {
aoqi@0 174 return "JAXBElement#value";
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 public boolean isCollection() {
aoqi@0 178 return isCollection;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 /**
aoqi@0 182 * For {@link ElementInfo}s, a collection always means a list of values.
aoqi@0 183 */
aoqi@0 184 public boolean isValueList() {
aoqi@0 185 return isCollection;
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 public boolean isRequired() {
aoqi@0 189 return true;
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 public PropertyKind kind() {
aoqi@0 193 return PropertyKind.ELEMENT;
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 public Adapter<T,C> getAdapter() {
aoqi@0 197 return adapter;
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 public ID id() {
aoqi@0 201 return id;
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 public MimeType getExpectedMimeType() {
aoqi@0 205 return expectedMimeType;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 public QName getSchemaType() {
aoqi@0 209 return schemaType;
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 public boolean inlineBinaryData() {
aoqi@0 213 return inlineBinary;
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 public PropertyInfo<T,C> getSource() {
aoqi@0 217 return this;
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 //
aoqi@0 221 //
aoqi@0 222 // AnnotationSource impl
aoqi@0 223 //
aoqi@0 224 //
aoqi@0 225 public <A extends Annotation> A readAnnotation(Class<A> annotationType) {
aoqi@0 226 return reader().getMethodAnnotation(annotationType,method,ElementInfoImpl.this);
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 public boolean hasAnnotation(Class<? extends Annotation> annotationType) {
aoqi@0 230 return reader().hasMethodAnnotation(annotationType,method);
aoqi@0 231 }
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 /**
aoqi@0 235 * @param m
aoqi@0 236 * The factory method on ObjectFactory that comes with {@link XmlElementDecl}.
aoqi@0 237 */
aoqi@0 238 public ElementInfoImpl(ModelBuilder<T,C,F,M> builder,
aoqi@0 239 RegistryInfoImpl<T,C,F,M> registry, M m ) throws IllegalAnnotationException {
aoqi@0 240 super(builder,registry);
aoqi@0 241
aoqi@0 242 this.method = m;
aoqi@0 243 anno = reader().getMethodAnnotation( XmlElementDecl.class, m, this );
aoqi@0 244 assert anno!=null; // the caller should check this
aoqi@0 245 assert anno instanceof Locatable;
aoqi@0 246
aoqi@0 247 elementType = nav().getReturnType(m);
aoqi@0 248 T baseClass = nav().getBaseClass(elementType,nav().asDecl(JAXBElement.class));
aoqi@0 249 if(baseClass==null)
aoqi@0 250 throw new IllegalAnnotationException(
aoqi@0 251 Messages.XML_ELEMENT_MAPPING_ON_NON_IXMLELEMENT_METHOD.format(nav().getMethodName(m)),
aoqi@0 252 anno );
aoqi@0 253
aoqi@0 254 tagName = parseElementName(anno);
aoqi@0 255 T[] methodParams = nav().getMethodParameters(m);
aoqi@0 256
aoqi@0 257 // adapter
aoqi@0 258 Adapter<T,C> a = null;
aoqi@0 259 if(methodParams.length>0) {
aoqi@0 260 XmlJavaTypeAdapter adapter = reader().getMethodAnnotation(XmlJavaTypeAdapter.class,m,this);
aoqi@0 261 if(adapter!=null)
aoqi@0 262 a = new Adapter<T,C>(adapter,reader(),nav());
aoqi@0 263 else {
aoqi@0 264 XmlAttachmentRef xsa = reader().getMethodAnnotation(XmlAttachmentRef.class,m,this);
aoqi@0 265 if(xsa!=null) {
aoqi@0 266 TODO.prototype("in Annotation Processing swaRefAdapter isn't avaialble, so this returns null");
aoqi@0 267 a = new Adapter<T,C>(owner.nav.asDecl(SwaRefAdapter.class),owner.nav);
aoqi@0 268 }
aoqi@0 269 }
aoqi@0 270 }
aoqi@0 271 this.adapter = a;
aoqi@0 272
aoqi@0 273 // T of JAXBElement<T>
aoqi@0 274 tOfJAXBElementT =
aoqi@0 275 methodParams.length>0 ? methodParams[0] // this is more reliable, as it works even for ObjectFactory that sometimes have to return public types
aoqi@0 276 : nav().getTypeArgument(baseClass,0); // fall back to infer from the return type if no parameter.
aoqi@0 277
aoqi@0 278 if(adapter==null) {
aoqi@0 279 T list = nav().getBaseClass(tOfJAXBElementT,nav().asDecl(List.class));
aoqi@0 280 if(list==null) {
aoqi@0 281 isCollection = false;
aoqi@0 282 contentType = builder.getTypeInfo(tOfJAXBElementT,this); // suck this type into the current set.
aoqi@0 283 } else {
aoqi@0 284 isCollection = true;
aoqi@0 285 contentType = builder.getTypeInfo(nav().getTypeArgument(list,0),this);
aoqi@0 286 }
aoqi@0 287 } else {
aoqi@0 288 // but if adapted, use the adapted type
aoqi@0 289 contentType = builder.getTypeInfo(this.adapter.defaultType,this);
aoqi@0 290 isCollection = false;
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 // scope
aoqi@0 294 T s = reader().getClassValue(anno,"scope");
aoqi@0 295 if(nav().isSameType(s, nav().ref(XmlElementDecl.GLOBAL.class)))
aoqi@0 296 scope = null;
aoqi@0 297 else {
aoqi@0 298 // TODO: what happens if there's an error?
aoqi@0 299 NonElement<T,C> scp = builder.getClassInfo(nav().asDecl(s),this);
aoqi@0 300 if(!(scp instanceof ClassInfo)) {
aoqi@0 301 throw new IllegalAnnotationException(
aoqi@0 302 Messages.SCOPE_IS_NOT_COMPLEXTYPE.format(nav().getTypeName(s)),
aoqi@0 303 anno );
aoqi@0 304 }
aoqi@0 305 scope = (ClassInfo<T,C>)scp;
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 id = calcId();
aoqi@0 309
aoqi@0 310 property = createPropertyImpl();
aoqi@0 311
aoqi@0 312 this.expectedMimeType = Util.calcExpectedMediaType(property,builder);
aoqi@0 313 this.inlineBinary = reader().hasMethodAnnotation(XmlInlineBinaryData.class,method);
aoqi@0 314 this.schemaType = Util.calcSchemaType(reader(),property,registry.registryClass,
aoqi@0 315 getContentInMemoryType(),this);
aoqi@0 316 }
aoqi@0 317
aoqi@0 318 final QName parseElementName(XmlElementDecl e) {
aoqi@0 319 String local = e.name();
aoqi@0 320 String nsUri = e.namespace();
aoqi@0 321 if(nsUri.equals("##default")) {
aoqi@0 322 // if defaulted ...
aoqi@0 323 XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class,
aoqi@0 324 nav().getDeclaringClassForMethod(method),this);
aoqi@0 325 if(xs!=null)
aoqi@0 326 nsUri = xs.namespace();
aoqi@0 327 else {
aoqi@0 328 nsUri = builder.defaultNsUri;
aoqi@0 329 }
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 return new QName(nsUri.intern(),local.intern());
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 protected PropertyImpl createPropertyImpl() {
aoqi@0 336 return new PropertyImpl();
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 public ElementPropertyInfo<T,C> getProperty() {
aoqi@0 340 return property;
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 public NonElement<T,C> getContentType() {
aoqi@0 344 return contentType;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 public T getContentInMemoryType() {
aoqi@0 348 if(adapter==null) {
aoqi@0 349 return tOfJAXBElementT;
aoqi@0 350 } else {
aoqi@0 351 return adapter.customType;
aoqi@0 352 }
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 public QName getElementName() {
aoqi@0 356 return tagName;
aoqi@0 357 }
aoqi@0 358
aoqi@0 359 public T getType() {
aoqi@0 360 return elementType;
aoqi@0 361 }
aoqi@0 362
aoqi@0 363 /**
aoqi@0 364 * Leaf-type cannot be referenced from IDREF.
aoqi@0 365 *
aoqi@0 366 * @deprecated
aoqi@0 367 * why are you calling a method whose return value is always known?
aoqi@0 368 */
aoqi@0 369 public final boolean canBeReferencedByIDREF() {
aoqi@0 370 return false;
aoqi@0 371 }
aoqi@0 372
aoqi@0 373 private ID calcId() {
aoqi@0 374 // TODO: share code with PropertyInfoImpl
aoqi@0 375 if(reader().hasMethodAnnotation(XmlID.class,method)) {
aoqi@0 376 return ID.ID;
aoqi@0 377 } else
aoqi@0 378 if(reader().hasMethodAnnotation(XmlIDREF.class,method)) {
aoqi@0 379 return ID.IDREF;
aoqi@0 380 } else {
aoqi@0 381 return ID.NONE;
aoqi@0 382 }
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 public ClassInfo<T, C> getScope() {
aoqi@0 386 return scope;
aoqi@0 387 }
aoqi@0 388
aoqi@0 389 public ElementInfo<T,C> getSubstitutionHead() {
aoqi@0 390 return substitutionHead;
aoqi@0 391 }
aoqi@0 392
aoqi@0 393 public Collection<? extends ElementInfoImpl<T,C,F,M>> getSubstitutionMembers() {
aoqi@0 394 if(substitutionMembers==null)
aoqi@0 395 return Collections.emptyList();
aoqi@0 396 else
aoqi@0 397 return substitutionMembers;
aoqi@0 398 }
aoqi@0 399
aoqi@0 400 /**
aoqi@0 401 * Called after all the {@link TypeInfo}s are collected into the {@link #owner}.
aoqi@0 402 */
aoqi@0 403 /*package*/ void link() {
aoqi@0 404 // substitution head
aoqi@0 405 if(anno.substitutionHeadName().length()!=0) {
aoqi@0 406 QName name = new QName(
aoqi@0 407 anno.substitutionHeadNamespace(), anno.substitutionHeadName() );
aoqi@0 408 substitutionHead = owner.getElementInfo(null,name);
aoqi@0 409 if(substitutionHead==null) {
aoqi@0 410 builder.reportError(
aoqi@0 411 new IllegalAnnotationException(Messages.NON_EXISTENT_ELEMENT_MAPPING.format(
aoqi@0 412 name.getNamespaceURI(),name.getLocalPart()), anno));
aoqi@0 413 // recover by ignoring this substitution declaration
aoqi@0 414 } else
aoqi@0 415 substitutionHead.addSubstitutionMember(this);
aoqi@0 416 } else
aoqi@0 417 substitutionHead = null;
aoqi@0 418 super.link();
aoqi@0 419 }
aoqi@0 420
aoqi@0 421 private void addSubstitutionMember(ElementInfoImpl<T,C,F,M> child) {
aoqi@0 422 if(substitutionMembers==null)
aoqi@0 423 substitutionMembers = new FinalArrayList<ElementInfoImpl<T,C,F,M>>();
aoqi@0 424 substitutionMembers.add(child);
aoqi@0 425 }
aoqi@0 426
aoqi@0 427 public Location getLocation() {
aoqi@0 428 return nav().getMethodLocation(method);
aoqi@0 429 }
aoqi@0 430 }

mercurial