ohair@286: /* ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.model; ohair@286: ohair@286: import com.sun.xml.internal.ws.model.AbstractWrapperBeanGenerator.BeanMemberFactory; ohair@286: import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; ohair@286: import com.sun.xml.internal.bind.v2.model.annotation.RuntimeInlineAnnotationReader; ohair@286: import com.sun.xml.internal.bind.v2.model.nav.Navigator; ohair@286: import com.sun.xml.internal.ws.org.objectweb.asm.*; ohair@286: import static com.sun.xml.internal.ws.org.objectweb.asm.Opcodes.*; ohair@286: import com.sun.xml.internal.ws.org.objectweb.asm.Type; ohair@286: ohair@286: import javax.xml.bind.annotation.XmlAttachmentRef; ohair@286: import javax.xml.bind.annotation.XmlElement; ohair@286: import javax.xml.bind.annotation.XmlList; ohair@286: import javax.xml.bind.annotation.XmlMimeType; ohair@286: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.ws.Holder; ohair@286: import javax.xml.ws.WebServiceException; ohair@286: import java.lang.annotation.Annotation; ohair@286: import java.lang.reflect.*; ohair@286: import java.util.*; ohair@286: import java.util.logging.Level; ohair@286: import java.util.logging.Logger; ohair@286: ohair@286: /** ohair@286: * Runtime Wrapper and exception bean generator implementation. ohair@286: * It uses ASM to generate request, response and exception beans. ohair@286: * ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: public class WrapperBeanGenerator { ohair@286: ohair@286: private static final Logger LOGGER = Logger.getLogger(WrapperBeanGenerator.class.getName()); ohair@286: ohair@286: private static final FieldFactory FIELD_FACTORY = new FieldFactory(); ohair@286: ohair@286: private static final AbstractWrapperBeanGenerator RUNTIME_GENERATOR = ohair@286: new RuntimeWrapperBeanGenerator(new RuntimeInlineAnnotationReader(), ohair@286: Navigator.REFLECTION, FIELD_FACTORY); ohair@286: ohair@286: private static final class RuntimeWrapperBeanGenerator extends AbstractWrapperBeanGenerator { ohair@286: ohair@286: protected RuntimeWrapperBeanGenerator(AnnotationReader annReader, Navigator nav, BeanMemberFactory beanMemberFactory) { ohair@286: super(annReader, nav, beanMemberFactory); ohair@286: } ohair@286: ohair@286: protected java.lang.reflect.Type getSafeType(java.lang.reflect.Type type) { ohair@286: return type; ohair@286: } ohair@286: ohair@286: protected java.lang.reflect.Type getHolderValueType(java.lang.reflect.Type paramType) { ohair@286: if (paramType instanceof ParameterizedType) { ohair@286: ParameterizedType p = (ParameterizedType)paramType; ohair@286: if (p.getRawType().equals(Holder.class)) { ohair@286: return p.getActualTypeArguments()[0]; ohair@286: } ohair@286: } ohair@286: return null; ohair@286: } ohair@286: ohair@286: protected boolean isVoidType(java.lang.reflect.Type type) { ohair@286: return type == Void.TYPE; ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: private static final class FieldFactory implements BeanMemberFactory { ohair@286: public Field createWrapperBeanMember(java.lang.reflect.Type paramType, ohair@286: String paramName, List jaxb) { ohair@286: return new Field(paramName, paramType, getASMType(paramType), jaxb); ohair@286: } ohair@286: } ohair@286: ohair@286: // Creates class's bytes ohair@286: private static byte[] createBeanImage(String className, ohair@286: String rootName, String rootNS, ohair@286: String typeName, String typeNS, ohair@286: Collection fields) throws Exception { ohair@286: ohair@286: ClassWriter cw = new ClassWriter(0); ohair@286: //org.objectweb.asm.util.TraceClassVisitor cw = new org.objectweb.asm.util.TraceClassVisitor(actual, new java.io.PrintWriter(System.out)); ohair@286: ohair@286: cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, replaceDotWithSlash(className), null, "java/lang/Object", null); ohair@286: ohair@286: AnnotationVisitor root = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true); ohair@286: root.visit("name", rootName); ohair@286: root.visit("namespace", rootNS); ohair@286: root.visitEnd(); ohair@286: ohair@286: AnnotationVisitor type = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlType;", true); ohair@286: type.visit("name", typeName); ohair@286: type.visit("namespace", typeNS); ohair@286: if (fields.size() > 1) { ohair@286: AnnotationVisitor propVisitor = type.visitArray("propOrder"); ohair@286: for(Field field : fields) { ohair@286: propVisitor.visit("propOrder", field.fieldName); ohair@286: } ohair@286: propVisitor.visitEnd(); ohair@286: } ohair@286: type.visitEnd(); ohair@286: ohair@286: for(Field field : fields) { ohair@286: FieldVisitor fv = cw.visitField(ACC_PUBLIC, field.fieldName, field.asmType.getDescriptor(), field.getSignature(), null); ohair@286: ohair@286: for(Annotation ann : field.jaxbAnnotations) { ohair@286: if (ann instanceof XmlMimeType) { ohair@286: AnnotationVisitor mime = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlMimeType;", true); ohair@286: mime.visit("value", ((XmlMimeType)ann).value()); ohair@286: mime.visitEnd(); ohair@286: } else if (ann instanceof XmlJavaTypeAdapter) { ohair@286: AnnotationVisitor ada = fv.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true); ohair@286: ada.visit("value", getASMType(((XmlJavaTypeAdapter)ann).value())); ohair@286: // XmlJavaTypeAdapter.type() is for package only. No need to copy. ohair@286: // ada.visit("type", ((XmlJavaTypeAdapter)ann).type()); ohair@286: ada.visitEnd(); ohair@286: } else if (ann instanceof XmlAttachmentRef) { ohair@286: AnnotationVisitor att = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttachmentRef;", true); ohair@286: att.visitEnd(); ohair@286: } else if (ann instanceof XmlList) { ohair@286: AnnotationVisitor list = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlList;", true); ohair@286: list.visitEnd(); ohair@286: } else if (ann instanceof XmlElement) { ohair@286: AnnotationVisitor elem = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true); ohair@286: XmlElement xmlElem = (XmlElement)ann; ohair@286: elem.visit("name", xmlElem.name()); ohair@286: elem.visit("namespace", xmlElem.namespace()); ohair@286: if (xmlElem.nillable()) { ohair@286: elem.visit("nillable", true); ohair@286: } ohair@286: if (xmlElem.required()) { ohair@286: elem.visit("required", true); ohair@286: } ohair@286: elem.visitEnd(); ohair@286: } else { ohair@286: throw new WebServiceException("Unknown JAXB annotation " + ann); ohair@286: } ohair@286: } ohair@286: ohair@286: fv.visitEnd(); ohair@286: } ohair@286: ohair@286: MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null, null); ohair@286: mv.visitCode(); ohair@286: mv.visitVarInsn(ALOAD, 0); ohair@286: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V"); ohair@286: mv.visitInsn(RETURN); ohair@286: mv.visitMaxs(1, 1); ohair@286: mv.visitEnd(); ohair@286: ohair@286: cw.visitEnd(); ohair@286: ohair@286: if (LOGGER.isLoggable(Level.FINE)) { ohair@286: // Class's @XmlRootElement ohair@286: StringBuilder sb = new StringBuilder(); ohair@286: sb.append("\n"); ohair@286: sb.append("@XmlRootElement(name=").append(rootName) ohair@286: .append(", namespace=").append(rootNS).append(")"); ohair@286: ohair@286: // Class's @XmlType ohair@286: sb.append("\n"); ohair@286: sb.append("@XmlType(name=").append(typeName) ohair@286: .append(", namespace=").append(typeNS); ohair@286: if (fields.size() > 1) { ohair@286: sb.append(", propOrder={"); ohair@286: for(Field field : fields) { ohair@286: sb.append(" "); ohair@286: sb.append(field.fieldName); ohair@286: } ohair@286: sb.append(" }"); ohair@286: } ohair@286: sb.append(")"); ohair@286: ohair@286: // class declaration ohair@286: sb.append("\n"); ohair@286: sb.append("public class ").append(className).append(" {"); ohair@286: ohair@286: // fields declaration ohair@286: for(Field field : fields) { ohair@286: sb.append("\n"); ohair@286: ohair@286: // Field's other JAXB annotations ohair@286: for(Annotation ann : field.jaxbAnnotations) { ohair@286: sb.append("\n "); ohair@286: ohair@286: if (ann instanceof XmlMimeType) { ohair@286: sb.append("@XmlMimeType(value=").append(((XmlMimeType)ann).value()).append(")"); ohair@286: } else if (ann instanceof XmlJavaTypeAdapter) { ohair@286: sb.append("@XmlJavaTypeAdapter(value=").append(getASMType(((XmlJavaTypeAdapter)ann).value())).append(")"); ohair@286: } else if (ann instanceof XmlAttachmentRef) { ohair@286: sb.append("@XmlAttachmentRef"); ohair@286: } else if (ann instanceof XmlList) { ohair@286: sb.append("@XmlList"); ohair@286: } else if (ann instanceof XmlElement) { ohair@286: XmlElement xmlElem = (XmlElement)ann; ohair@286: sb.append("\n "); ohair@286: sb.append("@XmlElement(name=").append(xmlElem.name()) ohair@286: .append(", namespace=").append(xmlElem.namespace()); ohair@286: if (xmlElem.nillable()) { ohair@286: sb.append(", nillable=true"); ohair@286: } ohair@286: if (xmlElem.required()) { ohair@286: sb.append(", required=true"); ohair@286: } ohair@286: sb.append(")"); ohair@286: } else { ohair@286: throw new WebServiceException("Unknown JAXB annotation " + ann); ohair@286: } ohair@286: } ohair@286: ohair@286: // Field declaration ohair@286: sb.append("\n "); ohair@286: sb.append("public "); ohair@286: if (field.getSignature() == null) { ohair@286: sb.append(field.asmType.getDescriptor()); ohair@286: } else { ohair@286: sb.append(field.getSignature()); ohair@286: } ohair@286: sb.append(" "); ohair@286: sb.append(field.fieldName); ohair@286: } ohair@286: ohair@286: sb.append("\n\n}"); ohair@286: LOGGER.fine(sb.toString()); ohair@286: } ohair@286: ohair@286: return cw.toByteArray(); ohair@286: } ohair@286: ohair@286: private static String replaceDotWithSlash(String name) { ohair@286: return name.replace('.', '/'); ohair@286: } ohair@286: ohair@286: static Class createRequestWrapperBean(String className, Method method, QName reqElemName, ClassLoader cl) { ohair@286: ohair@286: LOGGER.fine("Request Wrapper Class : "+className); ohair@286: ohair@286: List requestMembers = RUNTIME_GENERATOR.collectRequestBeanMembers( ohair@286: method); ohair@286: ohair@286: byte[] image; ohair@286: try { ohair@286: image = createBeanImage(className, reqElemName.getLocalPart(), reqElemName.getNamespaceURI(), ohair@286: reqElemName.getLocalPart(), reqElemName.getNamespaceURI(), ohair@286: requestMembers); ohair@286: } catch(Exception e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: // write(image, className); ohair@286: return Injector.inject(cl, className, image); ohair@286: } ohair@286: ohair@286: static Class createResponseWrapperBean(String className, Method method, QName resElemName, ClassLoader cl) { ohair@286: ohair@286: LOGGER.fine("Response Wrapper Class : "+className); ohair@286: ohair@286: List responseMembers = RUNTIME_GENERATOR.collectResponseBeanMembers(method); ohair@286: ohair@286: byte[] image; ohair@286: try { ohair@286: image = createBeanImage(className, resElemName.getLocalPart(), resElemName.getNamespaceURI(), ohair@286: resElemName.getLocalPart(), resElemName.getNamespaceURI(), ohair@286: responseMembers); ohair@286: } catch(Exception e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: // write(image, className); ohair@286: ohair@286: return Injector.inject(cl, className, image); ohair@286: } ohair@286: ohair@286: ohair@286: private static Type getASMType(java.lang.reflect.Type t) { ohair@286: assert t!=null; ohair@286: ohair@286: if (t instanceof Class) { ohair@286: return Type.getType((Class)t); ohair@286: } ohair@286: ohair@286: if (t instanceof ParameterizedType) { ohair@286: ParameterizedType pt = (ParameterizedType)t; ohair@286: if (pt.getRawType() instanceof Class) { ohair@286: return Type.getType((Class)pt.getRawType()); ohair@286: } ohair@286: } ohair@286: if (t instanceof GenericArrayType) { ohair@286: return Type.getType(FieldSignature.vms(t)); ohair@286: } ohair@286: ohair@286: if (t instanceof WildcardType) { ohair@286: return Type.getType(FieldSignature.vms(t)); ohair@286: } ohair@286: ohair@286: if (t instanceof TypeVariable) { ohair@286: TypeVariable tv = (TypeVariable)t; ohair@286: if (tv.getBounds()[0] instanceof Class) { ohair@286: return Type.getType((Class)tv.getBounds()[0]); ohair@286: } ohair@286: } ohair@286: ohair@286: throw new IllegalArgumentException("Not creating ASM Type for type = "+t); ohair@286: } ohair@286: ohair@286: ohair@286: static Class createExceptionBean(String className, Class exception, String typeNS, String elemName, String elemNS, ClassLoader cl) { ohair@286: ohair@286: Collection fields = RUNTIME_GENERATOR.collectExceptionBeanMembers(exception); ohair@286: ohair@286: byte[] image; ohair@286: try { ohair@286: image = createBeanImage(className, elemName, elemNS, ohair@286: exception.getSimpleName(), typeNS, ohair@286: fields); ohair@286: } catch(Exception e) { ohair@286: throw new WebServiceException(e); ohair@286: } ohair@286: ohair@286: return Injector.inject(cl, className, image); ohair@286: } ohair@286: ohair@286: private static class Field implements Comparable { ohair@286: private final java.lang.reflect.Type reflectType; ohair@286: private final Type asmType; ohair@286: private final String fieldName; ohair@286: private final List jaxbAnnotations; ohair@286: ohair@286: Field(String paramName, java.lang.reflect.Type paramType, Type asmType, ohair@286: List jaxbAnnotations) { ohair@286: this.reflectType = paramType; ohair@286: this.asmType = asmType; ohair@286: this.fieldName = paramName; ohair@286: this.jaxbAnnotations = jaxbAnnotations; ohair@286: } ohair@286: ohair@286: String getSignature() { ohair@286: if (reflectType instanceof Class) { ohair@286: return null; ohair@286: } ohair@286: if (reflectType instanceof TypeVariable) { ohair@286: return null; ohair@286: } ohair@286: return FieldSignature.vms(reflectType); ohair@286: } ohair@286: ohair@286: public int compareTo(Field o) { ohair@286: return fieldName.compareTo(o.fieldName); ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: static void write(byte[] b, String className) { ohair@286: className = className.substring(className.lastIndexOf(".")+1); ohair@286: try { ohair@286: java.io.FileOutputStream fo = new java.io.FileOutputStream(className + ".class"); ohair@286: fo.write(b); ohair@286: fo.flush(); ohair@286: fo.close(); ohair@286: } catch (java.io.IOException e) { ohair@286: // TODO Auto-generated catch block ohair@286: e.printStackTrace(); ohair@286: } ohair@286: } ohair@286: ohair@286: }