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