1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/model/CElementInfo.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,297 @@ 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.tools.internal.xjc.model; 1.30 + 1.31 +import java.util.Collection; 1.32 +import java.util.Collections; 1.33 +import java.util.HashSet; 1.34 +import java.util.List; 1.35 +import java.util.Set; 1.36 + 1.37 +import javax.xml.bind.JAXBElement; 1.38 +import javax.xml.bind.annotation.XmlElement; 1.39 +import javax.xml.namespace.QName; 1.40 + 1.41 +import com.sun.codemodel.internal.JPackage; 1.42 +import com.sun.codemodel.internal.JType; 1.43 +import com.sun.istack.internal.Nullable; 1.44 +import static com.sun.tools.internal.xjc.model.CElementPropertyInfo.CollectionMode.NOT_REPEATED; 1.45 +import static com.sun.tools.internal.xjc.model.CElementPropertyInfo.CollectionMode.REPEATED_VALUE; 1.46 +import com.sun.tools.internal.xjc.model.nav.NClass; 1.47 +import com.sun.tools.internal.xjc.model.nav.NType; 1.48 +import com.sun.tools.internal.xjc.model.nav.NavigatorImpl; 1.49 +import com.sun.tools.internal.xjc.outline.Aspect; 1.50 +import com.sun.tools.internal.xjc.outline.Outline; 1.51 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIInlineBinaryData; 1.52 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIFactoryMethod; 1.53 +import com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder; 1.54 +import com.sun.tools.internal.xjc.reader.Ring; 1.55 +import com.sun.xml.internal.bind.v2.model.core.ElementInfo; 1.56 +import com.sun.xml.internal.xsom.XSElementDecl; 1.57 +import com.sun.xml.internal.xsom.XmlString; 1.58 + 1.59 +import org.xml.sax.Locator; 1.60 + 1.61 +/** 1.62 + * {@link ElementInfo} implementation for the compile-time model. 1.63 + * 1.64 + * <p> 1.65 + * As an NType, it represents the Java representation of this element 1.66 + * (either JAXBElement<T> or Foo). 1.67 + * 1.68 + * @author Kohsuke Kawaguchi 1.69 + */ 1.70 +public final class CElementInfo extends AbstractCElement 1.71 + implements ElementInfo<NType,NClass>, NType, CClassInfoParent { 1.72 + 1.73 + private final QName tagName; 1.74 + 1.75 + /** 1.76 + * Represents {@code JAXBElement<ContentType>}. 1.77 + */ 1.78 + private NType type; 1.79 + 1.80 + /** 1.81 + * If this element produces its own class, the short name of that class. 1.82 + * Otherwise null. 1.83 + */ 1.84 + private String className; 1.85 + 1.86 + /** 1.87 + * If this element is global, the element info is considered to be 1.88 + * package-level, and this points to the package in which this element 1.89 + * lives in. 1.90 + * 1.91 + * <p> 1.92 + * For local elements, this points to the parent {@link CClassInfo}. 1.93 + */ 1.94 + public final CClassInfoParent parent; 1.95 + 1.96 + private CElementInfo substitutionHead; 1.97 + 1.98 + /** 1.99 + * Lazily computed. 1.100 + */ 1.101 + private Set<CElementInfo> substitutionMembers; 1.102 + 1.103 + /** 1.104 + * {@link Model} that owns this object. 1.105 + */ 1.106 + private final Model model; 1.107 + 1.108 + private CElementPropertyInfo property; 1.109 + 1.110 + /** 1.111 + * Custom {@link #getSqueezedName() squeezed name}, if any. 1.112 + */ 1.113 + private /*almost final*/ @Nullable String squeezedName; 1.114 + 1.115 + /** 1.116 + * Creates an element in the given parent. 1.117 + * 1.118 + * <p> 1.119 + * When using this construction, {@link #initContentType(TypeUse, XSElementDecl, XmlString)} 1.120 + * must not be invoked. 1.121 + */ 1.122 + public CElementInfo(Model model,QName tagName, CClassInfoParent parent, TypeUse contentType, XmlString defaultValue, XSElementDecl source, CCustomizations customizations, Locator location ) { 1.123 + super(model,source,location,customizations); 1.124 + this.tagName = tagName; 1.125 + this.model = model; 1.126 + this.parent = parent; 1.127 + if(contentType!=null) 1.128 + initContentType(contentType, source, defaultValue); 1.129 + 1.130 + model.add(this); 1.131 + } 1.132 + 1.133 + /** 1.134 + * Creates an element with a class in the given parent. 1.135 + * 1.136 + * <p> 1.137 + * When using this construction, the caller must use 1.138 + * {@link #initContentType(TypeUse, XSElementDecl, XmlString)} to fill in the content type 1.139 + * later. 1.140 + * 1.141 + * This is to avoid a circular model construction dependency between buidling a type 1.142 + * inside an element and element itself. To build a content type, you need to have 1.143 + * {@link CElementInfo} for a parent, so we can't take it as a constructor parameter. 1.144 + */ 1.145 + public CElementInfo(Model model,QName tagName, CClassInfoParent parent, String className, CCustomizations customizations, Locator location ) { 1.146 + this(model,tagName,parent,null,null,null,customizations,location); 1.147 + this.className = className; 1.148 + } 1.149 + 1.150 + public void initContentType(TypeUse contentType, @Nullable XSElementDecl source, XmlString defaultValue) { 1.151 + assert this.property==null; // must not be called twice 1.152 + 1.153 + this.property = new CElementPropertyInfo("Value", 1.154 + contentType.isCollection()?REPEATED_VALUE:NOT_REPEATED, 1.155 + contentType.idUse(), 1.156 + contentType.getExpectedMimeType(), 1.157 + source,null,getLocator(),true); 1.158 + this.property.setAdapter(contentType.getAdapterUse()); 1.159 + BIInlineBinaryData.handle(source,property); 1.160 + property.getTypes().add(new CTypeRef(contentType.getInfo(),tagName,CTypeRef.getSimpleTypeName(source), true,defaultValue)); 1.161 + this.type = NavigatorImpl.createParameterizedType( 1.162 + NavigatorImpl.theInstance.ref(JAXBElement.class), 1.163 + getContentInMemoryType() ); 1.164 + 1.165 + BIFactoryMethod factoryMethod = Ring.get(BGMBuilder.class).getBindInfo(source).get(BIFactoryMethod.class); 1.166 + if(factoryMethod!=null) { 1.167 + factoryMethod.markAsAcknowledged(); 1.168 + this.squeezedName = factoryMethod.name; 1.169 + } 1.170 + 1.171 + } 1.172 + 1.173 + public final String getDefaultValue() { 1.174 + return getProperty().getTypes().get(0).getDefaultValue(); 1.175 + } 1.176 + 1.177 + public final JPackage _package() { 1.178 + return parent.getOwnerPackage(); 1.179 + } 1.180 + 1.181 + public CNonElement getContentType() { 1.182 + return getProperty().ref().get(0); 1.183 + } 1.184 + 1.185 + public NType getContentInMemoryType() { 1.186 + if(getProperty().getAdapter()==null) { 1.187 + NType itemType = getContentType().getType(); 1.188 + if(!property.isCollection()) 1.189 + return itemType; 1.190 + 1.191 + return NavigatorImpl.createParameterizedType(List.class,itemType); 1.192 + } else { 1.193 + return getProperty().getAdapter().customType; 1.194 + } 1.195 + } 1.196 + 1.197 + public CElementPropertyInfo getProperty() { 1.198 + return property; 1.199 + } 1.200 + 1.201 + public CClassInfo getScope() { 1.202 + if(parent instanceof CClassInfo) 1.203 + return (CClassInfo)parent; 1.204 + return null; 1.205 + } 1.206 + 1.207 + /** 1.208 + * @deprecated why are you calling a method that returns this? 1.209 + */ 1.210 + public NType getType() { 1.211 + return this; 1.212 + } 1.213 + 1.214 + public QName getElementName() { 1.215 + return tagName; 1.216 + } 1.217 + 1.218 + public JType toType(Outline o, Aspect aspect) { 1.219 + if(className==null) 1.220 + return type.toType(o,aspect); 1.221 + else 1.222 + return o.getElement(this).implClass; 1.223 + } 1.224 + 1.225 + /** 1.226 + * Returns the "squeezed name" of this element. 1.227 + * 1.228 + * @see CClassInfo#getSqueezedName() 1.229 + */ 1.230 + @XmlElement 1.231 + public String getSqueezedName() { 1.232 + if(squeezedName!=null) return squeezedName; 1.233 + 1.234 + StringBuilder b = new StringBuilder(); 1.235 + CClassInfo s = getScope(); 1.236 + if(s!=null) 1.237 + b.append(s.getSqueezedName()); 1.238 + if(className!=null) 1.239 + b.append(className); 1.240 + else 1.241 + b.append( model.getNameConverter().toClassName(tagName.getLocalPart())); 1.242 + return b.toString(); 1.243 + } 1.244 + 1.245 + public CElementInfo getSubstitutionHead() { 1.246 + return substitutionHead; 1.247 + } 1.248 + 1.249 + public Collection<CElementInfo> getSubstitutionMembers() { 1.250 + if(substitutionMembers==null) 1.251 + return Collections.emptyList(); 1.252 + else 1.253 + return substitutionMembers; 1.254 + } 1.255 + 1.256 + public void setSubstitutionHead(CElementInfo substitutionHead) { 1.257 + // don't set it twice 1.258 + assert this.substitutionHead==null; 1.259 + assert substitutionHead!=null; 1.260 + this.substitutionHead = substitutionHead; 1.261 + 1.262 + if(substitutionHead.substitutionMembers==null) 1.263 + substitutionHead.substitutionMembers = new HashSet<CElementInfo>(); 1.264 + substitutionHead.substitutionMembers.add(this); 1.265 + } 1.266 + 1.267 + public boolean isBoxedType() { 1.268 + return false; 1.269 + } 1.270 + 1.271 + public String fullName() { 1.272 + if(className==null) 1.273 + return type.fullName(); 1.274 + else { 1.275 + String r = parent.fullName(); 1.276 + if(r.length()==0) return className; 1.277 + else return r+'.'+className; 1.278 + } 1.279 + } 1.280 + 1.281 + public <T> T accept(Visitor<T> visitor) { 1.282 + return visitor.onElement(this); 1.283 + } 1.284 + 1.285 + public JPackage getOwnerPackage() { 1.286 + return parent.getOwnerPackage(); 1.287 + } 1.288 + 1.289 + public String shortName() { 1.290 + return className; 1.291 + } 1.292 + 1.293 + /** 1.294 + * True if this element has its own class 1.295 + * (as opposed to be represented as an instance of {@link JAXBElement}. 1.296 + */ 1.297 + public boolean hasClass() { 1.298 + return className!=null; 1.299 + } 1.300 +}