Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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.tools.internal.xjc.model; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.lang.annotation.Annotation; |
aoqi@0 | 29 | import java.util.Collection; |
aoqi@0 | 30 | import java.util.Map; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import javax.xml.XMLConstants; |
aoqi@0 | 33 | import javax.xml.bind.annotation.XmlTransient; |
aoqi@0 | 34 | import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
aoqi@0 | 35 | import javax.xml.namespace.QName; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import com.sun.codemodel.internal.JClass; |
aoqi@0 | 38 | import com.sun.codemodel.internal.JJavaName; |
aoqi@0 | 39 | import com.sun.codemodel.internal.JType; |
aoqi@0 | 40 | import com.sun.tools.internal.xjc.generator.bean.field.FieldRenderer; |
aoqi@0 | 41 | import com.sun.tools.internal.xjc.model.nav.NClass; |
aoqi@0 | 42 | import com.sun.tools.internal.xjc.model.nav.NType; |
aoqi@0 | 43 | import com.sun.tools.internal.xjc.reader.Ring; |
aoqi@0 | 44 | import com.sun.xml.internal.bind.api.impl.NameConverter; |
aoqi@0 | 45 | import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
aoqi@0 | 46 | import com.sun.xml.internal.bind.v2.model.core.PropertyInfo; |
aoqi@0 | 47 | import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil; |
aoqi@0 | 48 | import com.sun.xml.internal.xsom.XSComponent; |
aoqi@0 | 49 | |
aoqi@0 | 50 | import org.xml.sax.Locator; |
aoqi@0 | 51 | |
aoqi@0 | 52 | /** |
aoqi@0 | 53 | * Model of a property to be generated. |
aoqi@0 | 54 | * |
aoqi@0 | 55 | * @author Kohsuke Kawaguchi |
aoqi@0 | 56 | */ |
aoqi@0 | 57 | public abstract class CPropertyInfo implements PropertyInfo<NType,NClass>, CCustomizable { |
aoqi@0 | 58 | |
aoqi@0 | 59 | @XmlTransient |
aoqi@0 | 60 | private CClassInfo parent; |
aoqi@0 | 61 | private String privateName; |
aoqi@0 | 62 | private String publicName; |
aoqi@0 | 63 | private final boolean isCollection; |
aoqi@0 | 64 | |
aoqi@0 | 65 | @XmlTransient |
aoqi@0 | 66 | public final Locator locator; |
aoqi@0 | 67 | |
aoqi@0 | 68 | /** |
aoqi@0 | 69 | * @see #getSchemaComponent() |
aoqi@0 | 70 | */ |
aoqi@0 | 71 | private final XSComponent source; |
aoqi@0 | 72 | |
aoqi@0 | 73 | /** |
aoqi@0 | 74 | * If the base type of the property is overriden, |
aoqi@0 | 75 | * this field is set to non-null. |
aoqi@0 | 76 | */ |
aoqi@0 | 77 | public JType baseType; |
aoqi@0 | 78 | |
aoqi@0 | 79 | /** |
aoqi@0 | 80 | * Javadoc for this property. Must not be null. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public String javadoc=""; |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** |
aoqi@0 | 85 | * Property with {@link @XmlInlineBinaryData}. |
aoqi@0 | 86 | */ |
aoqi@0 | 87 | public boolean inlineBinaryData; |
aoqi@0 | 88 | |
aoqi@0 | 89 | /** |
aoqi@0 | 90 | * Specifies how the field is generated by the backend. |
aoqi@0 | 91 | */ |
aoqi@0 | 92 | @XmlJavaTypeAdapter(RuntimeUtil.ToStringAdapter.class) |
aoqi@0 | 93 | public FieldRenderer realization; |
aoqi@0 | 94 | |
aoqi@0 | 95 | /** |
aoqi@0 | 96 | * If non-null, keeps the default value in Java representation. |
aoqi@0 | 97 | * |
aoqi@0 | 98 | * If {@link #isCollection} is true, this field is always null, |
aoqi@0 | 99 | * for we don't handle default values for a list. |
aoqi@0 | 100 | */ |
aoqi@0 | 101 | public CDefaultValue defaultValue; |
aoqi@0 | 102 | |
aoqi@0 | 103 | private final CCustomizations customizations; |
aoqi@0 | 104 | |
aoqi@0 | 105 | protected CPropertyInfo(String name, boolean collection, XSComponent source, |
aoqi@0 | 106 | CCustomizations customizations, Locator locator) { |
aoqi@0 | 107 | this.publicName = name; |
aoqi@0 | 108 | String n = null; |
aoqi@0 | 109 | |
aoqi@0 | 110 | Model m = Ring.get(Model.class); |
aoqi@0 | 111 | if (m != null) { |
aoqi@0 | 112 | n = m.getNameConverter().toVariableName(name); |
aoqi@0 | 113 | } else { |
aoqi@0 | 114 | n = NameConverter.standard.toVariableName(name); |
aoqi@0 | 115 | } |
aoqi@0 | 116 | |
aoqi@0 | 117 | if(!JJavaName.isJavaIdentifier(n)) |
aoqi@0 | 118 | n = '_'+n; // avoid colliding with the reserved names like 'abstract'. |
aoqi@0 | 119 | this.privateName = n; |
aoqi@0 | 120 | |
aoqi@0 | 121 | this.isCollection = collection; |
aoqi@0 | 122 | this.locator = locator; |
aoqi@0 | 123 | if(customizations==null) |
aoqi@0 | 124 | this.customizations = CCustomizations.EMPTY; |
aoqi@0 | 125 | else |
aoqi@0 | 126 | this.customizations = customizations; |
aoqi@0 | 127 | this.source = source; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | // called from CClassInfo when added |
aoqi@0 | 131 | final void setParent( CClassInfo parent ) { |
aoqi@0 | 132 | assert this.parent==null; |
aoqi@0 | 133 | assert parent!=null; |
aoqi@0 | 134 | this.parent = parent; |
aoqi@0 | 135 | customizations.setParent(parent.model,this); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | public CTypeInfo parent() { |
aoqi@0 | 139 | return parent; |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | public Locator getLocator() { |
aoqi@0 | 143 | return locator; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | /** |
aoqi@0 | 147 | * If this model object is built from XML Schema, |
aoqi@0 | 148 | * this property returns a schema component from which the model is built. |
aoqi@0 | 149 | * |
aoqi@0 | 150 | * @return |
aoqi@0 | 151 | * null if the model is built from sources other than XML Schema |
aoqi@0 | 152 | * (such as DTD.) |
aoqi@0 | 153 | */ |
aoqi@0 | 154 | public final XSComponent getSchemaComponent() { |
aoqi@0 | 155 | return source; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | public abstract CAdapter getAdapter(); |
aoqi@0 | 159 | |
aoqi@0 | 160 | /** |
aoqi@0 | 161 | * Name of the property. |
aoqi@0 | 162 | * |
aoqi@0 | 163 | * <p> |
aoqi@0 | 164 | * This method is implemented to follow the contract of |
aoqi@0 | 165 | * {@link PropertyInfo#getName()}, and therefore it always |
aoqi@0 | 166 | * returns the name of the annotated field. |
aoqi@0 | 167 | * <p> |
aoqi@0 | 168 | * This name is normally not useful for the rest of XJC, |
aoqi@0 | 169 | * which usually wants to access the "public name" of the property. |
aoqi@0 | 170 | * A "public name" of the property is a name like "FooBar" which |
aoqi@0 | 171 | * is used as a seed for generating the accessor methods. |
aoqi@0 | 172 | * This is the name controlled by the schema customization via users. |
aoqi@0 | 173 | * |
aoqi@0 | 174 | * <p> |
aoqi@0 | 175 | * If the caller is calling this method statically, it's usually |
aoqi@0 | 176 | * the sign of a mistake. Use {@link #getName(boolean)} method instead, |
aoqi@0 | 177 | * which forces you to think about which name you want to get. |
aoqi@0 | 178 | * |
aoqi@0 | 179 | * @deprecated |
aoqi@0 | 180 | * marked as deprecated so that we can spot the use of this method. |
aoqi@0 | 181 | * |
aoqi@0 | 182 | * @see #getName(boolean) |
aoqi@0 | 183 | */ |
aoqi@0 | 184 | public String getName() { |
aoqi@0 | 185 | return getName(false); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | /** |
aoqi@0 | 189 | * Gets the name of the property. |
aoqi@0 | 190 | * |
aoqi@0 | 191 | * @param isPublic |
aoqi@0 | 192 | * if true, this method returns a name like "FooBar", which |
aoqi@0 | 193 | * should be used as a seed for generating user-visible names |
aoqi@0 | 194 | * (such as accessors like "getFooBar".) |
aoqi@0 | 195 | * |
aoqi@0 | 196 | * <p> |
aoqi@0 | 197 | * if false, this method returns the "name of the property" |
aoqi@0 | 198 | * as defined in the j2s side of the spec. This name is usually |
aoqi@0 | 199 | * something like "fooBar", which often corresponds to the XML |
aoqi@0 | 200 | * element/attribute name of this property (for taking advantage |
aoqi@0 | 201 | * of annotation defaulting as much as possible) |
aoqi@0 | 202 | */ |
aoqi@0 | 203 | public String getName(boolean isPublic) { |
aoqi@0 | 204 | return isPublic?publicName:privateName; |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | /** |
aoqi@0 | 208 | * Overrides the name of the property. |
aoqi@0 | 209 | * |
aoqi@0 | 210 | * This method can be used from {@link Plugin#postProcessModel(Model, ErrorHandler)}. |
aoqi@0 | 211 | * But the caller should do so with the understanding that this is inherently |
aoqi@0 | 212 | * dangerous method. |
aoqi@0 | 213 | */ |
aoqi@0 | 214 | public void setName(boolean isPublic, String newName) { |
aoqi@0 | 215 | if(isPublic) |
aoqi@0 | 216 | publicName = newName; |
aoqi@0 | 217 | else |
aoqi@0 | 218 | privateName = newName; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | public String displayName() { |
aoqi@0 | 222 | return parent.toString()+'#'+getName(false); |
aoqi@0 | 223 | } |
aoqi@0 | 224 | |
aoqi@0 | 225 | public boolean isCollection() { |
aoqi@0 | 226 | return isCollection; |
aoqi@0 | 227 | } |
aoqi@0 | 228 | |
aoqi@0 | 229 | |
aoqi@0 | 230 | public abstract Collection<? extends CTypeInfo> ref(); |
aoqi@0 | 231 | |
aoqi@0 | 232 | /** |
aoqi@0 | 233 | * Returns true if this property is "unboxable". |
aoqi@0 | 234 | * |
aoqi@0 | 235 | * <p> |
aoqi@0 | 236 | * In general, a property often has to be capable of representing null |
aoqi@0 | 237 | * to indicate the absence of the value. This requires properties |
aoqi@0 | 238 | * to be generated as <tt>@XmlElement Float f</tt>, not as |
aoqi@0 | 239 | * <tt>@XmlElement float f;</tt>. But this is slow. |
aoqi@0 | 240 | * |
aoqi@0 | 241 | * <p> |
aoqi@0 | 242 | * Fortunately, there are cases where we know that the property can |
aoqi@0 | 243 | * never legally be absent. When this condition holds we can generate |
aoqi@0 | 244 | * the optimized "unboxed form". |
aoqi@0 | 245 | * |
aoqi@0 | 246 | * <p> |
aoqi@0 | 247 | * The exact such conditions depend |
aoqi@0 | 248 | * on the kind of properties, so refer to the implementation code |
aoqi@0 | 249 | * for the details. |
aoqi@0 | 250 | * |
aoqi@0 | 251 | * <p> |
aoqi@0 | 252 | * This method returns true when the property can be generated |
aoqi@0 | 253 | * as "unboxed form", false otherwise. |
aoqi@0 | 254 | * |
aoqi@0 | 255 | * <p> |
aoqi@0 | 256 | * When this property is a collection, this method returns true |
aoqi@0 | 257 | * if items in the collection is unboxable. Obviously, the collection |
aoqi@0 | 258 | * itself is always a reference type. |
aoqi@0 | 259 | */ |
aoqi@0 | 260 | public boolean isUnboxable() { |
aoqi@0 | 261 | Collection<? extends CTypeInfo> ts = ref(); |
aoqi@0 | 262 | if(ts.size()!=1) |
aoqi@0 | 263 | // if the property is heterogeneous, no way. |
aoqi@0 | 264 | // ts.size()==0 is a special case that can happen for wildcards. |
aoqi@0 | 265 | return false; |
aoqi@0 | 266 | |
aoqi@0 | 267 | if(baseType!=null && (baseType instanceof JClass)) |
aoqi@0 | 268 | return false; |
aoqi@0 | 269 | |
aoqi@0 | 270 | CTypeInfo t = ts.iterator().next(); |
aoqi@0 | 271 | // only a primitive type is eligible. |
aoqi@0 | 272 | return t.getType().isBoxedType(); |
aoqi@0 | 273 | } |
aoqi@0 | 274 | |
aoqi@0 | 275 | /** |
aoqi@0 | 276 | * Returns true if this property needs to represent null |
aoqi@0 | 277 | * just for the purpose of representing an absence of the property. |
aoqi@0 | 278 | */ |
aoqi@0 | 279 | public boolean isOptionalPrimitive() { |
aoqi@0 | 280 | return false; |
aoqi@0 | 281 | } |
aoqi@0 | 282 | |
aoqi@0 | 283 | public CCustomizations getCustomizations() { |
aoqi@0 | 284 | return customizations; |
aoqi@0 | 285 | } |
aoqi@0 | 286 | |
aoqi@0 | 287 | public boolean inlineBinaryData() { |
aoqi@0 | 288 | return inlineBinaryData; |
aoqi@0 | 289 | } |
aoqi@0 | 290 | |
aoqi@0 | 291 | public abstract <V> V accept( CPropertyVisitor<V> visitor ); |
aoqi@0 | 292 | |
aoqi@0 | 293 | /** |
aoqi@0 | 294 | * Checks if the given {@link TypeUse} would need an explicit {@link XmlSchemaType} |
aoqi@0 | 295 | * annotation with the given type name. |
aoqi@0 | 296 | */ |
aoqi@0 | 297 | protected static boolean needsExplicitTypeName(TypeUse type, QName typeName) { |
aoqi@0 | 298 | if(typeName==null) |
aoqi@0 | 299 | // this is anonymous type. can't have @XmlSchemaType |
aoqi@0 | 300 | return false; |
aoqi@0 | 301 | |
aoqi@0 | 302 | if(!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(typeName.getNamespaceURI())) |
aoqi@0 | 303 | // if we put application-defined type name, it will be undefined |
aoqi@0 | 304 | // by the time we generate a schema. |
aoqi@0 | 305 | return false; |
aoqi@0 | 306 | |
aoqi@0 | 307 | if(type.isCollection()) |
aoqi@0 | 308 | // there's no built-in binding for a list simple type, |
aoqi@0 | 309 | // so any collection type always need @XmlSchemaType |
aoqi@0 | 310 | return true; |
aoqi@0 | 311 | |
aoqi@0 | 312 | QName itemType = type.getInfo().getTypeName(); |
aoqi@0 | 313 | if(itemType==null) |
aoqi@0 | 314 | // this is somewhat strange case, as it means the bound type is anonymous |
aoqi@0 | 315 | // but it's eventually derived by a named type and used. |
aoqi@0 | 316 | // but we can certainly use typeName as @XmlSchemaType value here |
aoqi@0 | 317 | return true; |
aoqi@0 | 318 | |
aoqi@0 | 319 | // if it's the default type name for this item, then no need |
aoqi@0 | 320 | return !itemType.equals(typeName); |
aoqi@0 | 321 | } |
aoqi@0 | 322 | |
aoqi@0 | 323 | /** |
aoqi@0 | 324 | * Puts the element names that this property possesses to the map, |
aoqi@0 | 325 | * so that we can find two properties that own the same element name, |
aoqi@0 | 326 | * which is an error. |
aoqi@0 | 327 | * |
aoqi@0 | 328 | * @return |
aoqi@0 | 329 | * null if no conflict was found. Otherwise return the QName that has the collision. |
aoqi@0 | 330 | */ |
aoqi@0 | 331 | public QName collectElementNames(Map<QName,CPropertyInfo> table) { |
aoqi@0 | 332 | return null; |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | public final <A extends Annotation> A readAnnotation(Class<A> annotationType) { |
aoqi@0 | 336 | throw new UnsupportedOperationException(); |
aoqi@0 | 337 | } |
aoqi@0 | 338 | |
aoqi@0 | 339 | public final boolean hasAnnotation(Class<? extends Annotation> annotationType) { |
aoqi@0 | 340 | throw new UnsupportedOperationException(); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | } |