1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/ReferencePropertyInfoImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,392 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.model.impl; 1.30 + 1.31 +import java.util.Collections; 1.32 +import java.util.LinkedHashSet; 1.33 +import java.util.Set; 1.34 + 1.35 +import javax.xml.bind.JAXBElement; 1.36 +import javax.xml.bind.annotation.XmlAnyElement; 1.37 +import javax.xml.bind.annotation.XmlElementRef; 1.38 +import javax.xml.bind.annotation.XmlElementRefs; 1.39 +import javax.xml.bind.annotation.XmlMixed; 1.40 +import javax.xml.bind.annotation.XmlSchema; 1.41 +import javax.xml.bind.annotation.XmlNsForm; 1.42 +import javax.xml.namespace.QName; 1.43 + 1.44 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; 1.45 +import com.sun.xml.internal.bind.v2.model.core.ClassInfo; 1.46 +import com.sun.xml.internal.bind.v2.model.core.Element; 1.47 +import com.sun.xml.internal.bind.v2.model.core.ElementInfo; 1.48 +import com.sun.xml.internal.bind.v2.model.core.NonElement; 1.49 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 1.50 +import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo; 1.51 +import com.sun.xml.internal.bind.v2.model.core.WildcardMode; 1.52 +import com.sun.xml.internal.bind.v2.model.nav.Navigator; 1.53 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; 1.54 +import java.util.Iterator; 1.55 + 1.56 +/** 1.57 + * Implementation of {@link ReferencePropertyInfo}. 1.58 + * 1.59 + * @author Kohsuke Kawaguchi 1.60 + */ 1.61 +class ReferencePropertyInfoImpl<T,C,F,M> 1.62 + extends ERPropertyInfoImpl<T,C,F,M> 1.63 + implements ReferencePropertyInfo<T,C>, DummyPropertyInfo<T, C, F, M> 1.64 +{ 1.65 + /** 1.66 + * Lazily computed. 1.67 + * @see #getElements() 1.68 + */ 1.69 + private Set<Element<T,C>> types; 1.70 + private Set<ReferencePropertyInfoImpl<T,C,F,M>> subTypes = new LinkedHashSet<ReferencePropertyInfoImpl<T,C,F,M>>(); 1.71 + 1.72 + private final boolean isMixed; 1.73 + 1.74 + private final WildcardMode wildcard; 1.75 + private final C domHandler; 1.76 + /** 1.77 + * Lazily computed. 1.78 + * @see #isRequired() 1.79 + */ 1.80 + private Boolean isRequired; 1.81 + 1.82 + public ReferencePropertyInfoImpl( 1.83 + ClassInfoImpl<T,C,F,M> classInfo, 1.84 + PropertySeed<T,C,F,M> seed) { 1.85 + 1.86 + super(classInfo, seed); 1.87 + 1.88 + isMixed = seed.readAnnotation(XmlMixed.class) != null; 1.89 + 1.90 + XmlAnyElement xae = seed.readAnnotation(XmlAnyElement.class); 1.91 + if(xae==null) { 1.92 + wildcard = null; 1.93 + domHandler = null; 1.94 + } else { 1.95 + wildcard = xae.lax()?WildcardMode.LAX:WildcardMode.SKIP; 1.96 + domHandler = nav().asDecl(reader().getClassValue(xae,"value")); 1.97 + } 1.98 + } 1.99 + 1.100 + public Set<? extends Element<T,C>> ref() { 1.101 + return getElements(); 1.102 + } 1.103 + 1.104 + public PropertyKind kind() { 1.105 + return PropertyKind.REFERENCE; 1.106 + } 1.107 + 1.108 + public Set<? extends Element<T,C>> getElements() { 1.109 + if(types==null) 1.110 + calcTypes(false); 1.111 + assert types!=null; 1.112 + return types; 1.113 + } 1.114 + 1.115 + /** 1.116 + * Compute {@link #types}. 1.117 + * 1.118 + * @param last 1.119 + * if true, every {@link XmlElementRef} must yield at least one type. 1.120 + */ 1.121 + private void calcTypes(boolean last) { 1.122 + XmlElementRef[] ann; 1.123 + types = new LinkedHashSet<Element<T,C>>(); 1.124 + XmlElementRefs refs = seed.readAnnotation(XmlElementRefs.class); 1.125 + XmlElementRef ref = seed.readAnnotation(XmlElementRef.class); 1.126 + 1.127 + if(refs!=null && ref!=null) { 1.128 + parent.builder.reportError(new IllegalAnnotationException( 1.129 + Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format( 1.130 + nav().getClassName(parent.getClazz())+'#'+seed.getName(), 1.131 + ref.annotationType().getName(), refs.annotationType().getName()), 1.132 + ref, refs )); 1.133 + } 1.134 + 1.135 + if(refs!=null) 1.136 + ann = refs.value(); 1.137 + else { 1.138 + if(ref!=null) 1.139 + ann = new XmlElementRef[]{ref}; 1.140 + else 1.141 + ann = null; 1.142 + } 1.143 + 1.144 + isRequired = !isCollection(); // this is by default, to remain compatible with 2.1 1.145 + 1.146 + if(ann!=null) { 1.147 + Navigator<T,C,F,M> nav = nav(); 1.148 + AnnotationReader<T,C,F,M> reader = reader(); 1.149 + 1.150 + final T defaultType = nav.ref(XmlElementRef.DEFAULT.class); 1.151 + final C je = nav.asDecl(JAXBElement.class); 1.152 + 1.153 + for( XmlElementRef r : ann ) { 1.154 + boolean yield; 1.155 + T type = reader.getClassValue(r,"type"); 1.156 + if(nav().isSameType(type, defaultType)) 1.157 + type = nav.erasure(getIndividualType()); 1.158 + if(nav.getBaseClass(type,je)!=null) 1.159 + yield = addGenericElement(r); 1.160 + else 1.161 + yield = addAllSubtypes(type); 1.162 + 1.163 + // essentially "isRequired &= isRequired(r)" except that we'd like to skip evaluating isRequird(r) 1.164 + // if the value is already false. 1.165 + if(isRequired && !isRequired(r)) 1.166 + isRequired = false; 1.167 + 1.168 + if(last && !yield) { 1.169 + // a reference didn't produce any type. 1.170 + // diagnose the problem 1.171 + if(nav().isSameType(type, nav.ref(JAXBElement.class))) { 1.172 + // no XmlElementDecl 1.173 + parent.builder.reportError(new IllegalAnnotationException( 1.174 + Messages.NO_XML_ELEMENT_DECL.format( 1.175 + getEffectiveNamespaceFor(r), r.name()), 1.176 + this 1.177 + )); 1.178 + } else { 1.179 + parent.builder.reportError(new IllegalAnnotationException( 1.180 + Messages.INVALID_XML_ELEMENT_REF.format(type),this)); 1.181 + } 1.182 + 1.183 + // reporting one error would do. 1.184 + // often the element ref field is using @XmlElementRefs 1.185 + // to point to multiple JAXBElements. 1.186 + // reporting one error for each @XmlElemetnRef is thus often redundant. 1.187 + return; 1.188 + } 1.189 + } 1.190 + } 1.191 + 1.192 + for (ReferencePropertyInfoImpl<T, C, F, M> info : subTypes) { 1.193 + PropertySeed sd = info.seed; 1.194 + refs = sd.readAnnotation(XmlElementRefs.class); 1.195 + ref = sd.readAnnotation(XmlElementRef.class); 1.196 + 1.197 + if (refs != null && ref != null) { 1.198 + parent.builder.reportError(new IllegalAnnotationException( 1.199 + Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format( 1.200 + nav().getClassName(parent.getClazz())+'#'+seed.getName(), 1.201 + ref.annotationType().getName(), refs.annotationType().getName()), 1.202 + ref, refs )); 1.203 + } 1.204 + 1.205 + if (refs != null) { 1.206 + ann = refs.value(); 1.207 + } else { 1.208 + if (ref != null) { 1.209 + ann = new XmlElementRef[]{ref}; 1.210 + } else { 1.211 + ann = null; 1.212 + } 1.213 + } 1.214 + 1.215 + if (ann != null) { 1.216 + Navigator<T,C,F,M> nav = nav(); 1.217 + AnnotationReader<T,C,F,M> reader = reader(); 1.218 + 1.219 + final T defaultType = nav.ref(XmlElementRef.DEFAULT.class); 1.220 + final C je = nav.asDecl(JAXBElement.class); 1.221 + 1.222 + for( XmlElementRef r : ann ) { 1.223 + boolean yield; 1.224 + T type = reader.getClassValue(r,"type"); 1.225 + if (nav().isSameType(type, defaultType)) { 1.226 + type = nav.erasure(getIndividualType()); 1.227 + } 1.228 + if (nav.getBaseClass(type,je) != null) { 1.229 + yield = addGenericElement(r, info); 1.230 + 1.231 + } else { 1.232 + yield = addAllSubtypes(type); 1.233 + } 1.234 + 1.235 + if(last && !yield) { 1.236 + // a reference didn't produce any type. 1.237 + // diagnose the problem 1.238 + if(nav().isSameType(type, nav.ref(JAXBElement.class))) { 1.239 + // no XmlElementDecl 1.240 + parent.builder.reportError(new IllegalAnnotationException( 1.241 + Messages.NO_XML_ELEMENT_DECL.format( 1.242 + getEffectiveNamespaceFor(r), r.name()), 1.243 + this 1.244 + )); 1.245 + } else { 1.246 + parent.builder.reportError(new IllegalAnnotationException( 1.247 + Messages.INVALID_XML_ELEMENT_REF.format(),this)); 1.248 + } 1.249 + 1.250 + // reporting one error would do. 1.251 + // often the element ref field is using @XmlElementRefs 1.252 + // to point to multiple JAXBElements. 1.253 + // reporting one error for each @XmlElemetnRef is thus often redundant. 1.254 + return; 1.255 + } 1.256 + } 1.257 + } 1.258 + } 1.259 + 1.260 + types = Collections.unmodifiableSet(types); 1.261 + } 1.262 + 1.263 + public boolean isRequired() { 1.264 + if(isRequired==null) 1.265 + calcTypes(false); 1.266 + return isRequired; 1.267 + } 1.268 + 1.269 + /** 1.270 + * If we find out that we are working with 2.1 API, remember the fact so that 1.271 + * we don't waste time generating exceptions every time we call {@link #isRequired(XmlElementRef)}. 1.272 + */ 1.273 + private static boolean is2_2 = true; 1.274 + 1.275 + /** 1.276 + * Reads the value of {@code XmlElementRef.required()}. 1.277 + * 1.278 + * If we are working as 2.1 RI, this defaults to true. 1.279 + */ 1.280 + private boolean isRequired(XmlElementRef ref) { 1.281 + if(!is2_2) return true; 1.282 + 1.283 + try { 1.284 + return ref.required(); 1.285 + } catch(LinkageError e) { 1.286 + is2_2 = false; 1.287 + return true; // the value defaults to true 1.288 + } 1.289 + } 1.290 + 1.291 + /** 1.292 + * @return 1.293 + * true if the reference yields at least one type 1.294 + */ 1.295 + private boolean addGenericElement(XmlElementRef r) { 1.296 + String nsUri = getEffectiveNamespaceFor(r); 1.297 + // TODO: check spec. defaulting of localName. 1.298 + return addGenericElement(parent.owner.getElementInfo(parent.getClazz(),new QName(nsUri,r.name()))); 1.299 + } 1.300 + 1.301 + private boolean addGenericElement(XmlElementRef r, ReferencePropertyInfoImpl<T,C,F,M> info) { 1.302 + String nsUri = info.getEffectiveNamespaceFor(r); 1.303 + ElementInfo ei = parent.owner.getElementInfo(info.parent.getClazz(), new QName(nsUri, r.name())); 1.304 + types.add(ei); 1.305 + return true; 1.306 + } 1.307 + 1.308 + private String getEffectiveNamespaceFor(XmlElementRef r) { 1.309 + String nsUri = r.namespace(); 1.310 + 1.311 + XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this ); 1.312 + if(xs!=null && xs.attributeFormDefault()== XmlNsForm.QUALIFIED) { 1.313 + // JAX-RPC doesn't want the default namespace URI swapping to take effect to 1.314 + // local "unqualified" elements. UGLY. 1.315 + if(nsUri.length()==0) 1.316 + nsUri = parent.builder.defaultNsUri; 1.317 + } 1.318 + 1.319 + return nsUri; 1.320 + } 1.321 + 1.322 + private boolean addGenericElement(ElementInfo<T,C> ei) { 1.323 + if(ei==null) 1.324 + return false; 1.325 + types.add(ei); 1.326 + for( ElementInfo<T,C> subst : ei.getSubstitutionMembers() ) 1.327 + addGenericElement(subst); 1.328 + return true; 1.329 + } 1.330 + 1.331 + private boolean addAllSubtypes(T type) { 1.332 + Navigator<T,C,F,M> nav = nav(); 1.333 + 1.334 + // this allows the explicitly referenced type to be sucked in to the model 1.335 + NonElement<T,C> t = parent.builder.getClassInfo(nav.asDecl(type),this); 1.336 + if(!(t instanceof ClassInfo)) 1.337 + // this is leaf. 1.338 + return false; 1.339 + 1.340 + boolean result = false; 1.341 + 1.342 + ClassInfo<T,C> c = (ClassInfo<T,C>) t; 1.343 + if(c.isElement()) { 1.344 + types.add(c.asElement()); 1.345 + result = true; 1.346 + } 1.347 + 1.348 + // look for other possible types 1.349 + for( ClassInfo<T,C> ci : parent.owner.beans().values() ) { 1.350 + if(ci.isElement() && nav.isSubClassOf(ci.getType(),type)) { 1.351 + types.add(ci.asElement()); 1.352 + result = true; 1.353 + } 1.354 + } 1.355 + 1.356 + // don't allow local elements to substitute. 1.357 + for( ElementInfo<T,C> ei : parent.owner.getElementMappings(null).values()) { 1.358 + if(nav.isSubClassOf(ei.getType(),type)) { 1.359 + types.add(ei); 1.360 + result = true; 1.361 + } 1.362 + } 1.363 + 1.364 + return result; 1.365 + } 1.366 + 1.367 + 1.368 + @Override 1.369 + protected void link() { 1.370 + super.link(); 1.371 + 1.372 + // until we get the whole thing into TypeInfoSet, 1.373 + // we never really know what are all the possible types that can be assigned on this field. 1.374 + // so recompute this value when we have all the information. 1.375 + calcTypes(true); 1.376 + 1.377 + } 1.378 + 1.379 + public final void addType(PropertyInfoImpl<T,C,F,M> info) { 1.380 + //noinspection unchecked 1.381 + subTypes.add((ReferencePropertyInfoImpl)info); 1.382 + } 1.383 + 1.384 + public final boolean isMixed() { 1.385 + return isMixed; 1.386 + } 1.387 + 1.388 + public final WildcardMode getWildcard() { 1.389 + return wildcard; 1.390 + } 1.391 + 1.392 + public final C getDOMHandler() { 1.393 + return domHandler; 1.394 + } 1.395 +}