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/CEnumLeafInfo.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,277 @@ 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 + 1.33 +import javax.activation.MimeType; 1.34 +import javax.xml.namespace.QName; 1.35 + 1.36 +import com.sun.codemodel.internal.JClass; 1.37 +import com.sun.codemodel.internal.JExpression; 1.38 +import com.sun.tools.internal.xjc.model.nav.NClass; 1.39 +import com.sun.tools.internal.xjc.model.nav.NType; 1.40 +import com.sun.tools.internal.xjc.outline.Aspect; 1.41 +import com.sun.tools.internal.xjc.outline.Outline; 1.42 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable; 1.43 +import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo; 1.44 +import com.sun.xml.internal.bind.v2.model.core.ID; 1.45 +import com.sun.xml.internal.bind.v2.model.core.NonElement; 1.46 +import com.sun.xml.internal.bind.v2.model.core.Element; 1.47 +import com.sun.xml.internal.bind.v2.runtime.Location; 1.48 +import com.sun.xml.internal.xsom.XSComponent; 1.49 +import com.sun.xml.internal.xsom.XmlString; 1.50 + 1.51 +import org.xml.sax.Locator; 1.52 + 1.53 +/** 1.54 + * Transducer that converts a string into an "enumeration class." 1.55 + * 1.56 + * The structure of the generated class needs to precisely 1.57 + * follow the JAXB spec. 1.58 + * 1.59 + * @author 1.60 + * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a> 1.61 + */ 1.62 +public final class CEnumLeafInfo implements EnumLeafInfo<NType,NClass>, NClass, CNonElement 1.63 +{ 1.64 + /** 1.65 + * The {@link Model} object to which this bean belongs. 1.66 + */ 1.67 + public final Model model; 1.68 + 1.69 + /** 1.70 + * The parent into which the enum class should be generated. 1.71 + */ 1.72 + public final CClassInfoParent parent; 1.73 + 1.74 + /** 1.75 + * Short name of the generated type-safe enum. 1.76 + */ 1.77 + public final String shortName; 1.78 + 1.79 + private final QName typeName; 1.80 + 1.81 + private final XSComponent source; 1.82 + 1.83 + /** 1.84 + * Represents the underlying type of this enumeration 1.85 + * and its conversion. 1.86 + * 1.87 + * <p> 1.88 + * To parse XML into a constant, we use the base type 1.89 + * to do lexical -> value, then use a map to pick up the right one. 1.90 + * 1.91 + * <p> 1.92 + * Hence this also represents the type of the Java value. 1.93 + * For example, if this is an enumeration of xs:int, 1.94 + * then this field will be Java int. 1.95 + */ 1.96 + public final CNonElement base; 1.97 + 1.98 + 1.99 + /** 1.100 + * List of enum members. 1.101 + */ 1.102 + public final Collection<CEnumConstant> members; 1.103 + 1.104 + private final CCustomizations customizations; 1.105 + 1.106 + /** 1.107 + * @see #getLocator() 1.108 + */ 1.109 + private final Locator sourceLocator; 1.110 + 1.111 + public String javadoc; 1.112 + 1.113 + public CEnumLeafInfo(Model model, 1.114 + QName typeName, 1.115 + CClassInfoParent container, 1.116 + String shortName, 1.117 + CNonElement base, 1.118 + Collection<CEnumConstant> _members, 1.119 + XSComponent source, 1.120 + CCustomizations customizations, 1.121 + Locator _sourceLocator) { 1.122 + this.model = model; 1.123 + this.parent = container; 1.124 + this.shortName = model.allocator.assignClassName(parent,shortName); 1.125 + this.base = base; 1.126 + this.members = _members; 1.127 + this.source = source; 1.128 + if(customizations==null) 1.129 + customizations = CCustomizations.EMPTY; 1.130 + this.customizations = customizations; 1.131 + this.sourceLocator = _sourceLocator; 1.132 + this.typeName = typeName; 1.133 + 1.134 + for( CEnumConstant mem : members ) 1.135 + mem.setParent(this); 1.136 + 1.137 + model.add(this); 1.138 + 1.139 + // TODO: can we take advantage of the fact that enum can be XmlRootElement? 1.140 + } 1.141 + 1.142 + /** 1.143 + * Source line information that points to the place 1.144 + * where this type-safe enum is defined. 1.145 + * Used to report error messages. 1.146 + */ 1.147 + public Locator getLocator() { 1.148 + return sourceLocator; 1.149 + } 1.150 + 1.151 + public QName getTypeName() { 1.152 + return typeName; 1.153 + } 1.154 + 1.155 + public NType getType() { 1.156 + return this; 1.157 + } 1.158 + 1.159 + /** 1.160 + * @deprecated 1.161 + * why are you calling the method whose return value is known? 1.162 + */ 1.163 + public boolean canBeReferencedByIDREF() { 1.164 + return false; 1.165 + } 1.166 + 1.167 + public boolean isElement() { 1.168 + return false; 1.169 + } 1.170 + 1.171 + public QName getElementName() { 1.172 + return null; 1.173 + } 1.174 + 1.175 + public Element<NType,NClass> asElement() { 1.176 + return null; 1.177 + } 1.178 + 1.179 + public NClass getClazz() { 1.180 + return this; 1.181 + } 1.182 + 1.183 + public XSComponent getSchemaComponent() { 1.184 + return source; 1.185 + } 1.186 + 1.187 + public JClass toType(Outline o, Aspect aspect) { 1.188 + return o.getEnum(this).clazz; 1.189 + } 1.190 + 1.191 + public boolean isAbstract() { 1.192 + return false; 1.193 + } 1.194 + 1.195 + public boolean isBoxedType() { 1.196 + return false; 1.197 + } 1.198 + 1.199 + public String fullName() { 1.200 + return parent.fullName()+'.'+shortName; 1.201 + } 1.202 + 1.203 + public boolean isPrimitive() { 1.204 + return false; 1.205 + } 1.206 + 1.207 + public boolean isSimpleType() { 1.208 + return true; 1.209 + } 1.210 + 1.211 + 1.212 + /** 1.213 + * The spec says the value field in the enum class will be generated 1.214 + * only under certain circumstances. 1.215 + * 1.216 + * @return 1.217 + * true if the generated enum class should have the value field. 1.218 + */ 1.219 + public boolean needsValueField() { 1.220 + for (CEnumConstant cec : members) { 1.221 + if(!cec.getName().equals(cec.getLexicalValue())) 1.222 + return true; 1.223 + } 1.224 + return false; 1.225 + } 1.226 + 1.227 + public JExpression createConstant(Outline outline, XmlString literal) { 1.228 + // correctly identifying which constant it maps to is hard, so 1.229 + // here I'm cheating 1.230 + JClass type = toType(outline,Aspect.EXPOSED); 1.231 + for (CEnumConstant mem : members) { 1.232 + if(mem.getLexicalValue().equals(literal.value)) 1.233 + return type.staticRef(mem.getName()); 1.234 + } 1.235 + return null; 1.236 + } 1.237 + 1.238 + @Deprecated 1.239 + public boolean isCollection() { 1.240 + return false; 1.241 + } 1.242 + 1.243 + @Deprecated 1.244 + public CAdapter getAdapterUse() { 1.245 + return null; 1.246 + } 1.247 + 1.248 + @Deprecated 1.249 + public CNonElement getInfo() { 1.250 + return this; 1.251 + } 1.252 + 1.253 + public ID idUse() { 1.254 + return ID.NONE; 1.255 + } 1.256 + 1.257 + public MimeType getExpectedMimeType() { 1.258 + return null; 1.259 + } 1.260 + 1.261 + public Collection<CEnumConstant> getConstants() { 1.262 + return members; 1.263 + } 1.264 + 1.265 + public NonElement<NType,NClass> getBaseType() { 1.266 + return base; 1.267 + } 1.268 + 1.269 + public CCustomizations getCustomizations() { 1.270 + return customizations; 1.271 + } 1.272 + 1.273 + public Locatable getUpstream() { 1.274 + throw new UnsupportedOperationException(); 1.275 + } 1.276 + 1.277 + public Location getLocation() { 1.278 + throw new UnsupportedOperationException(); 1.279 + } 1.280 +}