src/share/jaxws_classes/com/sun/tools/internal/xjc/model/CPropertyInfo.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

     1 /*
     2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.internal.xjc.model;
    28 import java.lang.annotation.Annotation;
    29 import java.util.Collection;
    30 import java.util.Map;
    32 import javax.xml.bind.annotation.XmlSchemaType;
    33 import javax.xml.bind.annotation.XmlTransient;
    34 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    35 import javax.xml.namespace.QName;
    37 import com.sun.codemodel.internal.JClass;
    38 import com.sun.codemodel.internal.JJavaName;
    39 import com.sun.codemodel.internal.JType;
    40 import com.sun.tools.internal.xjc.Plugin;
    41 import com.sun.tools.internal.xjc.generator.bean.field.FieldRenderer;
    42 import com.sun.tools.internal.xjc.model.nav.NClass;
    43 import com.sun.tools.internal.xjc.model.nav.NType;
    44 import com.sun.xml.internal.bind.api.impl.NameConverter;
    45 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    46 import com.sun.xml.internal.bind.v2.model.core.PropertyInfo;
    47 import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil;
    48 import com.sun.xml.internal.xsom.XSComponent;
    50 import org.xml.sax.Locator;
    52 /**
    53  * Model of a property to be generated.
    54  *
    55  * @author Kohsuke Kawaguchi
    56  */
    57 public abstract class CPropertyInfo implements PropertyInfo<NType,NClass>, CCustomizable {
    59     @XmlTransient
    60     private CClassInfo parent;
    61     private String privateName;
    62     private String publicName;
    63     private final boolean isCollection;
    65     @XmlTransient
    66     public final Locator locator;
    68     /**
    69      * @see #getSchemaComponent()
    70      */
    71     private final XSComponent source;
    73     /**
    74      * If the base type of the property is overriden,
    75      * this field is set to non-null.
    76      */
    77     public JType baseType;
    79     /**
    80      * Javadoc for this property. Must not be null.
    81      */
    82     public String javadoc="";
    84     /**
    85      * Property with {@link @XmlInlineBinaryData}.
    86      */
    87     public boolean inlineBinaryData;
    89     /**
    90      * Specifies how the field is generated by the backend.
    91      */
    92     @XmlJavaTypeAdapter(RuntimeUtil.ToStringAdapter.class)
    93     public FieldRenderer realization;
    95     /**
    96      * If non-null, keeps the default value in Java representation.
    97      *
    98      * If {@link #isCollection} is true, this field is always null,
    99      * for we don't handle default values for a list.
   100      */
   101     public CDefaultValue defaultValue;
   103     private final CCustomizations customizations;
   105     protected CPropertyInfo(String name, boolean collection, XSComponent source,
   106                             CCustomizations customizations, Locator locator) {
   107         this.publicName = name;
   108         String n = NameConverter.standard.toVariableName(name);
   109         if(!JJavaName.isJavaIdentifier(n))
   110             n = '_'+n;  // avoid colliding with the reserved names like 'abstract'.
   111         this.privateName = n;
   113         this.isCollection = collection;
   114         this.locator = locator;
   115         if(customizations==null)
   116             this.customizations = CCustomizations.EMPTY;
   117         else
   118             this.customizations = customizations;
   119         this.source = source;
   120     }
   122     // called from CClassInfo when added
   123     final void setParent( CClassInfo parent ) {
   124         assert this.parent==null;
   125         assert parent!=null;
   126         this.parent = parent;
   127         customizations.setParent(parent.model,this);
   128     }
   130     public CTypeInfo parent() {
   131         return parent;
   132     }
   134     public Locator getLocator() {
   135         return locator;
   136     }
   138     /**
   139      * If this model object is built from XML Schema,
   140      * this property returns a schema component from which the model is built.
   141      *
   142      * @return
   143      *      null if the model is built from sources other than XML Schema
   144      *      (such as DTD.)
   145      */
   146     public final XSComponent getSchemaComponent() {
   147         return source;
   148     }
   150     public abstract CAdapter getAdapter();
   152     /**
   153      * Name of the property.
   154      *
   155      * <p>
   156      * This method is implemented to follow the contract of
   157      * {@link PropertyInfo#getName()}, and therefore it always
   158      * returns the name of the annotated field.
   159      * <p>
   160      * This name is normally not useful for the rest of XJC,
   161      * which usually wants to access the "public name" of the property.
   162      * A "public name" of the property is a name like "FooBar" which
   163      * is used as a seed for generating the accessor methods.
   164      * This is the name controlled by the schema customization via users.
   165      *
   166      * <p>
   167      * If the caller is calling this method statically, it's usually
   168      * the sign of a mistake. Use {@link #getName(boolean)} method instead,
   169      * which forces you to think about which name you want to get.
   170      *
   171      * @deprecated
   172      *      marked as deprecated so that we can spot the use of this method.
   173      *
   174      * @see #getName(boolean)
   175      */
   176     public String getName() {
   177         return getName(false);
   178     }
   180     /**
   181      * Gets the name of the property.
   182      *
   183      * @param isPublic
   184      *      if true, this method returns a name like "FooBar", which
   185      *      should be used as a seed for generating user-visible names
   186      *      (such as accessors like "getFooBar".)
   187      *
   188      *      <p>
   189      *      if false, this method returns the "name of the property"
   190      *      as defined in the j2s side of the spec. This name is usually
   191      *      something like "fooBar", which often corresponds to the XML
   192      *      element/attribute name of this property (for taking advantage
   193      *      of annotation defaulting as much as possible)
   194      */
   195     public String getName(boolean isPublic) {
   196         return isPublic?publicName:privateName;
   197     }
   199     /**
   200      * Overrides the name of the property.
   201      *
   202      * This method can be used from {@link Plugin#postProcessModel(Model, ErrorHandler)}.
   203      * But the caller should do so with the understanding that this is inherently
   204      * dangerous method.
   205      */
   206     public void setName(boolean isPublic, String newName) {
   207         if(isPublic)
   208             publicName = newName;
   209         else
   210             privateName = newName;
   211     }
   213     public String displayName() {
   214         return parent.toString()+'#'+getName(false);
   215     }
   217     public boolean isCollection() {
   218         return isCollection;
   219     }
   222     public abstract Collection<? extends CTypeInfo> ref();
   224     /**
   225      * Returns true if this property is "unboxable".
   226      *
   227      * <p>
   228      * In general, a property often has to be capable of representing null
   229      * to indicate the absence of the value. This requires properties
   230      * to be generated as <tt>@XmlElement Float f</tt>, not as
   231      * <tt>@XmlElement float f;</tt>. But this is slow.
   232      *
   233      * <p>
   234      * Fortunately, there are cases where we know that the property can
   235      * never legally be absent. When this condition holds we can generate
   236      * the optimized "unboxed form".
   237      *
   238      * <p>
   239      * The exact such conditions depend
   240      * on the kind of properties, so refer to the implementation code
   241      * for the details.
   242      *
   243      * <p>
   244      * This method returns true when the property can be generated
   245      * as "unboxed form", false otherwise.
   246      *
   247      * <p>
   248      * When this property is a collection, this method returns true
   249      * if items in the collection is unboxable. Obviously, the collection
   250      * itself is always a reference type.
   251      */
   252     public boolean isUnboxable() {
   253         Collection<? extends CTypeInfo> ts = ref();
   254         if(ts.size()!=1)
   255             // if the property is heterogeneous, no way.
   256             // ts.size()==0 is a special case that can happen for wildcards.
   257             return false;
   259         if(baseType!=null && (baseType instanceof JClass))
   260             return false;
   262         CTypeInfo t = ts.iterator().next();
   263         // only a primitive type is eligible.
   264         return t.getType().isBoxedType();
   265     }
   267     /**
   268      * Returns true if this property needs to represent null
   269      * just for the purpose of representing an absence of the property.
   270      */
   271     public boolean isOptionalPrimitive() {
   272         return false;
   273     }
   275     public CCustomizations getCustomizations() {
   276         return customizations;
   277     }
   279     public boolean inlineBinaryData() {
   280         return inlineBinaryData;
   281     }
   283     public abstract <V> V accept( CPropertyVisitor<V> visitor );
   285     /**
   286      * Checks if the given {@link TypeUse} would need an explicit {@link XmlSchemaType}
   287      * annotation with the given type name.
   288      */
   289     protected static boolean needsExplicitTypeName(TypeUse type, QName typeName) {
   290         if(typeName==null)
   291             // this is anonymous type. can't have @XmlSchemaType
   292             return false;
   294         if(!typeName.getNamespaceURI().equals(WellKnownNamespace.XML_SCHEMA))
   295             // if we put application-defined type name, it will be undefined
   296             // by the time we generate a schema.
   297             return false;
   299         if(type.isCollection())
   300             // there's no built-in binding for a list simple type,
   301             // so any collection type always need @XmlSchemaType
   302             return true;
   304         QName itemType = type.getInfo().getTypeName();
   305         if(itemType==null)
   306             // this is somewhat strange case, as it means the bound type is anonymous
   307             // but it's eventually derived by a named type and used.
   308             // but we can certainly use typeName as @XmlSchemaType value here
   309             return true;
   311         // if it's the default type name for this item, then no need
   312         return !itemType.equals(typeName);
   313     }
   315     /**
   316      * Puts the element names that this property possesses to the map,
   317      * so that we can find two properties that own the same element name,
   318      * which is an error.
   319      *
   320      * @return
   321      *      null if no conflict was found. Otherwise return the QName that has the collision.
   322      */
   323     public QName collectElementNames(Map<QName,CPropertyInfo> table) {
   324         return null;
   325     }
   327     public final <A extends Annotation> A readAnnotation(Class<A> annotationType) {
   328         throw new UnsupportedOperationException();
   329     }
   331     public final boolean hasAnnotation(Class<? extends Annotation> annotationType) {
   332         throw new UnsupportedOperationException();
   333     }
   334 }

mercurial