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

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

mercurial