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/EnumLeafInfoImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,260 @@ 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.Iterator; 1.32 + 1.33 +import javax.xml.bind.annotation.XmlEnum; 1.34 +import javax.xml.bind.annotation.XmlEnumValue; 1.35 +import javax.xml.bind.annotation.XmlRootElement; 1.36 +import javax.xml.namespace.QName; 1.37 + 1.38 +import com.sun.xml.internal.bind.v2.model.annotation.Locatable; 1.39 +import com.sun.xml.internal.bind.v2.model.core.EnumConstant; 1.40 +import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo; 1.41 +import com.sun.xml.internal.bind.v2.model.core.NonElement; 1.42 +import com.sun.xml.internal.bind.v2.model.core.Element; 1.43 +import com.sun.xml.internal.bind.v2.model.core.ClassInfo; 1.44 +import com.sun.xml.internal.bind.v2.runtime.Location; 1.45 +import java.util.Collection; 1.46 +import javax.xml.bind.annotation.XmlSchemaType; 1.47 + 1.48 +/** 1.49 + * {@link EnumLeafInfo} implementation. 1.50 + * 1.51 + * @author Kohsuke Kawaguchi 1.52 + */ 1.53 +class EnumLeafInfoImpl<T,C,F,M> extends TypeInfoImpl<T,C,F,M> 1.54 + implements EnumLeafInfo<T,C>, Element<T,C>, Iterable<EnumConstantImpl<T,C,F,M>> { 1.55 + 1.56 + /** 1.57 + * The enum class whose information this object represents. 1.58 + */ 1.59 + /*package*/ final C clazz; 1.60 + 1.61 + NonElement<T,C> baseType; 1.62 + 1.63 + private final T type; 1.64 + 1.65 + /** 1.66 + * Can be null for anonymous types. 1.67 + */ 1.68 + private final QName typeName; 1.69 + 1.70 + /** 1.71 + * All the {@link EnumConstantImpl}s are linked in this list. 1.72 + */ 1.73 + private EnumConstantImpl<T,C,F,M> firstConstant; 1.74 + 1.75 + /** 1.76 + * If this enum is also bound to an element, that tag name. 1.77 + * Or else null. 1.78 + */ 1.79 + private QName elementName; 1.80 + 1.81 + /** 1.82 + * Used to recognize token vs string. 1.83 + */ 1.84 + protected boolean tokenStringType; 1.85 + 1.86 + /** 1.87 + * @param clazz 1.88 + * @param type 1.89 + * clazz and type should both point to the enum class 1.90 + * that this {@link EnumLeafInfo} represents. 1.91 + * Because of the type parameterization we have to take them separately. 1.92 + */ 1.93 + public EnumLeafInfoImpl(ModelBuilder<T,C,F,M> builder, 1.94 + Locatable upstream, C clazz, T type ) { 1.95 + super(builder,upstream); 1.96 + this.clazz = clazz; 1.97 + this.type = type; 1.98 + 1.99 + elementName = parseElementName(clazz); 1.100 + 1.101 + // compute the type name 1.102 + // TODO: I guess it must be allowed for enums to have @XmlElement 1.103 + typeName = parseTypeName(clazz); 1.104 + 1.105 + // locate the base type. 1.106 + // this can be done eagerly because there shouldn't be no cycle. 1.107 + XmlEnum xe = builder.reader.getClassAnnotation(XmlEnum.class, clazz, this); 1.108 + if(xe!=null) { 1.109 + T base = builder.reader.getClassValue(xe, "value"); 1.110 + baseType = builder.getTypeInfo(base,this); 1.111 + } else { 1.112 + baseType = builder.getTypeInfo(builder.nav.ref(String.class),this); 1.113 + } 1.114 + } 1.115 + 1.116 + /** 1.117 + * Build {@link EnumConstant}s and discover/report any error in it. 1.118 + */ 1.119 + protected void calcConstants() { 1.120 + EnumConstantImpl<T,C,F,M> last = null; 1.121 + 1.122 + // first check if we represent xs:token derived type 1.123 + Collection<? extends F> fields = nav().getDeclaredFields(clazz); 1.124 + for (F f : fields) { 1.125 + if (nav().isSameType(nav().getFieldType(f), nav().ref(String.class))) { 1.126 + XmlSchemaType schemaTypeAnnotation = builder.reader.getFieldAnnotation(XmlSchemaType.class, f, this); 1.127 + if (schemaTypeAnnotation != null) { 1.128 + if ("token".equals(schemaTypeAnnotation.name())) { 1.129 + tokenStringType = true; 1.130 + break; 1.131 + } 1.132 + }; 1.133 + } 1.134 + } 1.135 + F[] constants = nav().getEnumConstants(clazz); 1.136 + for( int i=constants.length-1; i>=0; i-- ) { 1.137 + F constant = constants[i]; 1.138 + String name = nav().getFieldName(constant); 1.139 + XmlEnumValue xev = builder.reader.getFieldAnnotation(XmlEnumValue.class, constant, this); 1.140 + 1.141 + String literal; 1.142 + if(xev==null) literal = name; 1.143 + else literal = xev.value(); 1.144 + 1.145 + last = createEnumConstant(name,literal,constant,last); 1.146 + } 1.147 + this.firstConstant = last; 1.148 + } 1.149 + 1.150 + protected EnumConstantImpl<T,C,F,M> createEnumConstant(String name, String literal, F constant, EnumConstantImpl<T,C,F,M> last) { 1.151 + return new EnumConstantImpl<T,C,F,M>(this, name, literal, last); 1.152 + } 1.153 + 1.154 + 1.155 + public T getType() { 1.156 + return type; 1.157 + } 1.158 + 1.159 + /** 1.160 + * 1.161 + * @return true if enum is restriction/extension from xs:token type, otherwise false 1.162 + */ 1.163 + public boolean isToken() { 1.164 + return tokenStringType; 1.165 + } 1.166 + 1.167 + /** 1.168 + * Leaf-type cannot be referenced from IDREF. 1.169 + * 1.170 + * @deprecated 1.171 + * why are you calling a method whose return value is always known? 1.172 + */ 1.173 + public final boolean canBeReferencedByIDREF() { 1.174 + return false; 1.175 + } 1.176 + 1.177 + public QName getTypeName() { 1.178 + return typeName; 1.179 + } 1.180 + 1.181 + public C getClazz() { 1.182 + return clazz; 1.183 + } 1.184 + 1.185 + public NonElement<T,C> getBaseType() { 1.186 + return baseType; 1.187 + } 1.188 + 1.189 + public boolean isSimpleType() { 1.190 + return true; 1.191 + } 1.192 + 1.193 + public Location getLocation() { 1.194 + return nav().getClassLocation(clazz); 1.195 + } 1.196 + 1.197 + public Iterable<? extends EnumConstantImpl<T,C,F,M>> getConstants() { 1.198 + if(firstConstant==null) 1.199 + calcConstants(); 1.200 + return this; 1.201 + } 1.202 + 1.203 + @Override 1.204 + public void link() { 1.205 + // make sure we've computed constants 1.206 + getConstants(); 1.207 + super.link(); 1.208 + } 1.209 + 1.210 + /** 1.211 + * No substitution. 1.212 + * 1.213 + * @deprecated if you are invoking this method directly, there's something wrong. 1.214 + */ 1.215 + public Element<T, C> getSubstitutionHead() { 1.216 + return null; 1.217 + } 1.218 + 1.219 + public QName getElementName() { 1.220 + return elementName; 1.221 + } 1.222 + 1.223 + public boolean isElement() { 1.224 + return elementName!=null; 1.225 + } 1.226 + 1.227 + public Element<T,C> asElement() { 1.228 + if(isElement()) 1.229 + return this; 1.230 + else 1.231 + return null; 1.232 + } 1.233 + 1.234 + /** 1.235 + * When a bean binds to an element, it's always through {@link XmlRootElement}, 1.236 + * so this method always return null. 1.237 + * 1.238 + * @deprecated 1.239 + * you shouldn't be invoking this method on {@link ClassInfoImpl}. 1.240 + */ 1.241 + public ClassInfo<T,C> getScope() { 1.242 + return null; 1.243 + } 1.244 + 1.245 + public Iterator<EnumConstantImpl<T,C,F,M>> iterator() { 1.246 + return new Iterator<EnumConstantImpl<T,C,F,M>>() { 1.247 + private EnumConstantImpl<T,C,F,M> next = firstConstant; 1.248 + public boolean hasNext() { 1.249 + return next!=null; 1.250 + } 1.251 + 1.252 + public EnumConstantImpl<T,C,F,M> next() { 1.253 + EnumConstantImpl<T,C,F,M> r = next; 1.254 + next = next.next; 1.255 + return r; 1.256 + } 1.257 + 1.258 + public void remove() { 1.259 + throw new UnsupportedOperationException(); 1.260 + } 1.261 + }; 1.262 + } 1.263 +}