src/share/jaxws_classes/com/sun/xml/internal/bind/v2/model/impl/EnumLeafInfoImpl.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.bind.v2.model.impl;
aoqi@0 27
aoqi@0 28 import java.util.Iterator;
aoqi@0 29
aoqi@0 30 import javax.xml.bind.annotation.XmlEnum;
aoqi@0 31 import javax.xml.bind.annotation.XmlEnumValue;
aoqi@0 32 import javax.xml.bind.annotation.XmlRootElement;
aoqi@0 33 import javax.xml.namespace.QName;
aoqi@0 34
aoqi@0 35 import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
aoqi@0 36 import com.sun.xml.internal.bind.v2.model.core.EnumConstant;
aoqi@0 37 import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo;
aoqi@0 38 import com.sun.xml.internal.bind.v2.model.core.NonElement;
aoqi@0 39 import com.sun.xml.internal.bind.v2.model.core.Element;
aoqi@0 40 import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
aoqi@0 41 import com.sun.xml.internal.bind.v2.runtime.Location;
aoqi@0 42 import java.util.Collection;
aoqi@0 43 import javax.xml.bind.annotation.XmlSchemaType;
aoqi@0 44
aoqi@0 45 /**
aoqi@0 46 * {@link EnumLeafInfo} implementation.
aoqi@0 47 *
aoqi@0 48 * @author Kohsuke Kawaguchi
aoqi@0 49 */
aoqi@0 50 class EnumLeafInfoImpl<T,C,F,M> extends TypeInfoImpl<T,C,F,M>
aoqi@0 51 implements EnumLeafInfo<T,C>, Element<T,C>, Iterable<EnumConstantImpl<T,C,F,M>> {
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * The enum class whose information this object represents.
aoqi@0 55 */
aoqi@0 56 /*package*/ final C clazz;
aoqi@0 57
aoqi@0 58 NonElement<T,C> baseType;
aoqi@0 59
aoqi@0 60 private final T type;
aoqi@0 61
aoqi@0 62 /**
aoqi@0 63 * Can be null for anonymous types.
aoqi@0 64 */
aoqi@0 65 private final QName typeName;
aoqi@0 66
aoqi@0 67 /**
aoqi@0 68 * All the {@link EnumConstantImpl}s are linked in this list.
aoqi@0 69 */
aoqi@0 70 private EnumConstantImpl<T,C,F,M> firstConstant;
aoqi@0 71
aoqi@0 72 /**
aoqi@0 73 * If this enum is also bound to an element, that tag name.
aoqi@0 74 * Or else null.
aoqi@0 75 */
aoqi@0 76 private QName elementName;
aoqi@0 77
aoqi@0 78 /**
aoqi@0 79 * Used to recognize token vs string.
aoqi@0 80 */
aoqi@0 81 protected boolean tokenStringType;
aoqi@0 82
aoqi@0 83 /**
aoqi@0 84 * @param clazz
aoqi@0 85 * @param type
aoqi@0 86 * clazz and type should both point to the enum class
aoqi@0 87 * that this {@link EnumLeafInfo} represents.
aoqi@0 88 * Because of the type parameterization we have to take them separately.
aoqi@0 89 */
aoqi@0 90 public EnumLeafInfoImpl(ModelBuilder<T,C,F,M> builder,
aoqi@0 91 Locatable upstream, C clazz, T type ) {
aoqi@0 92 super(builder,upstream);
aoqi@0 93 this.clazz = clazz;
aoqi@0 94 this.type = type;
aoqi@0 95
aoqi@0 96 elementName = parseElementName(clazz);
aoqi@0 97
aoqi@0 98 // compute the type name
aoqi@0 99 // TODO: I guess it must be allowed for enums to have @XmlElement
aoqi@0 100 typeName = parseTypeName(clazz);
aoqi@0 101
aoqi@0 102 // locate the base type.
aoqi@0 103 // this can be done eagerly because there shouldn't be no cycle.
aoqi@0 104 XmlEnum xe = builder.reader.getClassAnnotation(XmlEnum.class, clazz, this);
aoqi@0 105 if(xe!=null) {
aoqi@0 106 T base = builder.reader.getClassValue(xe, "value");
aoqi@0 107 baseType = builder.getTypeInfo(base,this);
aoqi@0 108 } else {
aoqi@0 109 baseType = builder.getTypeInfo(builder.nav.ref(String.class),this);
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 /**
aoqi@0 114 * Build {@link EnumConstant}s and discover/report any error in it.
aoqi@0 115 */
aoqi@0 116 protected void calcConstants() {
aoqi@0 117 EnumConstantImpl<T,C,F,M> last = null;
aoqi@0 118
aoqi@0 119 // first check if we represent xs:token derived type
aoqi@0 120 Collection<? extends F> fields = nav().getDeclaredFields(clazz);
aoqi@0 121 for (F f : fields) {
aoqi@0 122 if (nav().isSameType(nav().getFieldType(f), nav().ref(String.class))) {
aoqi@0 123 XmlSchemaType schemaTypeAnnotation = builder.reader.getFieldAnnotation(XmlSchemaType.class, f, this);
aoqi@0 124 if (schemaTypeAnnotation != null) {
aoqi@0 125 if ("token".equals(schemaTypeAnnotation.name())) {
aoqi@0 126 tokenStringType = true;
aoqi@0 127 break;
aoqi@0 128 }
aoqi@0 129 };
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132 F[] constants = nav().getEnumConstants(clazz);
aoqi@0 133 for( int i=constants.length-1; i>=0; i-- ) {
aoqi@0 134 F constant = constants[i];
aoqi@0 135 String name = nav().getFieldName(constant);
aoqi@0 136 XmlEnumValue xev = builder.reader.getFieldAnnotation(XmlEnumValue.class, constant, this);
aoqi@0 137
aoqi@0 138 String literal;
aoqi@0 139 if(xev==null) literal = name;
aoqi@0 140 else literal = xev.value();
aoqi@0 141
aoqi@0 142 last = createEnumConstant(name,literal,constant,last);
aoqi@0 143 }
aoqi@0 144 this.firstConstant = last;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 protected EnumConstantImpl<T,C,F,M> createEnumConstant(String name, String literal, F constant, EnumConstantImpl<T,C,F,M> last) {
aoqi@0 148 return new EnumConstantImpl<T,C,F,M>(this, name, literal, last);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151
aoqi@0 152 public T getType() {
aoqi@0 153 return type;
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 /**
aoqi@0 157 *
aoqi@0 158 * @return true if enum is restriction/extension from xs:token type, otherwise false
aoqi@0 159 */
aoqi@0 160 public boolean isToken() {
aoqi@0 161 return tokenStringType;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Leaf-type cannot be referenced from IDREF.
aoqi@0 166 *
aoqi@0 167 * @deprecated
aoqi@0 168 * why are you calling a method whose return value is always known?
aoqi@0 169 */
aoqi@0 170 public final boolean canBeReferencedByIDREF() {
aoqi@0 171 return false;
aoqi@0 172 }
aoqi@0 173
aoqi@0 174 public QName getTypeName() {
aoqi@0 175 return typeName;
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 public C getClazz() {
aoqi@0 179 return clazz;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 public NonElement<T,C> getBaseType() {
aoqi@0 183 return baseType;
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 public boolean isSimpleType() {
aoqi@0 187 return true;
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 public Location getLocation() {
aoqi@0 191 return nav().getClassLocation(clazz);
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 public Iterable<? extends EnumConstantImpl<T,C,F,M>> getConstants() {
aoqi@0 195 if(firstConstant==null)
aoqi@0 196 calcConstants();
aoqi@0 197 return this;
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 @Override
aoqi@0 201 public void link() {
aoqi@0 202 // make sure we've computed constants
aoqi@0 203 getConstants();
aoqi@0 204 super.link();
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 /**
aoqi@0 208 * No substitution.
aoqi@0 209 *
aoqi@0 210 * @deprecated if you are invoking this method directly, there's something wrong.
aoqi@0 211 */
aoqi@0 212 public Element<T, C> getSubstitutionHead() {
aoqi@0 213 return null;
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 public QName getElementName() {
aoqi@0 217 return elementName;
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 public boolean isElement() {
aoqi@0 221 return elementName!=null;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 public Element<T,C> asElement() {
aoqi@0 225 if(isElement())
aoqi@0 226 return this;
aoqi@0 227 else
aoqi@0 228 return null;
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 /**
aoqi@0 232 * When a bean binds to an element, it's always through {@link XmlRootElement},
aoqi@0 233 * so this method always return null.
aoqi@0 234 *
aoqi@0 235 * @deprecated
aoqi@0 236 * you shouldn't be invoking this method on {@link ClassInfoImpl}.
aoqi@0 237 */
aoqi@0 238 public ClassInfo<T,C> getScope() {
aoqi@0 239 return null;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 public Iterator<EnumConstantImpl<T,C,F,M>> iterator() {
aoqi@0 243 return new Iterator<EnumConstantImpl<T,C,F,M>>() {
aoqi@0 244 private EnumConstantImpl<T,C,F,M> next = firstConstant;
aoqi@0 245 public boolean hasNext() {
aoqi@0 246 return next!=null;
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 public EnumConstantImpl<T,C,F,M> next() {
aoqi@0 250 EnumConstantImpl<T,C,F,M> r = next;
aoqi@0 251 next = next.next;
aoqi@0 252 return r;
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 public void remove() {
aoqi@0 256 throw new UnsupportedOperationException();
aoqi@0 257 }
aoqi@0 258 };
aoqi@0 259 }
aoqi@0 260 }

mercurial