Thu, 12 Oct 2017 19:44:07 +0800
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.AbstractList; |
aoqi@0 | 29 | import java.util.Collections; |
aoqi@0 | 30 | import java.util.List; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import javax.xml.bind.annotation.XmlElement; |
aoqi@0 | 33 | import javax.xml.bind.annotation.XmlElements; |
aoqi@0 | 34 | import javax.xml.bind.annotation.XmlList; |
aoqi@0 | 35 | import javax.xml.namespace.QName; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import com.sun.istack.internal.FinalArrayList; |
aoqi@0 | 38 | import com.sun.xml.internal.bind.v2.model.core.ElementPropertyInfo; |
aoqi@0 | 39 | import com.sun.xml.internal.bind.v2.model.core.ID; |
aoqi@0 | 40 | import com.sun.xml.internal.bind.v2.model.core.PropertyKind; |
aoqi@0 | 41 | import com.sun.xml.internal.bind.v2.model.core.TypeInfo; |
aoqi@0 | 42 | import com.sun.xml.internal.bind.v2.model.core.TypeRef; |
aoqi@0 | 43 | import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException; |
aoqi@0 | 44 | |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * Common {@link ElementPropertyInfo} implementation used for both |
aoqi@0 | 47 | * Annotation Processing and runtime. |
aoqi@0 | 48 | * |
aoqi@0 | 49 | * @author Kohsuke Kawaguchi |
aoqi@0 | 50 | */ |
aoqi@0 | 51 | class ElementPropertyInfoImpl<TypeT,ClassDeclT,FieldT,MethodT> |
aoqi@0 | 52 | extends ERPropertyInfoImpl<TypeT,ClassDeclT,FieldT,MethodT> |
aoqi@0 | 53 | implements ElementPropertyInfo<TypeT,ClassDeclT> |
aoqi@0 | 54 | { |
aoqi@0 | 55 | /** |
aoqi@0 | 56 | * Lazily computed. |
aoqi@0 | 57 | * @see #getTypes() |
aoqi@0 | 58 | */ |
aoqi@0 | 59 | private List<TypeRefImpl<TypeT,ClassDeclT>> types; |
aoqi@0 | 60 | |
aoqi@0 | 61 | private final List<TypeInfo<TypeT,ClassDeclT>> ref = new AbstractList<TypeInfo<TypeT,ClassDeclT>>() { |
aoqi@0 | 62 | public TypeInfo<TypeT,ClassDeclT> get(int index) { |
aoqi@0 | 63 | return getTypes().get(index).getTarget(); |
aoqi@0 | 64 | } |
aoqi@0 | 65 | |
aoqi@0 | 66 | public int size() { |
aoqi@0 | 67 | return getTypes().size(); |
aoqi@0 | 68 | } |
aoqi@0 | 69 | }; |
aoqi@0 | 70 | |
aoqi@0 | 71 | /** |
aoqi@0 | 72 | * Lazily computed. |
aoqi@0 | 73 | * @see #isRequired() |
aoqi@0 | 74 | */ |
aoqi@0 | 75 | private Boolean isRequired; |
aoqi@0 | 76 | |
aoqi@0 | 77 | /** |
aoqi@0 | 78 | * @see #isValueList() |
aoqi@0 | 79 | */ |
aoqi@0 | 80 | private final boolean isValueList; |
aoqi@0 | 81 | |
aoqi@0 | 82 | ElementPropertyInfoImpl( |
aoqi@0 | 83 | ClassInfoImpl<TypeT,ClassDeclT,FieldT,MethodT> parent, |
aoqi@0 | 84 | PropertySeed<TypeT,ClassDeclT,FieldT,MethodT> propertySeed) { |
aoqi@0 | 85 | super(parent, propertySeed); |
aoqi@0 | 86 | |
aoqi@0 | 87 | isValueList = seed.hasAnnotation(XmlList.class); |
aoqi@0 | 88 | |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | public List<? extends TypeRefImpl<TypeT,ClassDeclT>> getTypes() { |
aoqi@0 | 92 | if(types==null) { |
aoqi@0 | 93 | types = new FinalArrayList<TypeRefImpl<TypeT,ClassDeclT>>(); |
aoqi@0 | 94 | XmlElement[] ann=null; |
aoqi@0 | 95 | |
aoqi@0 | 96 | XmlElement xe = seed.readAnnotation(XmlElement.class); |
aoqi@0 | 97 | XmlElements xes = seed.readAnnotation(XmlElements.class); |
aoqi@0 | 98 | |
aoqi@0 | 99 | if(xe!=null && xes!=null) { |
aoqi@0 | 100 | parent.builder.reportError(new IllegalAnnotationException( |
aoqi@0 | 101 | Messages.MUTUALLY_EXCLUSIVE_ANNOTATIONS.format( |
aoqi@0 | 102 | nav().getClassName(parent.getClazz())+'#'+seed.getName(), |
aoqi@0 | 103 | xe.annotationType().getName(), xes.annotationType().getName()), |
aoqi@0 | 104 | xe, xes )); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | isRequired = true; |
aoqi@0 | 108 | |
aoqi@0 | 109 | if(xe!=null) |
aoqi@0 | 110 | ann = new XmlElement[]{xe}; |
aoqi@0 | 111 | else |
aoqi@0 | 112 | if(xes!=null) |
aoqi@0 | 113 | ann = xes.value(); |
aoqi@0 | 114 | |
aoqi@0 | 115 | if(ann==null) { |
aoqi@0 | 116 | // default |
aoqi@0 | 117 | TypeT t = getIndividualType(); |
aoqi@0 | 118 | if(!nav().isPrimitive(t) || isCollection()) |
aoqi@0 | 119 | isRequired = false; |
aoqi@0 | 120 | // nillableness defaults to true if it's collection |
aoqi@0 | 121 | types.add(createTypeRef(calcXmlName((XmlElement)null),t,isCollection(),null)); |
aoqi@0 | 122 | } else { |
aoqi@0 | 123 | for( XmlElement item : ann ) { |
aoqi@0 | 124 | // TODO: handle defaulting in names. |
aoqi@0 | 125 | QName name = calcXmlName(item); |
aoqi@0 | 126 | TypeT type = reader().getClassValue(item, "type"); |
aoqi@0 | 127 | if (nav().isSameType(type, nav().ref(XmlElement.DEFAULT.class))) |
aoqi@0 | 128 | type = getIndividualType(); |
aoqi@0 | 129 | if((!nav().isPrimitive(type) || isCollection()) && !item.required()) |
aoqi@0 | 130 | isRequired = false; |
aoqi@0 | 131 | types.add(createTypeRef(name, type, item.nillable(), getDefaultValue(item.defaultValue()) )); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | } |
aoqi@0 | 134 | types = Collections.unmodifiableList(types); |
aoqi@0 | 135 | assert !types.contains(null); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | return types; |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | private String getDefaultValue(String value) { |
aoqi@0 | 141 | if(value.equals("\u0000")) |
aoqi@0 | 142 | return null; |
aoqi@0 | 143 | else |
aoqi@0 | 144 | return value; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | /** |
aoqi@0 | 148 | * Used by {@link PropertyInfoImpl} to create new instances of {@link TypeRef} |
aoqi@0 | 149 | */ |
aoqi@0 | 150 | protected TypeRefImpl<TypeT,ClassDeclT> createTypeRef(QName name,TypeT type,boolean isNillable,String defaultValue) { |
aoqi@0 | 151 | return new TypeRefImpl<TypeT,ClassDeclT>(this,name,type,isNillable,defaultValue); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | public boolean isValueList() { |
aoqi@0 | 155 | return isValueList; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | public boolean isRequired() { |
aoqi@0 | 159 | if(isRequired==null) |
aoqi@0 | 160 | getTypes(); // compute the value |
aoqi@0 | 161 | return isRequired; |
aoqi@0 | 162 | } |
aoqi@0 | 163 | |
aoqi@0 | 164 | public List<? extends TypeInfo<TypeT,ClassDeclT>> ref() { |
aoqi@0 | 165 | return ref; |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | public final PropertyKind kind() { |
aoqi@0 | 169 | return PropertyKind.ELEMENT; |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | protected void link() { |
aoqi@0 | 173 | super.link(); |
aoqi@0 | 174 | for (TypeRefImpl<TypeT, ClassDeclT> ref : getTypes() ) { |
aoqi@0 | 175 | ref.link(); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | if(isValueList()) { |
aoqi@0 | 179 | // ugly test, because IDREF's are represented as text on the wire, |
aoqi@0 | 180 | // it's OK to be a value list in that case. |
aoqi@0 | 181 | if(id()!= ID.IDREF) { |
aoqi@0 | 182 | // check if all the item types are simple types |
aoqi@0 | 183 | // this can't be done when we compute types because |
aoqi@0 | 184 | // not all TypeInfos are available yet |
aoqi@0 | 185 | for (TypeRefImpl<TypeT,ClassDeclT> ref : types) { |
aoqi@0 | 186 | if(!ref.getTarget().isSimpleType()) { |
aoqi@0 | 187 | parent.builder.reportError(new IllegalAnnotationException( |
aoqi@0 | 188 | Messages.XMLLIST_NEEDS_SIMPLETYPE.format( |
aoqi@0 | 189 | nav().getTypeName(ref.getTarget().getType())), this )); |
aoqi@0 | 190 | break; |
aoqi@0 | 191 | } |
aoqi@0 | 192 | } |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | if(!isCollection()) |
aoqi@0 | 196 | parent.builder.reportError(new IllegalAnnotationException( |
aoqi@0 | 197 | Messages.XMLLIST_ON_SINGLE_PROPERTY.format(), this |
aoqi@0 | 198 | )); |
aoqi@0 | 199 | } |
aoqi@0 | 200 | } |
aoqi@0 | 201 | } |