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/ElementInfoImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,430 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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.lang.annotation.Annotation; 1.32 +import java.util.Collection; 1.33 +import java.util.Collections; 1.34 +import java.util.List; 1.35 + 1.36 +import javax.activation.MimeType; 1.37 +import javax.xml.bind.JAXBElement; 1.38 +import javax.xml.bind.annotation.XmlAttachmentRef; 1.39 +import javax.xml.bind.annotation.XmlElementDecl; 1.40 +import javax.xml.bind.annotation.XmlID; 1.41 +import javax.xml.bind.annotation.XmlIDREF; 1.42 +import javax.xml.bind.annotation.XmlInlineBinaryData; 1.43 +import javax.xml.bind.annotation.XmlSchema; 1.44 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 1.45 +import javax.xml.namespace.QName; 1.46 + 1.47 +import com.sun.istack.internal.FinalArrayList; 1.48 +import com.sun.xml.internal.bind.v2.TODO; 1.49 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationSource; 1.50 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable; 1.51 +import com.sun.xml.internal.bind.v2.model.core.Adapter; 1.52 +import com.sun.xml.internal.bind.v2.model.core.ClassInfo; 1.53 +import com.sun.xml.internal.bind.v2.model.core.ElementInfo; 1.54 +import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo; 1.55 +import com.sun.xml.internal.bind.v2.model.core.ID; 1.56 +import com.sun.xml.internal.bind.v2.model.core.NonElement; 1.57 +import com.sun.xml.internal.bind.v2.model.core.PropertyInfo; 1.58 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 1.59 +import com.sun.xml.internal.bind.v2.model.core.TypeInfo; 1.60 +import com.sun.xml.internal.bind.v2.model.core.TypeRef; 1.61 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; 1.62 +import com.sun.xml.internal.bind.v2.runtime.Location; 1.63 +import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapter; 1.64 + 1.65 +/** 1.66 + * {@link ElementInfo} implementation. 1.67 + * 1.68 + * @author Kohsuke Kawaguchi 1.69 + */ 1.70 +class ElementInfoImpl<T,C,F,M> extends TypeInfoImpl<T,C,F,M> implements ElementInfo<T,C> { 1.71 + 1.72 + private final QName tagName; 1.73 + 1.74 + private final NonElement<T,C> contentType; 1.75 + 1.76 + private final T tOfJAXBElementT; 1.77 + 1.78 + private final T elementType; 1.79 + 1.80 + private final ClassInfo<T,C> scope; 1.81 + 1.82 + /** 1.83 + * Annotation that controls the binding. 1.84 + */ 1.85 + private final XmlElementDecl anno; 1.86 + 1.87 + /** 1.88 + * If this element can substitute another element, the element name. 1.89 + * @see #link() 1.90 + */ 1.91 + private ElementInfoImpl<T,C,F,M> substitutionHead; 1.92 + 1.93 + /** 1.94 + * Lazily constructed list of {@link ElementInfo}s that can substitute this element. 1.95 + * This could be null. 1.96 + * @see #link() 1.97 + */ 1.98 + private FinalArrayList<ElementInfoImpl<T,C,F,M>> substitutionMembers; 1.99 + 1.100 + /** 1.101 + * The factory method from which this mapping was created. 1.102 + */ 1.103 + private final M method; 1.104 + 1.105 + /** 1.106 + * If the content type is adapter, return that adapter. 1.107 + */ 1.108 + private final Adapter<T,C> adapter; 1.109 + 1.110 + private final boolean isCollection; 1.111 + 1.112 + private final ID id; 1.113 + 1.114 + private final PropertyImpl property; 1.115 + private final MimeType expectedMimeType; 1.116 + private final boolean inlineBinary; 1.117 + private final QName schemaType; 1.118 + 1.119 + /** 1.120 + * Singleton instance of {@link ElementPropertyInfo} for this element. 1.121 + */ 1.122 + protected class PropertyImpl implements 1.123 + ElementPropertyInfo<T,C>, 1.124 + TypeRef<T,C>, 1.125 + AnnotationSource { 1.126 + // 1.127 + // TypeRef impl 1.128 + // 1.129 + public NonElement<T,C> getTarget() { 1.130 + return contentType; 1.131 + } 1.132 + public QName getTagName() { 1.133 + return tagName; 1.134 + } 1.135 + 1.136 + public List<? extends TypeRef<T,C>> getTypes() { 1.137 + return Collections.singletonList(this); 1.138 + } 1.139 + 1.140 + public List<? extends NonElement<T,C>> ref() { 1.141 + return Collections.singletonList(contentType); 1.142 + } 1.143 + 1.144 + public QName getXmlName() { 1.145 + return tagName; 1.146 + } 1.147 + 1.148 + public boolean isCollectionRequired() { 1.149 + return false; 1.150 + } 1.151 + 1.152 + public boolean isCollectionNillable() { 1.153 + return true; 1.154 + } 1.155 + 1.156 + public boolean isNillable() { 1.157 + return true; 1.158 + } 1.159 + 1.160 + public String getDefaultValue() { 1.161 + String v = anno.defaultValue(); 1.162 + if(v.equals("\u0000")) 1.163 + return null; 1.164 + else 1.165 + return v; 1.166 + } 1.167 + 1.168 + public ElementInfoImpl<T,C,F,M> parent() { 1.169 + return ElementInfoImpl.this; 1.170 + } 1.171 + 1.172 + public String getName() { 1.173 + return "value"; 1.174 + } 1.175 + 1.176 + public String displayName() { 1.177 + return "JAXBElement#value"; 1.178 + } 1.179 + 1.180 + public boolean isCollection() { 1.181 + return isCollection; 1.182 + } 1.183 + 1.184 + /** 1.185 + * For {@link ElementInfo}s, a collection always means a list of values. 1.186 + */ 1.187 + public boolean isValueList() { 1.188 + return isCollection; 1.189 + } 1.190 + 1.191 + public boolean isRequired() { 1.192 + return true; 1.193 + } 1.194 + 1.195 + public PropertyKind kind() { 1.196 + return PropertyKind.ELEMENT; 1.197 + } 1.198 + 1.199 + public Adapter<T,C> getAdapter() { 1.200 + return adapter; 1.201 + } 1.202 + 1.203 + public ID id() { 1.204 + return id; 1.205 + } 1.206 + 1.207 + public MimeType getExpectedMimeType() { 1.208 + return expectedMimeType; 1.209 + } 1.210 + 1.211 + public QName getSchemaType() { 1.212 + return schemaType; 1.213 + } 1.214 + 1.215 + public boolean inlineBinaryData() { 1.216 + return inlineBinary; 1.217 + } 1.218 + 1.219 + public PropertyInfo<T,C> getSource() { 1.220 + return this; 1.221 + } 1.222 + 1.223 + // 1.224 + // 1.225 + // AnnotationSource impl 1.226 + // 1.227 + // 1.228 + public <A extends Annotation> A readAnnotation(Class<A> annotationType) { 1.229 + return reader().getMethodAnnotation(annotationType,method,ElementInfoImpl.this); 1.230 + } 1.231 + 1.232 + public boolean hasAnnotation(Class<? extends Annotation> annotationType) { 1.233 + return reader().hasMethodAnnotation(annotationType,method); 1.234 + } 1.235 + } 1.236 + 1.237 + /** 1.238 + * @param m 1.239 + * The factory method on ObjectFactory that comes with {@link XmlElementDecl}. 1.240 + */ 1.241 + public ElementInfoImpl(ModelBuilder<T,C,F,M> builder, 1.242 + RegistryInfoImpl<T,C,F,M> registry, M m ) throws IllegalAnnotationException { 1.243 + super(builder,registry); 1.244 + 1.245 + this.method = m; 1.246 + anno = reader().getMethodAnnotation( XmlElementDecl.class, m, this ); 1.247 + assert anno!=null; // the caller should check this 1.248 + assert anno instanceof Locatable; 1.249 + 1.250 + elementType = nav().getReturnType(m); 1.251 + T baseClass = nav().getBaseClass(elementType,nav().asDecl(JAXBElement.class)); 1.252 + if(baseClass==null) 1.253 + throw new IllegalAnnotationException( 1.254 + Messages.XML_ELEMENT_MAPPING_ON_NON_IXMLELEMENT_METHOD.format(nav().getMethodName(m)), 1.255 + anno ); 1.256 + 1.257 + tagName = parseElementName(anno); 1.258 + T[] methodParams = nav().getMethodParameters(m); 1.259 + 1.260 + // adapter 1.261 + Adapter<T,C> a = null; 1.262 + if(methodParams.length>0) { 1.263 + XmlJavaTypeAdapter adapter = reader().getMethodAnnotation(XmlJavaTypeAdapter.class,m,this); 1.264 + if(adapter!=null) 1.265 + a = new Adapter<T,C>(adapter,reader(),nav()); 1.266 + else { 1.267 + XmlAttachmentRef xsa = reader().getMethodAnnotation(XmlAttachmentRef.class,m,this); 1.268 + if(xsa!=null) { 1.269 + TODO.prototype("in Annotation Processing swaRefAdapter isn't avaialble, so this returns null"); 1.270 + a = new Adapter<T,C>(owner.nav.asDecl(SwaRefAdapter.class),owner.nav); 1.271 + } 1.272 + } 1.273 + } 1.274 + this.adapter = a; 1.275 + 1.276 + // T of JAXBElement<T> 1.277 + tOfJAXBElementT = 1.278 + methodParams.length>0 ? methodParams[0] // this is more reliable, as it works even for ObjectFactory that sometimes have to return public types 1.279 + : nav().getTypeArgument(baseClass,0); // fall back to infer from the return type if no parameter. 1.280 + 1.281 + if(adapter==null) { 1.282 + T list = nav().getBaseClass(tOfJAXBElementT,nav().asDecl(List.class)); 1.283 + if(list==null) { 1.284 + isCollection = false; 1.285 + contentType = builder.getTypeInfo(tOfJAXBElementT,this); // suck this type into the current set. 1.286 + } else { 1.287 + isCollection = true; 1.288 + contentType = builder.getTypeInfo(nav().getTypeArgument(list,0),this); 1.289 + } 1.290 + } else { 1.291 + // but if adapted, use the adapted type 1.292 + contentType = builder.getTypeInfo(this.adapter.defaultType,this); 1.293 + isCollection = false; 1.294 + } 1.295 + 1.296 + // scope 1.297 + T s = reader().getClassValue(anno,"scope"); 1.298 + if(nav().isSameType(s, nav().ref(XmlElementDecl.GLOBAL.class))) 1.299 + scope = null; 1.300 + else { 1.301 + // TODO: what happens if there's an error? 1.302 + NonElement<T,C> scp = builder.getClassInfo(nav().asDecl(s),this); 1.303 + if(!(scp instanceof ClassInfo)) { 1.304 + throw new IllegalAnnotationException( 1.305 + Messages.SCOPE_IS_NOT_COMPLEXTYPE.format(nav().getTypeName(s)), 1.306 + anno ); 1.307 + } 1.308 + scope = (ClassInfo<T,C>)scp; 1.309 + } 1.310 + 1.311 + id = calcId(); 1.312 + 1.313 + property = createPropertyImpl(); 1.314 + 1.315 + this.expectedMimeType = Util.calcExpectedMediaType(property,builder); 1.316 + this.inlineBinary = reader().hasMethodAnnotation(XmlInlineBinaryData.class,method); 1.317 + this.schemaType = Util.calcSchemaType(reader(),property,registry.registryClass, 1.318 + getContentInMemoryType(),this); 1.319 + } 1.320 + 1.321 + final QName parseElementName(XmlElementDecl e) { 1.322 + String local = e.name(); 1.323 + String nsUri = e.namespace(); 1.324 + if(nsUri.equals("##default")) { 1.325 + // if defaulted ... 1.326 + XmlSchema xs = reader().getPackageAnnotation(XmlSchema.class, 1.327 + nav().getDeclaringClassForMethod(method),this); 1.328 + if(xs!=null) 1.329 + nsUri = xs.namespace(); 1.330 + else { 1.331 + nsUri = builder.defaultNsUri; 1.332 + } 1.333 + } 1.334 + 1.335 + return new QName(nsUri.intern(),local.intern()); 1.336 + } 1.337 + 1.338 + protected PropertyImpl createPropertyImpl() { 1.339 + return new PropertyImpl(); 1.340 + } 1.341 + 1.342 + public ElementPropertyInfo<T,C> getProperty() { 1.343 + return property; 1.344 + } 1.345 + 1.346 + public NonElement<T,C> getContentType() { 1.347 + return contentType; 1.348 + } 1.349 + 1.350 + public T getContentInMemoryType() { 1.351 + if(adapter==null) { 1.352 + return tOfJAXBElementT; 1.353 + } else { 1.354 + return adapter.customType; 1.355 + } 1.356 + } 1.357 + 1.358 + public QName getElementName() { 1.359 + return tagName; 1.360 + } 1.361 + 1.362 + public T getType() { 1.363 + return elementType; 1.364 + } 1.365 + 1.366 + /** 1.367 + * Leaf-type cannot be referenced from IDREF. 1.368 + * 1.369 + * @deprecated 1.370 + * why are you calling a method whose return value is always known? 1.371 + */ 1.372 + public final boolean canBeReferencedByIDREF() { 1.373 + return false; 1.374 + } 1.375 + 1.376 + private ID calcId() { 1.377 + // TODO: share code with PropertyInfoImpl 1.378 + if(reader().hasMethodAnnotation(XmlID.class,method)) { 1.379 + return ID.ID; 1.380 + } else 1.381 + if(reader().hasMethodAnnotation(XmlIDREF.class,method)) { 1.382 + return ID.IDREF; 1.383 + } else { 1.384 + return ID.NONE; 1.385 + } 1.386 + } 1.387 + 1.388 + public ClassInfo<T, C> getScope() { 1.389 + return scope; 1.390 + } 1.391 + 1.392 + public ElementInfo<T,C> getSubstitutionHead() { 1.393 + return substitutionHead; 1.394 + } 1.395 + 1.396 + public Collection<? extends ElementInfoImpl<T,C,F,M>> getSubstitutionMembers() { 1.397 + if(substitutionMembers==null) 1.398 + return Collections.emptyList(); 1.399 + else 1.400 + return substitutionMembers; 1.401 + } 1.402 + 1.403 + /** 1.404 + * Called after all the {@link TypeInfo}s are collected into the {@link #owner}. 1.405 + */ 1.406 + /*package*/ void link() { 1.407 + // substitution head 1.408 + if(anno.substitutionHeadName().length()!=0) { 1.409 + QName name = new QName( 1.410 + anno.substitutionHeadNamespace(), anno.substitutionHeadName() ); 1.411 + substitutionHead = owner.getElementInfo(null,name); 1.412 + if(substitutionHead==null) { 1.413 + builder.reportError( 1.414 + new IllegalAnnotationException(Messages.NON_EXISTENT_ELEMENT_MAPPING.format( 1.415 + name.getNamespaceURI(),name.getLocalPart()), anno)); 1.416 + // recover by ignoring this substitution declaration 1.417 + } else 1.418 + substitutionHead.addSubstitutionMember(this); 1.419 + } else 1.420 + substitutionHead = null; 1.421 + super.link(); 1.422 + } 1.423 + 1.424 + private void addSubstitutionMember(ElementInfoImpl<T,C,F,M> child) { 1.425 + if(substitutionMembers==null) 1.426 + substitutionMembers = new FinalArrayList<ElementInfoImpl<T,C,F,M>>(); 1.427 + substitutionMembers.add(child); 1.428 + } 1.429 + 1.430 + public Location getLocation() { 1.431 + return nav().getMethodLocation(method); 1.432 + } 1.433 +}