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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 397
b99d7e355d4b
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial