Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
ohair@286 | 1 | /* |
ohair@286 | 2 | * Copyright (c) 1997, 2011, 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; |
ohair@286 | 67 | private Set<PropertyInfoImpl<T,C,F,M>> subTypes = new LinkedHashSet<PropertyInfoImpl<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 | |
ohair@286 | 189 | Iterator<PropertyInfoImpl<T,C,F,M>> i = subTypes.iterator(); |
ohair@286 | 190 | while (i.hasNext()) { |
ohair@286 | 191 | |
ohair@286 | 192 | ReferencePropertyInfoImpl<T,C,F,M> info = (ReferencePropertyInfoImpl<T, C, F, M>) i.next(); |
ohair@286 | 193 | PropertySeed sd = info.seed; |
ohair@286 | 194 | refs = sd.readAnnotation(XmlElementRefs.class); |
ohair@286 | 195 | ref = sd.readAnnotation(XmlElementRef.class); |
ohair@286 | 196 | |
ohair@286 | 197 | if (refs != null && ref != null) { |
ohair@286 | 198 | parent.builder.reportError(new IllegalAnnotationException( |
ohair@286 | 199 | Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format( |
ohair@286 | 200 | nav().getClassName(parent.getClazz())+'#'+seed.getName(), |
ohair@286 | 201 | ref.annotationType().getName(), refs.annotationType().getName()), |
ohair@286 | 202 | ref, refs )); |
ohair@286 | 203 | } |
ohair@286 | 204 | |
ohair@286 | 205 | if (refs != null) { |
ohair@286 | 206 | ann = refs.value(); |
ohair@286 | 207 | } else { |
ohair@286 | 208 | if (ref != null) { |
ohair@286 | 209 | ann = new XmlElementRef[]{ref}; |
ohair@286 | 210 | } else { |
ohair@286 | 211 | ann = null; |
ohair@286 | 212 | } |
ohair@286 | 213 | } |
ohair@286 | 214 | |
ohair@286 | 215 | if (ann != null) { |
ohair@286 | 216 | Navigator<T,C,F,M> nav = nav(); |
ohair@286 | 217 | AnnotationReader<T,C,F,M> reader = reader(); |
ohair@286 | 218 | |
ohair@286 | 219 | final T defaultType = nav.ref(XmlElementRef.DEFAULT.class); |
ohair@286 | 220 | final C je = nav.asDecl(JAXBElement.class); |
ohair@286 | 221 | |
ohair@286 | 222 | for( XmlElementRef r : ann ) { |
ohair@286 | 223 | boolean yield; |
ohair@286 | 224 | T type = reader.getClassValue(r,"type"); |
ohair@286 | 225 | if (nav().isSameType(type, defaultType)) { |
ohair@286 | 226 | type = nav.erasure(getIndividualType()); |
ohair@286 | 227 | } |
ohair@286 | 228 | if (nav.getBaseClass(type,je) != null) { |
ohair@286 | 229 | yield = addGenericElement(r, info); |
ohair@286 | 230 | |
ohair@286 | 231 | } else { |
ohair@286 | 232 | yield = addAllSubtypes(type); |
ohair@286 | 233 | } |
ohair@286 | 234 | |
ohair@286 | 235 | if(last && !yield) { |
ohair@286 | 236 | // a reference didn't produce any type. |
ohair@286 | 237 | // diagnose the problem |
ohair@286 | 238 | if(nav().isSameType(type, nav.ref(JAXBElement.class))) { |
ohair@286 | 239 | // no XmlElementDecl |
ohair@286 | 240 | parent.builder.reportError(new IllegalAnnotationException( |
ohair@286 | 241 | Messages.NO_XML_ELEMENT_DECL.format( |
ohair@286 | 242 | getEffectiveNamespaceFor(r), r.name()), |
ohair@286 | 243 | this |
ohair@286 | 244 | )); |
ohair@286 | 245 | } else { |
ohair@286 | 246 | parent.builder.reportError(new IllegalAnnotationException( |
ohair@286 | 247 | Messages.INVALID_XML_ELEMENT_REF.format(),this)); |
ohair@286 | 248 | } |
ohair@286 | 249 | |
ohair@286 | 250 | // reporting one error would do. |
ohair@286 | 251 | // often the element ref field is using @XmlElementRefs |
ohair@286 | 252 | // to point to multiple JAXBElements. |
ohair@286 | 253 | // reporting one error for each @XmlElemetnRef is thus often redundant. |
ohair@286 | 254 | return; |
ohair@286 | 255 | } |
ohair@286 | 256 | } |
ohair@286 | 257 | } |
ohair@286 | 258 | } |
ohair@286 | 259 | |
ohair@286 | 260 | types = Collections.unmodifiableSet(types); |
ohair@286 | 261 | } |
ohair@286 | 262 | |
ohair@286 | 263 | public boolean isRequired() { |
ohair@286 | 264 | if(isRequired==null) |
ohair@286 | 265 | calcTypes(false); |
ohair@286 | 266 | return isRequired; |
ohair@286 | 267 | } |
ohair@286 | 268 | |
ohair@286 | 269 | /** |
ohair@286 | 270 | * If we find out that we are working with 2.1 API, remember the fact so that |
ohair@286 | 271 | * we don't waste time generating exceptions every time we call {@link #isRequired(XmlElementRef)}. |
ohair@286 | 272 | */ |
ohair@286 | 273 | private static boolean is2_2 = true; |
ohair@286 | 274 | |
ohair@286 | 275 | /** |
ohair@286 | 276 | * Reads the value of {@code XmlElementRef.required()}. |
ohair@286 | 277 | * |
ohair@286 | 278 | * If we are working as 2.1 RI, this defaults to true. |
ohair@286 | 279 | */ |
ohair@286 | 280 | private boolean isRequired(XmlElementRef ref) { |
ohair@286 | 281 | if(!is2_2) return true; |
ohair@286 | 282 | |
ohair@286 | 283 | try { |
ohair@286 | 284 | return ref.required(); |
ohair@286 | 285 | } catch(LinkageError e) { |
ohair@286 | 286 | is2_2 = false; |
ohair@286 | 287 | return true; // the value defaults to true |
ohair@286 | 288 | } |
ohair@286 | 289 | } |
ohair@286 | 290 | |
ohair@286 | 291 | /** |
ohair@286 | 292 | * @return |
ohair@286 | 293 | * true if the reference yields at least one type |
ohair@286 | 294 | */ |
ohair@286 | 295 | private boolean addGenericElement(XmlElementRef r) { |
ohair@286 | 296 | String nsUri = getEffectiveNamespaceFor(r); |
ohair@286 | 297 | // TODO: check spec. defaulting of localName. |
ohair@286 | 298 | return addGenericElement(parent.owner.getElementInfo(parent.getClazz(),new QName(nsUri,r.name()))); |
ohair@286 | 299 | } |
ohair@286 | 300 | |
ohair@286 | 301 | private boolean addGenericElement(XmlElementRef r, ReferencePropertyInfoImpl<T,C,F,M> info) { |
ohair@286 | 302 | String nsUri = info.getEffectiveNamespaceFor(r); |
ohair@286 | 303 | ElementInfo ei = parent.owner.getElementInfo(info.parent.getClazz(), new QName(nsUri, r.name())); |
ohair@286 | 304 | types.add(ei); |
ohair@286 | 305 | return true; |
ohair@286 | 306 | } |
ohair@286 | 307 | |
ohair@286 | 308 | private String getEffectiveNamespaceFor(XmlElementRef r) { |
ohair@286 | 309 | String nsUri = r.namespace(); |
ohair@286 | 310 | |
ohair@286 | 311 | XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this ); |
ohair@286 | 312 | if(xs!=null && xs.attributeFormDefault()== XmlNsForm.QUALIFIED) { |
ohair@286 | 313 | // JAX-RPC doesn't want the default namespace URI swapping to take effect to |
ohair@286 | 314 | // local "unqualified" elements. UGLY. |
ohair@286 | 315 | if(nsUri.length()==0) |
ohair@286 | 316 | nsUri = parent.builder.defaultNsUri; |
ohair@286 | 317 | } |
ohair@286 | 318 | |
ohair@286 | 319 | return nsUri; |
ohair@286 | 320 | } |
ohair@286 | 321 | |
ohair@286 | 322 | private boolean addGenericElement(ElementInfo<T,C> ei) { |
ohair@286 | 323 | if(ei==null) |
ohair@286 | 324 | return false; |
ohair@286 | 325 | types.add(ei); |
ohair@286 | 326 | for( ElementInfo<T,C> subst : ei.getSubstitutionMembers() ) |
ohair@286 | 327 | addGenericElement(subst); |
ohair@286 | 328 | return true; |
ohair@286 | 329 | } |
ohair@286 | 330 | |
ohair@286 | 331 | private boolean addAllSubtypes(T type) { |
ohair@286 | 332 | Navigator<T,C,F,M> nav = nav(); |
ohair@286 | 333 | |
ohair@286 | 334 | // this allows the explicitly referenced type to be sucked in to the model |
ohair@286 | 335 | NonElement<T,C> t = parent.builder.getClassInfo(nav.asDecl(type),this); |
ohair@286 | 336 | if(!(t instanceof ClassInfo)) |
ohair@286 | 337 | // this is leaf. |
ohair@286 | 338 | return false; |
ohair@286 | 339 | |
ohair@286 | 340 | boolean result = false; |
ohair@286 | 341 | |
ohair@286 | 342 | ClassInfo<T,C> c = (ClassInfo<T,C>) t; |
ohair@286 | 343 | if(c.isElement()) { |
ohair@286 | 344 | types.add(c.asElement()); |
ohair@286 | 345 | result = true; |
ohair@286 | 346 | } |
ohair@286 | 347 | |
ohair@286 | 348 | // look for other possible types |
ohair@286 | 349 | for( ClassInfo<T,C> ci : parent.owner.beans().values() ) { |
ohair@286 | 350 | if(ci.isElement() && nav.isSubClassOf(ci.getType(),type)) { |
ohair@286 | 351 | types.add(ci.asElement()); |
ohair@286 | 352 | result = true; |
ohair@286 | 353 | } |
ohair@286 | 354 | } |
ohair@286 | 355 | |
ohair@286 | 356 | // don't allow local elements to substitute. |
ohair@286 | 357 | for( ElementInfo<T,C> ei : parent.owner.getElementMappings(null).values()) { |
ohair@286 | 358 | if(nav.isSubClassOf(ei.getType(),type)) { |
ohair@286 | 359 | types.add(ei); |
ohair@286 | 360 | result = true; |
ohair@286 | 361 | } |
ohair@286 | 362 | } |
ohair@286 | 363 | |
ohair@286 | 364 | return result; |
ohair@286 | 365 | } |
ohair@286 | 366 | |
ohair@286 | 367 | |
ohair@286 | 368 | @Override |
ohair@286 | 369 | protected void link() { |
ohair@286 | 370 | super.link(); |
ohair@286 | 371 | |
ohair@286 | 372 | // until we get the whole thing into TypeInfoSet, |
ohair@286 | 373 | // we never really know what are all the possible types that can be assigned on this field. |
ohair@286 | 374 | // so recompute this value when we have all the information. |
ohair@286 | 375 | calcTypes(true); |
ohair@286 | 376 | |
ohair@286 | 377 | } |
ohair@286 | 378 | |
ohair@286 | 379 | public final void addType(PropertyInfoImpl<T,C,F,M> info) { |
ohair@286 | 380 | subTypes.add(info); |
ohair@286 | 381 | } |
ohair@286 | 382 | |
ohair@286 | 383 | public final boolean isMixed() { |
ohair@286 | 384 | return isMixed; |
ohair@286 | 385 | } |
ohair@286 | 386 | |
ohair@286 | 387 | public final WildcardMode getWildcard() { |
ohair@286 | 388 | return wildcard; |
ohair@286 | 389 | } |
ohair@286 | 390 | |
ohair@286 | 391 | public final C getDOMHandler() { |
ohair@286 | 392 | return domHandler; |
ohair@286 | 393 | } |
ohair@286 | 394 | } |