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/PropertyInfoImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,386 @@ 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.util.Collection; 1.32 +import java.lang.annotation.Annotation; 1.33 + 1.34 +import javax.activation.MimeType; 1.35 +import javax.xml.bind.annotation.XmlAttachmentRef; 1.36 +import javax.xml.bind.annotation.XmlElement; 1.37 +import javax.xml.bind.annotation.XmlElementWrapper; 1.38 +import javax.xml.bind.annotation.XmlID; 1.39 +import javax.xml.bind.annotation.XmlIDREF; 1.40 +import javax.xml.bind.annotation.XmlInlineBinaryData; 1.41 +import javax.xml.bind.annotation.XmlMimeType; 1.42 +import javax.xml.bind.annotation.XmlSchema; 1.43 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 1.44 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters; 1.45 +import javax.xml.bind.annotation.adapters.XmlAdapter; 1.46 +import javax.xml.namespace.QName; 1.47 + 1.48 +import com.sun.xml.internal.bind.v2.TODO; 1.49 +import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; 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.ID; 1.53 +import com.sun.xml.internal.bind.v2.model.core.PropertyInfo; 1.54 +import com.sun.xml.internal.bind.v2.model.core.TypeInfo; 1.55 +import com.sun.xml.internal.bind.v2.model.core.TypeInfoSet; 1.56 +import com.sun.xml.internal.bind.v2.model.nav.Navigator; 1.57 +import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; 1.58 +import com.sun.xml.internal.bind.v2.runtime.Location; 1.59 +import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapter; 1.60 + 1.61 +/** 1.62 + * Default partial implementation for {@link PropertyInfo}. 1.63 + * 1.64 + * @author Kohsuke Kawaguchi 1.65 + */ 1.66 +abstract class PropertyInfoImpl<T,C,F,M> 1.67 + implements PropertyInfo<T,C>, Locatable, Comparable<PropertyInfoImpl> /*by their names*/ { 1.68 + 1.69 + /** 1.70 + * Object that reads annotations. 1.71 + */ 1.72 + protected final PropertySeed<T,C,F,M> seed; 1.73 + 1.74 + private final boolean isCollection; 1.75 + 1.76 + private final ID id; 1.77 + 1.78 + private final MimeType expectedMimeType; 1.79 + private final boolean inlineBinary; 1.80 + private final QName schemaType; 1.81 + 1.82 + protected final ClassInfoImpl<T,C,F,M> parent; 1.83 + 1.84 + private final Adapter<T,C> adapter; 1.85 + 1.86 + protected PropertyInfoImpl(ClassInfoImpl<T,C,F,M> parent, PropertySeed<T,C,F,M> spi) { 1.87 + this.seed = spi; 1.88 + this.parent = parent; 1.89 + 1.90 + if(parent==null) 1.91 + /* 1.92 + Various people reported a bug where this parameter is somehow null. 1.93 + In an attempt to catch the error better, let's do an explicit check here. 1.94 + 1.95 + http://forums.java.net/jive/thread.jspa?threadID=18479 1.96 + http://forums.java.net/jive/thread.jspa?messageID=165946 1.97 + */ 1.98 + throw new AssertionError(); 1.99 + 1.100 + MimeType mt = Util.calcExpectedMediaType(seed,parent.builder); 1.101 + if(mt!=null && !kind().canHaveXmlMimeType) { 1.102 + parent.builder.reportError(new IllegalAnnotationException( 1.103 + Messages.ILLEGAL_ANNOTATION.format(XmlMimeType.class.getName()), 1.104 + seed.readAnnotation(XmlMimeType.class) 1.105 + )); 1.106 + mt = null; 1.107 + } 1.108 + this.expectedMimeType = mt; 1.109 + this.inlineBinary = seed.hasAnnotation(XmlInlineBinaryData.class); 1.110 + 1.111 + T t = seed.getRawType(); 1.112 + 1.113 + // check if there's an adapter applicable to the whole property 1.114 + XmlJavaTypeAdapter xjta = getApplicableAdapter(t); 1.115 + if(xjta!=null) { 1.116 + isCollection = false; 1.117 + adapter = new Adapter<T,C>(xjta,reader(),nav()); 1.118 + } else { 1.119 + // check if the adapter is applicable to the individual item in the property 1.120 + 1.121 + this.isCollection = nav().isSubClassOf(t, nav().ref(Collection.class)) 1.122 + || nav().isArrayButNotByteArray(t); 1.123 + 1.124 + xjta = getApplicableAdapter(getIndividualType()); 1.125 + if(xjta==null) { 1.126 + // ugly ugly hack, but we implement swaRef as adapter 1.127 + XmlAttachmentRef xsa = seed.readAnnotation(XmlAttachmentRef.class); 1.128 + if(xsa!=null) { 1.129 + parent.builder.hasSwaRef = true; 1.130 + adapter = new Adapter<T,C>(nav().asDecl(SwaRefAdapter.class),nav()); 1.131 + } else { 1.132 + adapter = null; 1.133 + 1.134 + // if this field has adapter annotation but not applicable, 1.135 + // that must be an error of the user 1.136 + xjta = seed.readAnnotation(XmlJavaTypeAdapter.class); 1.137 + if(xjta!=null) { 1.138 + T ad = reader().getClassValue(xjta,"value"); 1.139 + parent.builder.reportError(new IllegalAnnotationException( 1.140 + Messages.UNMATCHABLE_ADAPTER.format( 1.141 + nav().getTypeName(ad), nav().getTypeName(t)), 1.142 + xjta 1.143 + )); 1.144 + } 1.145 + } 1.146 + } else { 1.147 + adapter = new Adapter<T,C>(xjta,reader(),nav()); 1.148 + } 1.149 + } 1.150 + 1.151 + this.id = calcId(); 1.152 + this.schemaType = Util.calcSchemaType(reader(),seed,parent.clazz, 1.153 + getIndividualType(),this); 1.154 + } 1.155 + 1.156 + 1.157 + public ClassInfoImpl<T,C,F,M> parent() { 1.158 + return parent; 1.159 + } 1.160 + 1.161 + protected final Navigator<T,C,F,M> nav() { 1.162 + return parent.nav(); 1.163 + } 1.164 + protected final AnnotationReader<T,C,F,M> reader() { 1.165 + return parent.reader(); 1.166 + } 1.167 + 1.168 + public T getRawType() { 1.169 + return seed.getRawType(); 1.170 + } 1.171 + 1.172 + public T getIndividualType() { 1.173 + if(adapter!=null) 1.174 + return adapter.defaultType; 1.175 + T raw = getRawType(); 1.176 + if(!isCollection()) { 1.177 + return raw; 1.178 + } else { 1.179 + if(nav().isArrayButNotByteArray(raw)) 1.180 + return nav().getComponentType(raw); 1.181 + 1.182 + T bt = nav().getBaseClass(raw, nav().asDecl(Collection.class) ); 1.183 + if(nav().isParameterizedType(bt)) 1.184 + return nav().getTypeArgument(bt,0); 1.185 + else 1.186 + return nav().ref(Object.class); 1.187 + } 1.188 + } 1.189 + 1.190 + public final String getName() { 1.191 + return seed.getName(); 1.192 + } 1.193 + 1.194 + /** 1.195 + * Checks if the given adapter is applicable to the declared property type. 1.196 + */ 1.197 + private boolean isApplicable(XmlJavaTypeAdapter jta, T declaredType ) { 1.198 + if(jta==null) return false; 1.199 + 1.200 + T type = reader().getClassValue(jta,"type"); 1.201 + if(nav().isSameType(declaredType, type)) 1.202 + return true; // for types explicitly marked in XmlJavaTypeAdapter.type() 1.203 + 1.204 + T ad = reader().getClassValue(jta,"value"); 1.205 + T ba = nav().getBaseClass(ad, nav().asDecl(XmlAdapter.class)); 1.206 + if(!nav().isParameterizedType(ba)) 1.207 + return true; // can't check type applicability. assume Object, which means applicable to any. 1.208 + T inMemType = nav().getTypeArgument(ba, 1); 1.209 + 1.210 + return nav().isSubClassOf(declaredType,inMemType); 1.211 + } 1.212 + 1.213 + private XmlJavaTypeAdapter getApplicableAdapter(T type) { 1.214 + XmlJavaTypeAdapter jta = seed.readAnnotation(XmlJavaTypeAdapter.class); 1.215 + if(jta!=null && isApplicable(jta,type)) 1.216 + return jta; 1.217 + 1.218 + // check the applicable adapters on the package 1.219 + XmlJavaTypeAdapters jtas = reader().getPackageAnnotation(XmlJavaTypeAdapters.class, parent.clazz, seed ); 1.220 + if(jtas!=null) { 1.221 + for (XmlJavaTypeAdapter xjta : jtas.value()) { 1.222 + if(isApplicable(xjta,type)) 1.223 + return xjta; 1.224 + } 1.225 + } 1.226 + jta = reader().getPackageAnnotation(XmlJavaTypeAdapter.class, parent.clazz, seed ); 1.227 + if(isApplicable(jta,type)) 1.228 + return jta; 1.229 + 1.230 + // then on the target class 1.231 + C refType = nav().asDecl(type); 1.232 + if(refType!=null) { 1.233 + jta = reader().getClassAnnotation(XmlJavaTypeAdapter.class, refType, seed ); 1.234 + if(jta!=null && isApplicable(jta,type)) // the one on the type always apply. 1.235 + return jta; 1.236 + } 1.237 + 1.238 + return null; 1.239 + } 1.240 + 1.241 + /** 1.242 + * This is the default implementation of the getAdapter method 1.243 + * defined on many of the {@link PropertyInfo}-derived classes. 1.244 + */ 1.245 + public Adapter<T,C> getAdapter() { 1.246 + return adapter; 1.247 + } 1.248 + 1.249 + 1.250 + public final String displayName() { 1.251 + return nav().getClassName(parent.getClazz())+'#'+getName(); 1.252 + } 1.253 + 1.254 + public final ID id() { 1.255 + return id; 1.256 + } 1.257 + 1.258 + private ID calcId() { 1.259 + if(seed.hasAnnotation(XmlID.class)) { 1.260 + // check the type 1.261 + if(!nav().isSameType(getIndividualType(), nav().ref(String.class))) 1.262 + parent.builder.reportError(new IllegalAnnotationException( 1.263 + Messages.ID_MUST_BE_STRING.format(getName()), seed ) 1.264 + ); 1.265 + return ID.ID; 1.266 + } else 1.267 + if(seed.hasAnnotation(XmlIDREF.class)) { 1.268 + return ID.IDREF; 1.269 + } else { 1.270 + return ID.NONE; 1.271 + } 1.272 + } 1.273 + 1.274 + public final MimeType getExpectedMimeType() { 1.275 + return expectedMimeType; 1.276 + } 1.277 + 1.278 + public final boolean inlineBinaryData() { 1.279 + return inlineBinary; 1.280 + } 1.281 + 1.282 + public final QName getSchemaType() { 1.283 + return schemaType; 1.284 + } 1.285 + 1.286 + public final boolean isCollection() { 1.287 + return isCollection; 1.288 + } 1.289 + 1.290 + /** 1.291 + * Called after all the {@link TypeInfo}s are collected into the governing {@link TypeInfoSet}. 1.292 + * 1.293 + * Derived class can do additional actions to complete the model. 1.294 + */ 1.295 + protected void link() { 1.296 + if(id==ID.IDREF) { 1.297 + // make sure that the refereced type has ID 1.298 + for (TypeInfo<T,C> ti : ref()) { 1.299 + if(!ti.canBeReferencedByIDREF()) 1.300 + parent.builder.reportError(new IllegalAnnotationException( 1.301 + Messages.INVALID_IDREF.format( 1.302 + parent.builder.nav.getTypeName(ti.getType())), this )); 1.303 + } 1.304 + } 1.305 + } 1.306 + 1.307 + /** 1.308 + * A {@link PropertyInfoImpl} is always referenced by its enclosing class, 1.309 + * so return that as the upstream. 1.310 + */ 1.311 + public Locatable getUpstream() { 1.312 + return parent; 1.313 + } 1.314 + 1.315 + public Location getLocation() { 1.316 + return seed.getLocation(); 1.317 + } 1.318 + 1.319 + 1.320 +// 1.321 +// 1.322 +// convenience methods for derived classes 1.323 +// 1.324 +// 1.325 + 1.326 + 1.327 + /** 1.328 + * Computes the tag name from a {@link XmlElement} by taking the defaulting into account. 1.329 + */ 1.330 + protected final QName calcXmlName(XmlElement e) { 1.331 + if(e!=null) 1.332 + return calcXmlName(e.namespace(),e.name()); 1.333 + else 1.334 + return calcXmlName("##default","##default"); 1.335 + } 1.336 + 1.337 + /** 1.338 + * Computes the tag name from a {@link XmlElementWrapper} by taking the defaulting into account. 1.339 + */ 1.340 + protected final QName calcXmlName(XmlElementWrapper e) { 1.341 + if(e!=null) 1.342 + return calcXmlName(e.namespace(),e.name()); 1.343 + else 1.344 + return calcXmlName("##default","##default"); 1.345 + } 1.346 + 1.347 + private QName calcXmlName(String uri,String local) { 1.348 + // compute the default 1.349 + TODO.checkSpec(); 1.350 + if(local.length()==0 || local.equals("##default")) 1.351 + local = seed.getName(); 1.352 + if(uri.equals("##default")) { 1.353 + XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this ); 1.354 + // JAX-RPC doesn't want the default namespace URI swapping to take effect to 1.355 + // local "unqualified" elements. UGLY. 1.356 + if(xs!=null) { 1.357 + switch(xs.elementFormDefault()) { 1.358 + case QUALIFIED: 1.359 + QName typeName = parent.getTypeName(); 1.360 + if(typeName!=null) 1.361 + uri = typeName.getNamespaceURI(); 1.362 + else 1.363 + uri = xs.namespace(); 1.364 + if(uri.length()==0) 1.365 + uri = parent.builder.defaultNsUri; 1.366 + break; 1.367 + case UNQUALIFIED: 1.368 + case UNSET: 1.369 + uri = ""; 1.370 + } 1.371 + } else { 1.372 + uri = ""; 1.373 + } 1.374 + } 1.375 + return new QName(uri.intern(),local.intern()); 1.376 + } 1.377 + 1.378 + public int compareTo(PropertyInfoImpl that) { 1.379 + return this.getName().compareTo(that.getName()); 1.380 + } 1.381 + 1.382 + public final <A extends Annotation> A readAnnotation(Class<A> annotationType) { 1.383 + return seed.readAnnotation(annotationType); 1.384 + } 1.385 + 1.386 + public final boolean hasAnnotation(Class<? extends Annotation> annotationType) { 1.387 + return seed.hasAnnotation(annotationType); 1.388 + } 1.389 +}