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

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

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, 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.util.Collections;
aoqi@0 29 import java.util.LinkedHashSet;
aoqi@0 30 import java.util.Set;
aoqi@0 31
aoqi@0 32 import javax.xml.bind.JAXBElement;
aoqi@0 33 import javax.xml.bind.annotation.XmlAnyElement;
aoqi@0 34 import javax.xml.bind.annotation.XmlElementRef;
aoqi@0 35 import javax.xml.bind.annotation.XmlElementRefs;
aoqi@0 36 import javax.xml.bind.annotation.XmlMixed;
aoqi@0 37 import javax.xml.bind.annotation.XmlSchema;
aoqi@0 38 import javax.xml.bind.annotation.XmlNsForm;
aoqi@0 39 import javax.xml.namespace.QName;
aoqi@0 40
aoqi@0 41 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
aoqi@0 42 import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
aoqi@0 43 import com.sun.xml.internal.bind.v2.model.core.Element;
aoqi@0 44 import com.sun.xml.internal.bind.v2.model.core.ElementInfo;
aoqi@0 45 import com.sun.xml.internal.bind.v2.model.core.NonElement;
aoqi@0 46 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
aoqi@0 47 import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo;
aoqi@0 48 import com.sun.xml.internal.bind.v2.model.core.WildcardMode;
aoqi@0 49 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
aoqi@0 50 import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
aoqi@0 51 import java.util.Iterator;
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * Implementation of {@link ReferencePropertyInfo}.
aoqi@0 55 *
aoqi@0 56 * @author Kohsuke Kawaguchi
aoqi@0 57 */
aoqi@0 58 class ReferencePropertyInfoImpl<T,C,F,M>
aoqi@0 59 extends ERPropertyInfoImpl<T,C,F,M>
aoqi@0 60 implements ReferencePropertyInfo<T,C>, DummyPropertyInfo<T, C, F, M>
aoqi@0 61 {
aoqi@0 62 /**
aoqi@0 63 * Lazily computed.
aoqi@0 64 * @see #getElements()
aoqi@0 65 */
aoqi@0 66 private Set<Element<T,C>> types;
aoqi@0 67 private Set<ReferencePropertyInfoImpl<T,C,F,M>> subTypes = new LinkedHashSet<ReferencePropertyInfoImpl<T,C,F,M>>();
aoqi@0 68
aoqi@0 69 private final boolean isMixed;
aoqi@0 70
aoqi@0 71 private final WildcardMode wildcard;
aoqi@0 72 private final C domHandler;
aoqi@0 73 /**
aoqi@0 74 * Lazily computed.
aoqi@0 75 * @see #isRequired()
aoqi@0 76 */
aoqi@0 77 private Boolean isRequired;
aoqi@0 78
aoqi@0 79 public ReferencePropertyInfoImpl(
aoqi@0 80 ClassInfoImpl<T,C,F,M> classInfo,
aoqi@0 81 PropertySeed<T,C,F,M> seed) {
aoqi@0 82
aoqi@0 83 super(classInfo, seed);
aoqi@0 84
aoqi@0 85 isMixed = seed.readAnnotation(XmlMixed.class) != null;
aoqi@0 86
aoqi@0 87 XmlAnyElement xae = seed.readAnnotation(XmlAnyElement.class);
aoqi@0 88 if(xae==null) {
aoqi@0 89 wildcard = null;
aoqi@0 90 domHandler = null;
aoqi@0 91 } else {
aoqi@0 92 wildcard = xae.lax()?WildcardMode.LAX:WildcardMode.SKIP;
aoqi@0 93 domHandler = nav().asDecl(reader().getClassValue(xae,"value"));
aoqi@0 94 }
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 public Set<? extends Element<T,C>> ref() {
aoqi@0 98 return getElements();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 public PropertyKind kind() {
aoqi@0 102 return PropertyKind.REFERENCE;
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 public Set<? extends Element<T,C>> getElements() {
aoqi@0 106 if(types==null)
aoqi@0 107 calcTypes(false);
aoqi@0 108 assert types!=null;
aoqi@0 109 return types;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 /**
aoqi@0 113 * Compute {@link #types}.
aoqi@0 114 *
aoqi@0 115 * @param last
aoqi@0 116 * if true, every {@link XmlElementRef} must yield at least one type.
aoqi@0 117 */
aoqi@0 118 private void calcTypes(boolean last) {
aoqi@0 119 XmlElementRef[] ann;
aoqi@0 120 types = new LinkedHashSet<Element<T,C>>();
aoqi@0 121 XmlElementRefs refs = seed.readAnnotation(XmlElementRefs.class);
aoqi@0 122 XmlElementRef ref = seed.readAnnotation(XmlElementRef.class);
aoqi@0 123
aoqi@0 124 if(refs!=null && ref!=null) {
aoqi@0 125 parent.builder.reportError(new IllegalAnnotationException(
aoqi@0 126 Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format(
aoqi@0 127 nav().getClassName(parent.getClazz())+'#'+seed.getName(),
aoqi@0 128 ref.annotationType().getName(), refs.annotationType().getName()),
aoqi@0 129 ref, refs ));
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 if(refs!=null)
aoqi@0 133 ann = refs.value();
aoqi@0 134 else {
aoqi@0 135 if(ref!=null)
aoqi@0 136 ann = new XmlElementRef[]{ref};
aoqi@0 137 else
aoqi@0 138 ann = null;
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 isRequired = !isCollection(); // this is by default, to remain compatible with 2.1
aoqi@0 142
aoqi@0 143 if(ann!=null) {
aoqi@0 144 Navigator<T,C,F,M> nav = nav();
aoqi@0 145 AnnotationReader<T,C,F,M> reader = reader();
aoqi@0 146
aoqi@0 147 final T defaultType = nav.ref(XmlElementRef.DEFAULT.class);
aoqi@0 148 final C je = nav.asDecl(JAXBElement.class);
aoqi@0 149
aoqi@0 150 for( XmlElementRef r : ann ) {
aoqi@0 151 boolean yield;
aoqi@0 152 T type = reader.getClassValue(r,"type");
aoqi@0 153 if(nav().isSameType(type, defaultType))
aoqi@0 154 type = nav.erasure(getIndividualType());
aoqi@0 155 if(nav.getBaseClass(type,je)!=null)
aoqi@0 156 yield = addGenericElement(r);
aoqi@0 157 else
aoqi@0 158 yield = addAllSubtypes(type);
aoqi@0 159
aoqi@0 160 // essentially "isRequired &= isRequired(r)" except that we'd like to skip evaluating isRequird(r)
aoqi@0 161 // if the value is already false.
aoqi@0 162 if(isRequired && !isRequired(r))
aoqi@0 163 isRequired = false;
aoqi@0 164
aoqi@0 165 if(last && !yield) {
aoqi@0 166 // a reference didn't produce any type.
aoqi@0 167 // diagnose the problem
aoqi@0 168 if(nav().isSameType(type, nav.ref(JAXBElement.class))) {
aoqi@0 169 // no XmlElementDecl
aoqi@0 170 parent.builder.reportError(new IllegalAnnotationException(
aoqi@0 171 Messages.NO_XML_ELEMENT_DECL.format(
aoqi@0 172 getEffectiveNamespaceFor(r), r.name()),
aoqi@0 173 this
aoqi@0 174 ));
aoqi@0 175 } else {
aoqi@0 176 parent.builder.reportError(new IllegalAnnotationException(
aoqi@0 177 Messages.INVALID_XML_ELEMENT_REF.format(type),this));
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 // reporting one error would do.
aoqi@0 181 // often the element ref field is using @XmlElementRefs
aoqi@0 182 // to point to multiple JAXBElements.
aoqi@0 183 // reporting one error for each @XmlElemetnRef is thus often redundant.
aoqi@0 184 return;
aoqi@0 185 }
aoqi@0 186 }
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 for (ReferencePropertyInfoImpl<T, C, F, M> info : subTypes) {
aoqi@0 190 PropertySeed sd = info.seed;
aoqi@0 191 refs = sd.readAnnotation(XmlElementRefs.class);
aoqi@0 192 ref = sd.readAnnotation(XmlElementRef.class);
aoqi@0 193
aoqi@0 194 if (refs != null && ref != null) {
aoqi@0 195 parent.builder.reportError(new IllegalAnnotationException(
aoqi@0 196 Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format(
aoqi@0 197 nav().getClassName(parent.getClazz())+'#'+seed.getName(),
aoqi@0 198 ref.annotationType().getName(), refs.annotationType().getName()),
aoqi@0 199 ref, refs ));
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 if (refs != null) {
aoqi@0 203 ann = refs.value();
aoqi@0 204 } else {
aoqi@0 205 if (ref != null) {
aoqi@0 206 ann = new XmlElementRef[]{ref};
aoqi@0 207 } else {
aoqi@0 208 ann = null;
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 if (ann != null) {
aoqi@0 213 Navigator<T,C,F,M> nav = nav();
aoqi@0 214 AnnotationReader<T,C,F,M> reader = reader();
aoqi@0 215
aoqi@0 216 final T defaultType = nav.ref(XmlElementRef.DEFAULT.class);
aoqi@0 217 final C je = nav.asDecl(JAXBElement.class);
aoqi@0 218
aoqi@0 219 for( XmlElementRef r : ann ) {
aoqi@0 220 boolean yield;
aoqi@0 221 T type = reader.getClassValue(r,"type");
aoqi@0 222 if (nav().isSameType(type, defaultType)) {
aoqi@0 223 type = nav.erasure(getIndividualType());
aoqi@0 224 }
aoqi@0 225 if (nav.getBaseClass(type,je) != null) {
aoqi@0 226 yield = addGenericElement(r, info);
aoqi@0 227
aoqi@0 228 } else {
aoqi@0 229 yield = addAllSubtypes(type);
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 if(last && !yield) {
aoqi@0 233 // a reference didn't produce any type.
aoqi@0 234 // diagnose the problem
aoqi@0 235 if(nav().isSameType(type, nav.ref(JAXBElement.class))) {
aoqi@0 236 // no XmlElementDecl
aoqi@0 237 parent.builder.reportError(new IllegalAnnotationException(
aoqi@0 238 Messages.NO_XML_ELEMENT_DECL.format(
aoqi@0 239 getEffectiveNamespaceFor(r), r.name()),
aoqi@0 240 this
aoqi@0 241 ));
aoqi@0 242 } else {
aoqi@0 243 parent.builder.reportError(new IllegalAnnotationException(
aoqi@0 244 Messages.INVALID_XML_ELEMENT_REF.format(),this));
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 // reporting one error would do.
aoqi@0 248 // often the element ref field is using @XmlElementRefs
aoqi@0 249 // to point to multiple JAXBElements.
aoqi@0 250 // reporting one error for each @XmlElemetnRef is thus often redundant.
aoqi@0 251 return;
aoqi@0 252 }
aoqi@0 253 }
aoqi@0 254 }
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 types = Collections.unmodifiableSet(types);
aoqi@0 258 }
aoqi@0 259
aoqi@0 260 public boolean isRequired() {
aoqi@0 261 if(isRequired==null)
aoqi@0 262 calcTypes(false);
aoqi@0 263 return isRequired;
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 /**
aoqi@0 267 * If we find out that we are working with 2.1 API, remember the fact so that
aoqi@0 268 * we don't waste time generating exceptions every time we call {@link #isRequired(XmlElementRef)}.
aoqi@0 269 */
aoqi@0 270 private static boolean is2_2 = true;
aoqi@0 271
aoqi@0 272 /**
aoqi@0 273 * Reads the value of {@code XmlElementRef.required()}.
aoqi@0 274 *
aoqi@0 275 * If we are working as 2.1 RI, this defaults to true.
aoqi@0 276 */
aoqi@0 277 private boolean isRequired(XmlElementRef ref) {
aoqi@0 278 if(!is2_2) return true;
aoqi@0 279
aoqi@0 280 try {
aoqi@0 281 return ref.required();
aoqi@0 282 } catch(LinkageError e) {
aoqi@0 283 is2_2 = false;
aoqi@0 284 return true; // the value defaults to true
aoqi@0 285 }
aoqi@0 286 }
aoqi@0 287
aoqi@0 288 /**
aoqi@0 289 * @return
aoqi@0 290 * true if the reference yields at least one type
aoqi@0 291 */
aoqi@0 292 private boolean addGenericElement(XmlElementRef r) {
aoqi@0 293 String nsUri = getEffectiveNamespaceFor(r);
aoqi@0 294 // TODO: check spec. defaulting of localName.
aoqi@0 295 return addGenericElement(parent.owner.getElementInfo(parent.getClazz(),new QName(nsUri,r.name())));
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 private boolean addGenericElement(XmlElementRef r, ReferencePropertyInfoImpl<T,C,F,M> info) {
aoqi@0 299 String nsUri = info.getEffectiveNamespaceFor(r);
aoqi@0 300 ElementInfo ei = parent.owner.getElementInfo(info.parent.getClazz(), new QName(nsUri, r.name()));
aoqi@0 301 types.add(ei);
aoqi@0 302 return true;
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 private String getEffectiveNamespaceFor(XmlElementRef r) {
aoqi@0 306 String nsUri = r.namespace();
aoqi@0 307
aoqi@0 308 XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this );
aoqi@0 309 if(xs!=null && xs.attributeFormDefault()== XmlNsForm.QUALIFIED) {
aoqi@0 310 // JAX-RPC doesn't want the default namespace URI swapping to take effect to
aoqi@0 311 // local "unqualified" elements. UGLY.
aoqi@0 312 if(nsUri.length()==0)
aoqi@0 313 nsUri = parent.builder.defaultNsUri;
aoqi@0 314 }
aoqi@0 315
aoqi@0 316 return nsUri;
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 private boolean addGenericElement(ElementInfo<T,C> ei) {
aoqi@0 320 if(ei==null)
aoqi@0 321 return false;
aoqi@0 322 types.add(ei);
aoqi@0 323 for( ElementInfo<T,C> subst : ei.getSubstitutionMembers() )
aoqi@0 324 addGenericElement(subst);
aoqi@0 325 return true;
aoqi@0 326 }
aoqi@0 327
aoqi@0 328 private boolean addAllSubtypes(T type) {
aoqi@0 329 Navigator<T,C,F,M> nav = nav();
aoqi@0 330
aoqi@0 331 // this allows the explicitly referenced type to be sucked in to the model
aoqi@0 332 NonElement<T,C> t = parent.builder.getClassInfo(nav.asDecl(type),this);
aoqi@0 333 if(!(t instanceof ClassInfo))
aoqi@0 334 // this is leaf.
aoqi@0 335 return false;
aoqi@0 336
aoqi@0 337 boolean result = false;
aoqi@0 338
aoqi@0 339 ClassInfo<T,C> c = (ClassInfo<T,C>) t;
aoqi@0 340 if(c.isElement()) {
aoqi@0 341 types.add(c.asElement());
aoqi@0 342 result = true;
aoqi@0 343 }
aoqi@0 344
aoqi@0 345 // look for other possible types
aoqi@0 346 for( ClassInfo<T,C> ci : parent.owner.beans().values() ) {
aoqi@0 347 if(ci.isElement() && nav.isSubClassOf(ci.getType(),type)) {
aoqi@0 348 types.add(ci.asElement());
aoqi@0 349 result = true;
aoqi@0 350 }
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 // don't allow local elements to substitute.
aoqi@0 354 for( ElementInfo<T,C> ei : parent.owner.getElementMappings(null).values()) {
aoqi@0 355 if(nav.isSubClassOf(ei.getType(),type)) {
aoqi@0 356 types.add(ei);
aoqi@0 357 result = true;
aoqi@0 358 }
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 return result;
aoqi@0 362 }
aoqi@0 363
aoqi@0 364
aoqi@0 365 @Override
aoqi@0 366 protected void link() {
aoqi@0 367 super.link();
aoqi@0 368
aoqi@0 369 // until we get the whole thing into TypeInfoSet,
aoqi@0 370 // we never really know what are all the possible types that can be assigned on this field.
aoqi@0 371 // so recompute this value when we have all the information.
aoqi@0 372 calcTypes(true);
aoqi@0 373
aoqi@0 374 }
aoqi@0 375
aoqi@0 376 public final void addType(PropertyInfoImpl<T,C,F,M> info) {
aoqi@0 377 //noinspection unchecked
aoqi@0 378 subTypes.add((ReferencePropertyInfoImpl)info);
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 public final boolean isMixed() {
aoqi@0 382 return isMixed;
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 public final WildcardMode getWildcard() {
aoqi@0 386 return wildcard;
aoqi@0 387 }
aoqi@0 388
aoqi@0 389 public final C getDOMHandler() {
aoqi@0 390 return domHandler;
aoqi@0 391 }
aoqi@0 392 }

mercurial