src/share/jaxws_classes/com/sun/xml/internal/ws/model/WrapperBeanGenerator.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.xml.internal.ws.model;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.ws.model.AbstractWrapperBeanGenerator.BeanMemberFactory;
ohair@286 29 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
ohair@286 30 import com.sun.xml.internal.bind.v2.model.annotation.RuntimeInlineAnnotationReader;
ohair@286 31 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
ohair@286 32 import com.sun.xml.internal.ws.org.objectweb.asm.*;
ohair@286 33 import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.*;
ohair@286 34 import com.sun.xml.internal.ws.org.objectweb.asm.Type;
ohair@286 35
ohair@286 36 import javax.xml.bind.annotation.XmlAttachmentRef;
ohair@286 37 import javax.xml.bind.annotation.XmlElement;
ohair@286 38 import javax.xml.bind.annotation.XmlList;
ohair@286 39 import javax.xml.bind.annotation.XmlMimeType;
ohair@286 40 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
ohair@286 41 import javax.xml.namespace.QName;
ohair@286 42 import javax.xml.ws.Holder;
ohair@286 43 import javax.xml.ws.WebServiceException;
ohair@286 44 import java.lang.annotation.Annotation;
ohair@286 45 import java.lang.reflect.*;
ohair@286 46 import java.util.*;
ohair@286 47 import java.util.logging.Level;
ohair@286 48 import java.util.logging.Logger;
ohair@286 49
ohair@286 50 /**
ohair@286 51 * Runtime Wrapper and exception bean generator implementation.
ohair@286 52 * It uses ASM to generate request, response and exception beans.
ohair@286 53 *
ohair@286 54 * @author Jitendra Kotamraju
ohair@286 55 */
ohair@286 56 public class WrapperBeanGenerator {
ohair@286 57
ohair@286 58 private static final Logger LOGGER = Logger.getLogger(WrapperBeanGenerator.class.getName());
ohair@286 59
ohair@286 60 private static final FieldFactory FIELD_FACTORY = new FieldFactory();
ohair@286 61
ohair@286 62 private static final AbstractWrapperBeanGenerator RUNTIME_GENERATOR =
ohair@286 63 new RuntimeWrapperBeanGenerator(new RuntimeInlineAnnotationReader(),
ohair@286 64 Navigator.REFLECTION, FIELD_FACTORY);
ohair@286 65
ohair@286 66 private static final class RuntimeWrapperBeanGenerator extends AbstractWrapperBeanGenerator<java.lang.reflect.Type, Class, java.lang.reflect.Method, Field> {
ohair@286 67
ohair@286 68 protected RuntimeWrapperBeanGenerator(AnnotationReader<java.lang.reflect.Type, Class, ?, Method> annReader, Navigator<java.lang.reflect.Type, Class, ?, Method> nav, BeanMemberFactory<java.lang.reflect.Type, Field> beanMemberFactory) {
ohair@286 69 super(annReader, nav, beanMemberFactory);
ohair@286 70 }
ohair@286 71
ohair@286 72 protected java.lang.reflect.Type getSafeType(java.lang.reflect.Type type) {
ohair@286 73 return type;
ohair@286 74 }
ohair@286 75
ohair@286 76 protected java.lang.reflect.Type getHolderValueType(java.lang.reflect.Type paramType) {
ohair@286 77 if (paramType instanceof ParameterizedType) {
ohair@286 78 ParameterizedType p = (ParameterizedType)paramType;
ohair@286 79 if (p.getRawType().equals(Holder.class)) {
ohair@286 80 return p.getActualTypeArguments()[0];
ohair@286 81 }
ohair@286 82 }
ohair@286 83 return null;
ohair@286 84 }
ohair@286 85
ohair@286 86 protected boolean isVoidType(java.lang.reflect.Type type) {
ohair@286 87 return type == Void.TYPE;
ohair@286 88 }
ohair@286 89
ohair@286 90 }
ohair@286 91
ohair@286 92 private static final class FieldFactory implements BeanMemberFactory<java.lang.reflect.Type, Field> {
ohair@286 93 public Field createWrapperBeanMember(java.lang.reflect.Type paramType,
ohair@286 94 String paramName, List<Annotation> jaxb) {
ohair@286 95 return new Field(paramName, paramType, getASMType(paramType), jaxb);
ohair@286 96 }
ohair@286 97 }
ohair@286 98
ohair@286 99 // Creates class's bytes
ohair@286 100 private static byte[] createBeanImage(String className,
ohair@286 101 String rootName, String rootNS,
ohair@286 102 String typeName, String typeNS,
ohair@286 103 Collection<Field> fields) throws Exception {
ohair@286 104
ohair@286 105 ClassWriter cw = new ClassWriter(0);
ohair@286 106 //org.objectweb.asm.util.TraceClassVisitor cw = new org.objectweb.asm.util.TraceClassVisitor(actual, new java.io.PrintWriter(System.out));
ohair@286 107
ohair@286 108 cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, replaceDotWithSlash(className), null, "java/lang/Object", null);
ohair@286 109
ohair@286 110 AnnotationVisitor root = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true);
ohair@286 111 root.visit("name", rootName);
ohair@286 112 root.visit("namespace", rootNS);
ohair@286 113 root.visitEnd();
ohair@286 114
ohair@286 115 AnnotationVisitor type = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlType;", true);
ohair@286 116 type.visit("name", typeName);
ohair@286 117 type.visit("namespace", typeNS);
ohair@286 118 if (fields.size() > 1) {
ohair@286 119 AnnotationVisitor propVisitor = type.visitArray("propOrder");
ohair@286 120 for(Field field : fields) {
ohair@286 121 propVisitor.visit("propOrder", field.fieldName);
ohair@286 122 }
ohair@286 123 propVisitor.visitEnd();
ohair@286 124 }
ohair@286 125 type.visitEnd();
ohair@286 126
ohair@286 127 for(Field field : fields) {
ohair@286 128 FieldVisitor fv = cw.visitField(ACC_PUBLIC, field.fieldName, field.asmType.getDescriptor(), field.getSignature(), null);
ohair@286 129
ohair@286 130 for(Annotation ann : field.jaxbAnnotations) {
ohair@286 131 if (ann instanceof XmlMimeType) {
ohair@286 132 AnnotationVisitor mime = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlMimeType;", true);
ohair@286 133 mime.visit("value", ((XmlMimeType)ann).value());
ohair@286 134 mime.visitEnd();
ohair@286 135 } else if (ann instanceof XmlJavaTypeAdapter) {
ohair@286 136 AnnotationVisitor ada = fv.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
ohair@286 137 ada.visit("value", getASMType(((XmlJavaTypeAdapter)ann).value()));
ohair@286 138 // XmlJavaTypeAdapter.type() is for package only. No need to copy.
ohair@286 139 // ada.visit("type", ((XmlJavaTypeAdapter)ann).type());
ohair@286 140 ada.visitEnd();
ohair@286 141 } else if (ann instanceof XmlAttachmentRef) {
ohair@286 142 AnnotationVisitor att = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttachmentRef;", true);
ohair@286 143 att.visitEnd();
ohair@286 144 } else if (ann instanceof XmlList) {
ohair@286 145 AnnotationVisitor list = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlList;", true);
ohair@286 146 list.visitEnd();
ohair@286 147 } else if (ann instanceof XmlElement) {
ohair@286 148 AnnotationVisitor elem = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
ohair@286 149 XmlElement xmlElem = (XmlElement)ann;
ohair@286 150 elem.visit("name", xmlElem.name());
ohair@286 151 elem.visit("namespace", xmlElem.namespace());
ohair@286 152 if (xmlElem.nillable()) {
ohair@286 153 elem.visit("nillable", true);
ohair@286 154 }
ohair@286 155 if (xmlElem.required()) {
ohair@286 156 elem.visit("required", true);
ohair@286 157 }
ohair@286 158 elem.visitEnd();
ohair@286 159 } else {
ohair@286 160 throw new WebServiceException("Unknown JAXB annotation " + ann);
ohair@286 161 }
ohair@286 162 }
ohair@286 163
ohair@286 164 fv.visitEnd();
ohair@286 165 }
ohair@286 166
ohair@286 167 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
ohair@286 168 mv.visitCode();
ohair@286 169 mv.visitVarInsn(ALOAD, 0);
ohair@286 170 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
ohair@286 171 mv.visitInsn(RETURN);
ohair@286 172 mv.visitMaxs(1, 1);
ohair@286 173 mv.visitEnd();
ohair@286 174
ohair@286 175 cw.visitEnd();
ohair@286 176
ohair@286 177 if (LOGGER.isLoggable(Level.FINE)) {
ohair@286 178 // Class's @XmlRootElement
ohair@286 179 StringBuilder sb = new StringBuilder();
ohair@286 180 sb.append("\n");
ohair@286 181 sb.append("@XmlRootElement(name=").append(rootName)
ohair@286 182 .append(", namespace=").append(rootNS).append(")");
ohair@286 183
ohair@286 184 // Class's @XmlType
ohair@286 185 sb.append("\n");
ohair@286 186 sb.append("@XmlType(name=").append(typeName)
ohair@286 187 .append(", namespace=").append(typeNS);
ohair@286 188 if (fields.size() > 1) {
ohair@286 189 sb.append(", propOrder={");
ohair@286 190 for(Field field : fields) {
ohair@286 191 sb.append(" ");
ohair@286 192 sb.append(field.fieldName);
ohair@286 193 }
ohair@286 194 sb.append(" }");
ohair@286 195 }
ohair@286 196 sb.append(")");
ohair@286 197
ohair@286 198 // class declaration
ohair@286 199 sb.append("\n");
ohair@286 200 sb.append("public class ").append(className).append(" {");
ohair@286 201
ohair@286 202 // fields declaration
ohair@286 203 for(Field field : fields) {
ohair@286 204 sb.append("\n");
ohair@286 205
ohair@286 206 // Field's other JAXB annotations
ohair@286 207 for(Annotation ann : field.jaxbAnnotations) {
ohair@286 208 sb.append("\n ");
ohair@286 209
ohair@286 210 if (ann instanceof XmlMimeType) {
ohair@286 211 sb.append("@XmlMimeType(value=").append(((XmlMimeType)ann).value()).append(")");
ohair@286 212 } else if (ann instanceof XmlJavaTypeAdapter) {
ohair@286 213 sb.append("@XmlJavaTypeAdapter(value=").append(getASMType(((XmlJavaTypeAdapter)ann).value())).append(")");
ohair@286 214 } else if (ann instanceof XmlAttachmentRef) {
ohair@286 215 sb.append("@XmlAttachmentRef");
ohair@286 216 } else if (ann instanceof XmlList) {
ohair@286 217 sb.append("@XmlList");
ohair@286 218 } else if (ann instanceof XmlElement) {
ohair@286 219 XmlElement xmlElem = (XmlElement)ann;
ohair@286 220 sb.append("\n ");
ohair@286 221 sb.append("@XmlElement(name=").append(xmlElem.name())
ohair@286 222 .append(", namespace=").append(xmlElem.namespace());
ohair@286 223 if (xmlElem.nillable()) {
ohair@286 224 sb.append(", nillable=true");
ohair@286 225 }
ohair@286 226 if (xmlElem.required()) {
ohair@286 227 sb.append(", required=true");
ohair@286 228 }
ohair@286 229 sb.append(")");
ohair@286 230 } else {
ohair@286 231 throw new WebServiceException("Unknown JAXB annotation " + ann);
ohair@286 232 }
ohair@286 233 }
ohair@286 234
ohair@286 235 // Field declaration
ohair@286 236 sb.append("\n ");
ohair@286 237 sb.append("public ");
ohair@286 238 if (field.getSignature() == null) {
ohair@286 239 sb.append(field.asmType.getDescriptor());
ohair@286 240 } else {
ohair@286 241 sb.append(field.getSignature());
ohair@286 242 }
ohair@286 243 sb.append(" ");
ohair@286 244 sb.append(field.fieldName);
ohair@286 245 }
ohair@286 246
ohair@286 247 sb.append("\n\n}");
ohair@286 248 LOGGER.fine(sb.toString());
ohair@286 249 }
ohair@286 250
ohair@286 251 return cw.toByteArray();
ohair@286 252 }
ohair@286 253
ohair@286 254 private static String replaceDotWithSlash(String name) {
ohair@286 255 return name.replace('.', '/');
ohair@286 256 }
ohair@286 257
ohair@286 258 static Class createRequestWrapperBean(String className, Method method, QName reqElemName, ClassLoader cl) {
ohair@286 259
ohair@286 260 LOGGER.fine("Request Wrapper Class : "+className);
ohair@286 261
ohair@286 262 List<Field> requestMembers = RUNTIME_GENERATOR.collectRequestBeanMembers(
ohair@286 263 method);
ohair@286 264
ohair@286 265 byte[] image;
ohair@286 266 try {
ohair@286 267 image = createBeanImage(className, reqElemName.getLocalPart(), reqElemName.getNamespaceURI(),
ohair@286 268 reqElemName.getLocalPart(), reqElemName.getNamespaceURI(),
ohair@286 269 requestMembers);
ohair@286 270 } catch(Exception e) {
ohair@286 271 throw new WebServiceException(e);
ohair@286 272 }
ohair@286 273 // write(image, className);
ohair@286 274 return Injector.inject(cl, className, image);
ohair@286 275 }
ohair@286 276
ohair@286 277 static Class createResponseWrapperBean(String className, Method method, QName resElemName, ClassLoader cl) {
ohair@286 278
ohair@286 279 LOGGER.fine("Response Wrapper Class : "+className);
ohair@286 280
ohair@286 281 List<Field> responseMembers = RUNTIME_GENERATOR.collectResponseBeanMembers(method);
ohair@286 282
ohair@286 283 byte[] image;
ohair@286 284 try {
ohair@286 285 image = createBeanImage(className, resElemName.getLocalPart(), resElemName.getNamespaceURI(),
ohair@286 286 resElemName.getLocalPart(), resElemName.getNamespaceURI(),
ohair@286 287 responseMembers);
ohair@286 288 } catch(Exception e) {
ohair@286 289 throw new WebServiceException(e);
ohair@286 290 }
ohair@286 291 // write(image, className);
ohair@286 292
ohair@286 293 return Injector.inject(cl, className, image);
ohair@286 294 }
ohair@286 295
ohair@286 296
ohair@286 297 private static Type getASMType(java.lang.reflect.Type t) {
ohair@286 298 assert t!=null;
ohair@286 299
ohair@286 300 if (t instanceof Class) {
ohair@286 301 return Type.getType((Class)t);
ohair@286 302 }
ohair@286 303
ohair@286 304 if (t instanceof ParameterizedType) {
ohair@286 305 ParameterizedType pt = (ParameterizedType)t;
ohair@286 306 if (pt.getRawType() instanceof Class) {
ohair@286 307 return Type.getType((Class)pt.getRawType());
ohair@286 308 }
ohair@286 309 }
ohair@286 310 if (t instanceof GenericArrayType) {
ohair@286 311 return Type.getType(FieldSignature.vms(t));
ohair@286 312 }
ohair@286 313
ohair@286 314 if (t instanceof WildcardType) {
ohair@286 315 return Type.getType(FieldSignature.vms(t));
ohair@286 316 }
ohair@286 317
ohair@286 318 if (t instanceof TypeVariable) {
ohair@286 319 TypeVariable tv = (TypeVariable)t;
ohair@286 320 if (tv.getBounds()[0] instanceof Class) {
ohair@286 321 return Type.getType((Class)tv.getBounds()[0]);
ohair@286 322 }
ohair@286 323 }
ohair@286 324
ohair@286 325 throw new IllegalArgumentException("Not creating ASM Type for type = "+t);
ohair@286 326 }
ohair@286 327
ohair@286 328
ohair@286 329 static Class createExceptionBean(String className, Class exception, String typeNS, String elemName, String elemNS, ClassLoader cl) {
ohair@286 330
ohair@286 331 Collection<Field> fields = RUNTIME_GENERATOR.collectExceptionBeanMembers(exception);
ohair@286 332
ohair@286 333 byte[] image;
ohair@286 334 try {
ohair@286 335 image = createBeanImage(className, elemName, elemNS,
ohair@286 336 exception.getSimpleName(), typeNS,
ohair@286 337 fields);
ohair@286 338 } catch(Exception e) {
ohair@286 339 throw new WebServiceException(e);
ohair@286 340 }
ohair@286 341
ohair@286 342 return Injector.inject(cl, className, image);
ohair@286 343 }
ohair@286 344
ohair@286 345 private static class Field implements Comparable<Field> {
ohair@286 346 private final java.lang.reflect.Type reflectType;
ohair@286 347 private final Type asmType;
ohair@286 348 private final String fieldName;
ohair@286 349 private final List<Annotation> jaxbAnnotations;
ohair@286 350
ohair@286 351 Field(String paramName, java.lang.reflect.Type paramType, Type asmType,
ohair@286 352 List<Annotation> jaxbAnnotations) {
ohair@286 353 this.reflectType = paramType;
ohair@286 354 this.asmType = asmType;
ohair@286 355 this.fieldName = paramName;
ohair@286 356 this.jaxbAnnotations = jaxbAnnotations;
ohair@286 357 }
ohair@286 358
ohair@286 359 String getSignature() {
ohair@286 360 if (reflectType instanceof Class) {
ohair@286 361 return null;
ohair@286 362 }
ohair@286 363 if (reflectType instanceof TypeVariable) {
ohair@286 364 return null;
ohair@286 365 }
ohair@286 366 return FieldSignature.vms(reflectType);
ohair@286 367 }
ohair@286 368
ohair@286 369 public int compareTo(Field o) {
ohair@286 370 return fieldName.compareTo(o.fieldName);
ohair@286 371 }
ohair@286 372
ohair@286 373 }
ohair@286 374
ohair@286 375 static void write(byte[] b, String className) {
ohair@286 376 className = className.substring(className.lastIndexOf(".")+1);
ohair@286 377 try {
ohair@286 378 java.io.FileOutputStream fo = new java.io.FileOutputStream(className + ".class");
ohair@286 379 fo.write(b);
ohair@286 380 fo.flush();
ohair@286 381 fo.close();
ohair@286 382 } catch (java.io.IOException e) {
ohair@286 383 // TODO Auto-generated catch block
ohair@286 384 e.printStackTrace();
ohair@286 385 }
ohair@286 386 }
ohair@286 387
ohair@286 388 }

mercurial