Search in sources :

Example 21 with OpcodesProxy

use of org.apache.cxf.common.util.OpcodesProxy in project cxf by apache.

the class CorbaFixedAnyImplGenerator method createFixedAnyClass.

public Class<?> createFixedAnyClass() {
    ASMHelper helper = bus.getExtension(ASMHelper.class);
    OpcodesProxy opCodes = helper.getOpCodes();
    ASMHelper.ClassWriter cw = helper.createClassWriter();
    ASMHelper.FieldVisitor fv;
    cw.visit(opCodes.V1_6, opCodes.ACC_PUBLIC + opCodes.ACC_SUPER, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", null, "com/sun/corba/se/impl/corba/AnyImpl", null);
    cw.visitSource("FixedAnyImpl.java", null);
    fv = cw.visitField(0, "obj", "Lorg/omg/CORBA/portable/Streamable;", null, null);
    fv.visitEnd();
    addFixedAnyConstructor(helper, cw);
    addInsertOverride(helper, cw);
    addExtractOverride(helper, cw);
    addWriteOverride(helper, cw);
    addReadOverride(helper, cw);
    cw.visitEnd();
    byte[] b = cw.toByteArray();
    Class<?> c = loadClass("org.apache.cxf.binding.corba.utils.FixedAnyImpl", CorbaFixedAnyImplGenerator.class, b);
    return c;
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) ASMHelper(org.apache.cxf.common.util.ASMHelper)

Example 22 with OpcodesProxy

use of org.apache.cxf.common.util.OpcodesProxy in project cxf by apache.

the class FactoryClassGenerator method createFactory.

@SuppressWarnings("unused")
public Class<?> createFactory(Class<?> cls) {
    String newClassName = cls.getName() + "Factory";
    Class<?> factoryClass = findClass(newClassName, cls);
    if (factoryClass != null) {
        return factoryClass;
    }
    Constructor<?> contructor = ReflectionUtil.getDeclaredConstructors(cls)[0];
    OpcodesProxy opcodes = helper.getOpCodes();
    ASMHelper.ClassWriter cw = helper.createClassWriter();
    ASMHelper.MethodVisitor mv;
    cw.visit(opcodes.V1_6, opcodes.ACC_PUBLIC + opcodes.ACC_SUPER, StringUtils.periodToSlashes(newClassName), null, "java/lang/Object", null);
    cw.visitSource(cls.getSimpleName() + "Factory" + ".java", null);
    mv = cw.visitMethod(opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(opcodes.ALOAD, 0);
    mv.visitMethodInsn(opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(opcodes.ACC_PUBLIC, "create" + cls.getSimpleName(), "()L" + StringUtils.periodToSlashes(cls.getName()) + ";", null, null);
    mv.visitCode();
    String name = cls.getName().replace('.', '/');
    mv.visitTypeInsn(opcodes.NEW, name);
    mv.visitInsn(opcodes.DUP);
    StringBuilder paraString = new StringBuilder(32).append('(');
    for (Class<?> paraClass : contructor.getParameterTypes()) {
        mv.visitInsn(opcodes.ACONST_NULL);
        paraString.append("Ljava/lang/Object;");
    }
    paraString.append(")V");
    mv.visitMethodInsn(opcodes.INVOKESPECIAL, name, "<init>", paraString.toString(), false);
    mv.visitInsn(opcodes.ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    return loadClass(newClassName, cls, cw.toByteArray());
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) ASMHelper(org.apache.cxf.common.util.ASMHelper)

Example 23 with OpcodesProxy

use of org.apache.cxf.common.util.OpcodesProxy in project cxf by apache.

the class WrapperClassGenerator method generatePackageInfo.

// CHECKSTYLE:ON
private void generatePackageInfo(String className, String ns, Class<?> clz, Method method, InterfaceInfo interfaceInfo, boolean qualified) {
    ASMHelper.ClassWriter cw = helper.createClassWriter();
    String classFileName = StringUtils.periodToSlashes(className);
    OpcodesProxy opCodes = helper.getOpCodes();
    cw.visit(opCodes.V1_5, opCodes.ACC_ABSTRACT + opCodes.ACC_INTERFACE, classFileName, null, "java/lang/Object", null);
    boolean q = qualified;
    SchemaInfo si = interfaceInfo.getService().getSchema(ns);
    if (si != null) {
        q = si.isElementFormQualified();
    }
    ASMHelper.AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlSchema;", true);
    av0.visit("namespace", ns);
    av0.visitEnum("elementFormDefault", helper.getClassCode(XmlNsForm.class), q ? "QUALIFIED" : "UNQUALIFIED");
    av0.visitEnd();
    if (clz.getPackage() != null && clz.getPackage().getAnnotations() != null) {
        for (Annotation ann : clz.getPackage().getAnnotations()) {
            if (ann instanceof XmlJavaTypeAdapters) {
                av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapters;", true);
                generateXmlJavaTypeAdapters(av0, (XmlJavaTypeAdapters) ann);
                av0.visitEnd();
            } else if (ann instanceof XmlJavaTypeAdapter) {
                av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
                generateXmlJavaTypeAdapter(av0, (XmlJavaTypeAdapter) ann);
                av0.visitEnd();
            }
        }
    }
    cw.visitEnd();
    loadClass(className, method.getDeclaringClass(), cw.toByteArray());
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) XmlNsForm(javax.xml.bind.annotation.XmlNsForm) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) ASMHelper(org.apache.cxf.common.util.ASMHelper) XmlJavaTypeAdapters(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters) Annotation(java.lang.annotation.Annotation) SchemaInfo(org.apache.cxf.service.model.SchemaInfo)

Example 24 with OpcodesProxy

use of org.apache.cxf.common.util.OpcodesProxy in project cxf by apache.

the class ExtensionClassGenerator method createExtensionClass.

// CHECKSTYLE:OFF - very complicated ASM code
public Class<?> createExtensionClass(Class<?> cls, QName qname, ClassLoader loader) {
    String className = StringUtils.periodToSlashes(cls.getName());
    ASMHelper helper = bus.getExtension(ASMHelper.class);
    OpcodesProxy Opcodes = helper.getOpCodes();
    Class<?> extClass = findClass(className + "Extensibility", loader);
    if (extClass != null) {
        return extClass;
    }
    ASMHelper.ClassWriter cw = helper.createClassWriter();
    ASMHelper.FieldVisitor fv;
    ASMHelper.MethodVisitor mv;
    ASMHelper.AnnotationVisitor av0;
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC, className + "Extensibility", null, className, new String[] { "javax/wsdl/extensions/ExtensibilityElement" });
    cw.visitSource(cls.getSimpleName() + "Extensibility.java", null);
    fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;", null, null);
    fv.visitEnd();
    fv = cw.visitField(0, "qn", "Ljavax/xml/namespace/QName;", null, null);
    fv.visitEnd();
    boolean hasAttributes = false;
    try {
        Method m = cls.getDeclaredMethod("getOtherAttributes");
        if (m != null && m.getReturnType() == Map.class) {
            hasAttributes = true;
        }
    } catch (Throwable t) {
    // ignore
    }
    if (hasAttributes) {
        mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        ASMHelper.Label l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(64, l0);
        mv.visitTypeInsn(Opcodes.NEW, "javax/xml/namespace/QName");
        mv.visitInsn(Opcodes.DUP);
        mv.visitLdcInsn("http://schemas.xmlsoap.org/wsdl/");
        mv.visitLdcInsn("required");
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/xml/namespace/QName", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", false);
        mv.visitFieldInsn(Opcodes.PUTSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    } else {
        fv = cw.visitField(Opcodes.ACC_PRIVATE, "required", "Ljava/lang/Boolean;", null, null);
        fv.visitEnd();
    }
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    ASMHelper.Label l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(33, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", "()V", false);
    ASMHelper.Label l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitLineNumber(31, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitTypeInsn(Opcodes.NEW, "javax/xml/namespace/QName");
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn(qname.getNamespaceURI());
    mv.visitLdcInsn(qname.getLocalPart());
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/xml/namespace/QName", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", false);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className + "Extensibility", "qn", "Ljavax/xml/namespace/QName;");
    ASMHelper.Label l2 = helper.createLabel();
    mv.visitLabel(l2);
    mv.visitLineNumber(34, l2);
    mv.visitInsn(Opcodes.RETURN);
    ASMHelper.Label l3 = helper.createLabel();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l3, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setElementType", "(Ljavax/xml/namespace/QName;)V", null, null);
    mv.visitCode();
    l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(37, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className + "Extensibility", "qn", "Ljavax/xml/namespace/QName;");
    l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitLineNumber(38, l1);
    mv.visitInsn(Opcodes.RETURN);
    l2 = helper.createLabel();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l2, 0);
    mv.visitLocalVariable("elementType", "Ljavax/xml/namespace/QName;", null, l0, l2, 1);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getElementType", "()Ljavax/xml/namespace/QName;", null, null);
    av0 = mv.visitAnnotation("Ljavax/xml/bind/annotation/XmlTransient;", true);
    av0.visitEnd();
    mv.visitCode();
    l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(40, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, className + "Extensibility", "qn", "Ljavax/xml/namespace/QName;");
    mv.visitInsn(Opcodes.ARETURN);
    l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    if (hasAttributes) {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getRequired", "()Ljava/lang/Boolean;", null, null);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(66, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className + "Extensibility", "getOtherAttributes", "()Ljava/util/Map;", false);
        mv.visitFieldInsn(Opcodes.GETSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String");
        mv.visitVarInsn(Opcodes.ASTORE, 1);
        l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLineNumber(67, l1);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        l2 = helper.createLabel();
        mv.visitJumpInsn(Opcodes.IFNONNULL, l2);
        mv.visitInsn(Opcodes.ACONST_NULL);
        l3 = helper.createLabel();
        mv.visitJumpInsn(Opcodes.GOTO, l3);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/String" }, 0, null);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Ljava/lang/String;)Ljava/lang/Boolean;", false);
        mv.visitLabel(l3);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Boolean" });
        mv.visitInsn(Opcodes.ARETURN);
        ASMHelper.Label l4 = helper.createLabel();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l4, 0);
        mv.visitLocalVariable("s", "Ljava/lang/String;", null, l1, l4, 1);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setRequired", "(Ljava/lang/Boolean;)V", null, null);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(76, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        l1 = helper.createLabel();
        mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
        l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLineNumber(77, l2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className + "Extensibility", "getOtherAttributes", "()Ljava/util/Map;", false);
        mv.visitFieldInsn(Opcodes.GETSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "remove", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(Opcodes.POP);
        l3 = helper.createLabel();
        mv.visitLabel(l3);
        mv.visitLineNumber(78, l3);
        l4 = helper.createLabel();
        mv.visitJumpInsn(Opcodes.GOTO, l4);
        mv.visitLabel(l1);
        mv.visitLineNumber(79, l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className + "Extensibility", "getOtherAttributes", "()Ljava/util/Map;", false);
        mv.visitFieldInsn(Opcodes.GETSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "toString", "()Ljava/lang/String;", false);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitInsn(Opcodes.POP);
        mv.visitLabel(l4);
        mv.visitLineNumber(81, l4);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitInsn(Opcodes.RETURN);
        ASMHelper.Label l5 = helper.createLabel();
        mv.visitLabel(l5);
        mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l5, 0);
        mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l5, 1);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    } else {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getRequired", "()Ljava/lang/Boolean;", null, null);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(68, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, className + "Extensibility", "required", "Ljava/lang/Boolean;");
        mv.visitInsn(Opcodes.ARETURN);
        l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l1, 0);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setRequired", "(Ljava/lang/Boolean;)V", null, null);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(71, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className + "Extensibility", "required", "Ljava/lang/Boolean;");
        l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLineNumber(72, l1);
        mv.visitInsn(Opcodes.RETURN);
        l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l2, 0);
        mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    byte[] bytes = cw.toByteArray();
    return loadClass(className + "Extensibility", loader, bytes);
}
Also used : ASMHelper(org.apache.cxf.common.util.ASMHelper) Method(java.lang.reflect.Method) OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) Map(java.util.Map)

Aggregations

OpcodesProxy (org.apache.cxf.common.util.OpcodesProxy)24 ASMHelper (org.apache.cxf.common.util.ASMHelper)23 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 Map (java.util.Map)1 JAXBElement (javax.xml.bind.JAXBElement)1 XmlMimeType (javax.xml.bind.annotation.XmlMimeType)1 XmlNsForm (javax.xml.bind.annotation.XmlNsForm)1 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 XmlJavaTypeAdapters (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters)1 QName (javax.xml.namespace.QName)1 Holder (javax.xml.ws.Holder)1 WrapperHelper (org.apache.cxf.databinding.WrapperHelper)1 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)1 SchemaInfo (org.apache.cxf.service.model.SchemaInfo)1