src/share/jaxws_classes/com/sun/xml/internal/ws/model/WrapperBeanGenerator.java

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 368
0989ad8c0860
child 450
b0c2840e2513
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

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

mercurial