src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/BIConversion.java

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

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
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

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.tools.internal.xjc.reader.xmlschema.bindinfo;
aoqi@0 27
aoqi@0 28 import javax.xml.bind.DatatypeConverter;
aoqi@0 29 import javax.xml.bind.annotation.XmlAttribute;
aoqi@0 30 import javax.xml.bind.annotation.XmlRootElement;
aoqi@0 31 import javax.xml.bind.annotation.adapters.XmlAdapter;
aoqi@0 32 import javax.xml.namespace.QName;
aoqi@0 33
aoqi@0 34 import com.sun.codemodel.internal.JClass;
aoqi@0 35 import com.sun.codemodel.internal.JClassAlreadyExistsException;
aoqi@0 36 import com.sun.codemodel.internal.JCodeModel;
aoqi@0 37 import com.sun.codemodel.internal.JDefinedClass;
aoqi@0 38 import com.sun.codemodel.internal.JExpr;
aoqi@0 39 import com.sun.codemodel.internal.JExpression;
aoqi@0 40 import com.sun.codemodel.internal.JMethod;
aoqi@0 41 import com.sun.codemodel.internal.JMod;
aoqi@0 42 import com.sun.codemodel.internal.JPackage;
aoqi@0 43 import com.sun.codemodel.internal.JType;
aoqi@0 44 import com.sun.codemodel.internal.JVar;
aoqi@0 45 import com.sun.codemodel.internal.JConditional;
aoqi@0 46 import com.sun.tools.internal.xjc.ErrorReceiver;
aoqi@0 47 import com.sun.tools.internal.xjc.model.CAdapter;
aoqi@0 48 import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
aoqi@0 49 import com.sun.tools.internal.xjc.model.TypeUse;
aoqi@0 50 import com.sun.tools.internal.xjc.model.TypeUseFactory;
aoqi@0 51 import com.sun.tools.internal.xjc.reader.Const;
aoqi@0 52 import com.sun.tools.internal.xjc.reader.Ring;
aoqi@0 53 import com.sun.tools.internal.xjc.reader.TypeUtil;
aoqi@0 54 import com.sun.tools.internal.xjc.reader.xmlschema.ClassSelector;
aoqi@0 55 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
aoqi@0 56 import com.sun.xml.internal.xsom.XSSimpleType;
aoqi@0 57
aoqi@0 58 import org.xml.sax.Locator;
aoqi@0 59
aoqi@0 60 /**
aoqi@0 61 * Conversion declaration.
aoqi@0 62 *
aoqi@0 63 * <p>
aoqi@0 64 * A conversion declaration specifies how an XML type gets mapped
aoqi@0 65 * to a Java type.
aoqi@0 66 *
aoqi@0 67 * @author
aoqi@0 68 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
aoqi@0 69 */
aoqi@0 70 public abstract class BIConversion extends AbstractDeclarationImpl {
aoqi@0 71 @Deprecated
aoqi@0 72 public BIConversion( Locator loc ) {
aoqi@0 73 super(loc);
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 protected BIConversion() {
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 /**
aoqi@0 80 * Gets the {@link TypeUse} object that this conversion represents.
aoqi@0 81 * <p>
aoqi@0 82 * The returned {@link TypeUse} object is properly adapted.
aoqi@0 83 *
aoqi@0 84 * @param owner
aoqi@0 85 * A {@link BIConversion} is always associated with one
aoqi@0 86 * {@link XSSimpleType}, but that's not always available
aoqi@0 87 * when a {@link BIConversion} is built. So we pass this
aoqi@0 88 * as a parameter to this method.
aoqi@0 89 */
aoqi@0 90 public abstract TypeUse getTypeUse( XSSimpleType owner );
aoqi@0 91
aoqi@0 92 public QName getName() { return NAME; }
aoqi@0 93
aoqi@0 94 /** Name of the conversion declaration. */
aoqi@0 95 public static final QName NAME = new QName(
aoqi@0 96 Const.JAXB_NSURI, "conversion" );
aoqi@0 97
aoqi@0 98 /**
aoqi@0 99 * Implementation that returns a statically-determined constant {@link TypeUse}.
aoqi@0 100 */
aoqi@0 101 public static final class Static extends BIConversion {
aoqi@0 102 /**
aoqi@0 103 * Always non-null.
aoqi@0 104 */
aoqi@0 105 private final TypeUse transducer;
aoqi@0 106
aoqi@0 107 public Static(Locator loc, TypeUse transducer) {
aoqi@0 108 super(loc);
aoqi@0 109 this.transducer = transducer;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 public TypeUse getTypeUse(XSSimpleType owner) {
aoqi@0 113 return transducer;
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 /**
aoqi@0 118 * User-specified &lt;javaType> customization.
aoqi@0 119 *
aoqi@0 120 * The parse/print methods are allowed to be null,
aoqi@0 121 * and their default values are determined based on the
aoqi@0 122 * owner of the token.
aoqi@0 123 */
aoqi@0 124 @XmlRootElement(name="javaType")
aoqi@0 125 public static class User extends BIConversion {
aoqi@0 126 @XmlAttribute
aoqi@0 127 private String parseMethod;
aoqi@0 128 @XmlAttribute
aoqi@0 129 private String printMethod;
aoqi@0 130 @XmlAttribute(name="name")
aoqi@0 131 private String type = "java.lang.String";
aoqi@0 132
aoqi@0 133 /**
aoqi@0 134 * If null, computed from {@link #type}.
aoqi@0 135 * Sometimes this can be set instead of {@link #type}.
aoqi@0 136 */
aoqi@0 137 private JType inMemoryType;
aoqi@0 138
aoqi@0 139 public User(Locator loc, String parseMethod, String printMethod, JType inMemoryType) {
aoqi@0 140 super(loc);
aoqi@0 141 this.parseMethod = parseMethod;
aoqi@0 142 this.printMethod = printMethod;
aoqi@0 143 this.inMemoryType = inMemoryType;
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 public User() {
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 /**
aoqi@0 150 * Cache used by {@link #getTypeUse(XSSimpleType)} to improve the performance.
aoqi@0 151 */
aoqi@0 152 private TypeUse typeUse;
aoqi@0 153
aoqi@0 154 public TypeUse getTypeUse(XSSimpleType owner) {
aoqi@0 155 if(typeUse!=null)
aoqi@0 156 return typeUse;
aoqi@0 157
aoqi@0 158 JCodeModel cm = getCodeModel();
aoqi@0 159
aoqi@0 160 if(inMemoryType==null)
aoqi@0 161 inMemoryType = TypeUtil.getType(cm,type,Ring.get(ErrorReceiver.class),getLocation());
aoqi@0 162
aoqi@0 163 JDefinedClass adapter = generateAdapter(parseMethodFor(owner),printMethodFor(owner),owner);
aoqi@0 164
aoqi@0 165 // XmlJavaType customization always converts between string and an user-defined type.
aoqi@0 166 typeUse = TypeUseFactory.adapt(CBuiltinLeafInfo.STRING,new CAdapter(adapter));
aoqi@0 167
aoqi@0 168 return typeUse;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 /**
aoqi@0 172 * generate the adapter class.
aoqi@0 173 */
aoqi@0 174 private JDefinedClass generateAdapter(String parseMethod, String printMethod,XSSimpleType owner) {
aoqi@0 175 JDefinedClass adapter = null;
aoqi@0 176
aoqi@0 177 int id = 1;
aoqi@0 178 while(adapter==null) {
aoqi@0 179 try {
aoqi@0 180 JPackage pkg = Ring.get(ClassSelector.class).getClassScope().getOwnerPackage();
aoqi@0 181 adapter = pkg._class("Adapter"+id);
aoqi@0 182 } catch (JClassAlreadyExistsException e) {
aoqi@0 183 // try another name in search for an unique name.
aoqi@0 184 // this isn't too efficient, but we expect people to usually use
aoqi@0 185 // a very small number of adapters.
aoqi@0 186 id++;
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 JClass bim = inMemoryType.boxify();
aoqi@0 191
aoqi@0 192 adapter._extends(getCodeModel().ref(XmlAdapter.class).narrow(String.class).narrow(bim));
aoqi@0 193
aoqi@0 194 JMethod unmarshal = adapter.method(JMod.PUBLIC, bim, "unmarshal");
aoqi@0 195 JVar $value = unmarshal.param(String.class, "value");
aoqi@0 196
aoqi@0 197 JExpression inv;
aoqi@0 198
aoqi@0 199 if( parseMethod.equals("new") ) {
aoqi@0 200 // "new" indicates that the constructor of the target type
aoqi@0 201 // will do the unmarshalling.
aoqi@0 202
aoqi@0 203 // RESULT: new <type>()
aoqi@0 204 inv = JExpr._new(bim).arg($value);
aoqi@0 205 } else {
aoqi@0 206 int idx = parseMethod.lastIndexOf('.');
aoqi@0 207 if(idx<0) {
aoqi@0 208 // parseMethod specifies the static method of the target type
aoqi@0 209 // which will do the unmarshalling.
aoqi@0 210
aoqi@0 211 // because of an error check at the constructor,
aoqi@0 212 // we can safely assume that this cast works.
aoqi@0 213 inv = bim.staticInvoke(parseMethod).arg($value);
aoqi@0 214 } else {
aoqi@0 215 inv = JExpr.direct(parseMethod+"(value)");
aoqi@0 216 }
aoqi@0 217 }
aoqi@0 218 unmarshal.body()._return(inv);
aoqi@0 219
aoqi@0 220
aoqi@0 221 JMethod marshal = adapter.method(JMod.PUBLIC, String.class, "marshal");
aoqi@0 222 $value = marshal.param(bim,"value");
aoqi@0 223
aoqi@0 224 if(printMethod.startsWith("javax.xml.bind.DatatypeConverter.")) {
aoqi@0 225 // UGLY: if this conversion is the system-driven conversion,
aoqi@0 226 // check for null
aoqi@0 227 marshal.body()._if($value.eq(JExpr._null()))._then()._return(JExpr._null());
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 int idx = printMethod.lastIndexOf('.');
aoqi@0 231 if(idx<0) {
aoqi@0 232 // printMethod specifies a method in the target type
aoqi@0 233 // which performs the serialization.
aoqi@0 234
aoqi@0 235 // RESULT: <value>.<method>()
aoqi@0 236 inv = $value.invoke(printMethod);
aoqi@0 237
aoqi@0 238 // check value is not null ... if(value == null) return null;
aoqi@0 239 JConditional jcon = marshal.body()._if($value.eq(JExpr._null()));
aoqi@0 240 jcon._then()._return(JExpr._null());
aoqi@0 241 } else {
aoqi@0 242 // RESULT: <className>.<method>(<value>)
aoqi@0 243 if(this.printMethod==null) {
aoqi@0 244 // HACK HACK HACK
aoqi@0 245 JType t = inMemoryType.unboxify();
aoqi@0 246 inv = JExpr.direct(printMethod+"(("+findBaseConversion(owner).toLowerCase()+")("+t.fullName()+")value)");
aoqi@0 247 } else
aoqi@0 248 inv = JExpr.direct(printMethod+"(value)");
aoqi@0 249 }
aoqi@0 250 marshal.body()._return(inv);
aoqi@0 251
aoqi@0 252 return adapter;
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 private String printMethodFor(XSSimpleType owner) {
aoqi@0 256 if(printMethod!=null) return printMethod;
aoqi@0 257
aoqi@0 258 if(inMemoryType.unboxify().isPrimitive()) {
aoqi@0 259 String method = getConversionMethod("print",owner);
aoqi@0 260 if(method!=null)
aoqi@0 261 return method;
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 return "toString";
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 private String parseMethodFor(XSSimpleType owner) {
aoqi@0 268 if(parseMethod!=null) return parseMethod;
aoqi@0 269
aoqi@0 270 if(inMemoryType.unboxify().isPrimitive()) {
aoqi@0 271 String method = getConversionMethod("parse", owner);
aoqi@0 272 if(method!=null) {
aoqi@0 273 // this cast is necessary for conversion between primitive Java types
aoqi@0 274 return '('+inMemoryType.unboxify().fullName()+')'+method;
aoqi@0 275 }
aoqi@0 276 }
aoqi@0 277
aoqi@0 278 return "new";
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 private static final String[] knownBases = new String[]{
aoqi@0 282 "Float", "Double", "Byte", "Short", "Int", "Long", "Boolean"
aoqi@0 283 };
aoqi@0 284
aoqi@0 285 private String getConversionMethod(String methodPrefix, XSSimpleType owner) {
aoqi@0 286 String bc = findBaseConversion(owner);
aoqi@0 287 if(bc==null) return null;
aoqi@0 288
aoqi@0 289 return DatatypeConverter.class.getName()+'.'+methodPrefix+bc;
aoqi@0 290 }
aoqi@0 291
aoqi@0 292 private String findBaseConversion(XSSimpleType owner) {
aoqi@0 293 // find the base simple type mapping.
aoqi@0 294 for( XSSimpleType st=owner; st!=null; st = st.getSimpleBaseType() ) {
aoqi@0 295 if( !WellKnownNamespace.XML_SCHEMA.equals(st.getTargetNamespace()) )
aoqi@0 296 continue; // user-defined type
aoqi@0 297
aoqi@0 298 String name = st.getName().intern();
aoqi@0 299 for( String s : knownBases )
aoqi@0 300 if(name.equalsIgnoreCase(s))
aoqi@0 301 return s;
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 return null;
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 public QName getName() { return NAME; }
aoqi@0 308
aoqi@0 309 /** Name of the conversion declaration. */
aoqi@0 310 public static final QName NAME = new QName(
aoqi@0 311 Const.JAXB_NSURI, "javaType" );
aoqi@0 312 }
aoqi@0 313
aoqi@0 314 @XmlRootElement(name="javaType",namespace=Const.XJC_EXTENSION_URI)
aoqi@0 315 public static class UserAdapter extends BIConversion {
aoqi@0 316 @XmlAttribute(name="name")
aoqi@0 317 private String type = null;
aoqi@0 318
aoqi@0 319 @XmlAttribute
aoqi@0 320 private String adapter = null;
aoqi@0 321
aoqi@0 322 private TypeUse typeUse;
aoqi@0 323
aoqi@0 324 public TypeUse getTypeUse(XSSimpleType owner) {
aoqi@0 325 if(typeUse!=null)
aoqi@0 326 return typeUse;
aoqi@0 327
aoqi@0 328 JCodeModel cm = getCodeModel();
aoqi@0 329
aoqi@0 330 JDefinedClass a;
aoqi@0 331 try {
aoqi@0 332 a = cm._class(adapter);
aoqi@0 333 a.hide(); // we assume this is given by the user
aoqi@0 334 a._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(
aoqi@0 335 cm.ref(type)));
aoqi@0 336 } catch (JClassAlreadyExistsException e) {
aoqi@0 337 a = e.getExistingClass();
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 // TODO: it's not correct to say that it adapts from String,
aoqi@0 341 // but OTOH I don't think we can compute that.
aoqi@0 342 typeUse = TypeUseFactory.adapt(
aoqi@0 343 CBuiltinLeafInfo.STRING,
aoqi@0 344 new CAdapter(a));
aoqi@0 345
aoqi@0 346 return typeUse;
aoqi@0 347 }
aoqi@0 348 }
aoqi@0 349 }

mercurial