Search in sources :

Example 1 with FieldVisitor

use of org.apache.cxf.common.util.ASMHelper.FieldVisitor in project cxf by apache.

the class JAXBUtils method createNamespaceWrapperInternal.

private static Class<?> createNamespaceWrapperInternal(ASMHelper helper, ClassWriter cw, String postFix, Class<?> ref) {
    String className = "org.apache.cxf.jaxb.NamespaceMapper" + postFix;
    String superName = "com/sun/xml/" + ("RI".equals(postFix) ? "" : "internal/") + "bind/marshaller/NamespacePrefixMapper";
    String postFixedName = "org/apache/cxf/jaxb/NamespaceMapper" + postFix;
    FieldVisitor fv;
    MethodVisitor mv;
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, postFixedName, null, superName, null);
    cw.visitSource("NamespaceMapper.java", null);
    fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "nspref", "Ljava/util/Map;", "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", null);
    fv.visitEnd();
    fv = cw.visitField(Opcodes.ACC_PRIVATE, "nsctxt", "[Ljava/lang/String;", null, null);
    fv.visitEnd();
    fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "EMPTY_STRING", "[Ljava/lang/String;", null, null);
    fv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    Label l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(30, l0);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
    mv.visitFieldInsn(Opcodes.PUTSTATIC, postFixedName, "EMPTY_STRING", "[Ljava/lang/String;");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 0);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/util/Map;)V", "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V", null);
    mv.visitCode();
    l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(32, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "()V", false);
    Label l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitLineNumber(29, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETSTATIC, postFixedName, "EMPTY_STRING", "[Ljava/lang/String;");
    mv.visitFieldInsn(Opcodes.PUTFIELD, postFixedName, "nsctxt", "[Ljava/lang/String;");
    Label l2 = helper.createLabel();
    mv.visitLabel(l2);
    mv.visitLineNumber(33, l2);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, postFixedName, "nspref", "Ljava/util/Map;");
    Label l3 = helper.createLabel();
    mv.visitLabel(l3);
    mv.visitLineNumber(34, l3);
    mv.visitInsn(Opcodes.RETURN);
    Label l4 = helper.createLabel();
    mv.visitLabel(l4);
    mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l4, 0);
    mv.visitLocalVariable("nspref", "Ljava/util/Map;", "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", l0, l4, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPreferredPrefix", "(Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;", null, null);
    mv.visitCode();
    l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(39, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, postFixedName, "nspref", "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    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, 4);
    l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitLineNumber(40, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    l2 = helper.createLabel();
    mv.visitJumpInsn(Opcodes.IFNULL, l2);
    l3 = helper.createLabel();
    mv.visitLabel(l3);
    mv.visitLineNumber(41, l3);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitLabel(l2);
    mv.visitLineNumber(43, l2);
    mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/String" }, 0, null);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitInsn(Opcodes.ARETURN);
    l4 = helper.createLabel();
    mv.visitLabel(l4);
    mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l4, 0);
    mv.visitLocalVariable("namespaceUri", "Ljava/lang/String;", null, l0, l4, 1);
    mv.visitLocalVariable("suggestion", "Ljava/lang/String;", null, l0, l4, 2);
    mv.visitLocalVariable("requirePrefix", "Z", null, l0, l4, 3);
    mv.visitLocalVariable("prefix", "Ljava/lang/String;", null, l1, l4, 4);
    mv.visitMaxs(2, 5);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setContextualNamespaceDecls", "([Ljava/lang/String;)V", null, null);
    mv.visitCode();
    l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(47, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, postFixedName, "nsctxt", "[Ljava/lang/String;");
    l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitLineNumber(48, l1);
    mv.visitInsn(Opcodes.RETURN);
    l2 = helper.createLabel();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l2, 0);
    mv.visitLocalVariable("contextualNamespaceDecls", "[Ljava/lang/String;", null, l0, l2, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getContextualNamespaceDecls", "()[Ljava/lang/String;", null, null);
    mv.visitCode();
    l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(51, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, postFixedName, "nsctxt", "[Ljava/lang/String;");
    mv.visitInsn(Opcodes.ARETURN);
    l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    byte[] bts = cw.toByteArray();
    return helper.loadClass(className, ref, bts);
}
Also used : Label(org.apache.cxf.common.util.ASMHelper.Label) FieldVisitor(org.apache.cxf.common.util.ASMHelper.FieldVisitor) MethodVisitor(org.apache.cxf.common.util.ASMHelper.MethodVisitor)

Example 2 with FieldVisitor

use of org.apache.cxf.common.util.ASMHelper.FieldVisitor in project cxf by apache.

the class JAXBExtensionHelper method createExtensionClass.

// CHECKSTYLE:OFF - very complicated ASM code
private static Class<?> createExtensionClass(Class<?> cls, QName qname, ClassLoader loader) {
    String className = ASMHelper.periodToSlashes(cls.getName());
    ASMHelper helper = new ASMHelper();
    Class<?> extClass = helper.findClass(className + "Extensibility", loader);
    if (extClass != null) {
        return extClass;
    }
    ClassWriter cw = helper.createClassWriter();
    FieldVisitor fv;
    MethodVisitor mv;
    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();
        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(4, 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();
    Label l0 = helper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(33, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", "()V", false);
    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;");
    Label l2 = helper.createLabel();
    mv.visitLabel(l2);
    mv.visitLineNumber(34, l2);
    mv.visitInsn(Opcodes.RETURN);
    Label l3 = helper.createLabel();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l3, 0);
    mv.visitMaxs(5, 1);
    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(2, 2);
    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(1, 1);
    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);
        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(2, 2);
        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);
        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(3, 2);
        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(1, 1);
        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(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    byte[] bytes = cw.toByteArray();
    return helper.loadClass(className + "Extensibility", loader, bytes);
}
Also used : Label(org.apache.cxf.common.util.ASMHelper.Label) ASMHelper(org.apache.cxf.common.util.ASMHelper) Method(java.lang.reflect.Method) FieldVisitor(org.apache.cxf.common.util.ASMHelper.FieldVisitor) ClassWriter(org.apache.cxf.common.util.ASMHelper.ClassWriter) MethodVisitor(org.apache.cxf.common.util.ASMHelper.MethodVisitor) AnnotationVisitor(org.apache.cxf.common.util.ASMHelper.AnnotationVisitor) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with FieldVisitor

use of org.apache.cxf.common.util.ASMHelper.FieldVisitor in project cxf by apache.

the class CorbaAnyHelper method createFixedAnyConstructor.

private static synchronized void createFixedAnyConstructor() {
    if (fixedAnyConstructor != null) {
        return;
    }
    ASMHelper helper = new ASMHelper();
    ClassWriter cw = helper.createClassWriter();
    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 = helper.loadClass("org.apache.cxf.binding.corba.utils.FixedAnyImpl", CorbaAnyHelper.class, b);
    try {
        fixedAnyConstructor = c.getConstructor(ORB.class, Any.class);
    } catch (Exception e) {
    // shouldn't happen since we generated that constructor
    }
}
Also used : ASMHelper(org.apache.cxf.common.util.ASMHelper) FieldVisitor(org.apache.cxf.common.util.ASMHelper.FieldVisitor) Any(org.omg.CORBA.Any) ClassWriter(org.apache.cxf.common.util.ASMHelper.ClassWriter) ORB(org.omg.CORBA.ORB) CorbaBindingException(org.apache.cxf.binding.corba.CorbaBindingException)

Example 4 with FieldVisitor

use of org.apache.cxf.common.util.ASMHelper.FieldVisitor in project cxf by apache.

the class JAXBUtils method createEclipseNamespaceMapper.

/*
    // This is the "prototype" for the ASM generated class below
    public static class MapNamespacePrefixMapper2
        extends org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper {

        String nsctxt[];

        public MapNamespacePrefixMapper2(Map<String, String> foo) {
            super(foo);
        }
        public String[] getPreDeclaredNamespaceUris() {
            String sup[] = super.getPreDeclaredNamespaceUris();
            if (nsctxt == null) {
                return sup;
            }
            List<String> s = new ArrayList<>(Arrays.asList(sup));
            for (int x = 1; x < nsctxt.length; x = x + 2) {
                s.remove(nsctxt[x]);
            }
            return s.toArray(new String[s.size()]);
        }
        public void setContextualNamespaceDecls(String f[]) {
            nsctxt = f;
        }
        public String[] getContextualNamespaceDecls() {
            return nsctxt;
        }
    }
    */
// CHECKSTYLE:OFF
// bunch of really long ASM based methods that cannot be shortened easily
private static Object createEclipseNamespaceMapper(Class<?> mcls, Map<String, String> map) {
    ASMHelper helper = new ASMHelper();
    String className = "org.apache.cxf.jaxb.EclipseNamespaceMapper";
    String slashedName = "org/apache/cxf/jaxb/EclipseNamespaceMapper";
    Class<?> cls = helper.findClass(className, JAXBUtils.class);
    if (cls == null) {
        ClassWriter cw = helper.createClassWriter();
        if (cw == null) {
            return null;
        }
        String superName = "org/eclipse/persistence/internal/oxm/record/namespaces/MapNamespacePrefixMapper";
        FieldVisitor fv;
        MethodVisitor mv;
        cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER, slashedName, null, superName, null);
        cw.visitSource("EclipseNamespaceMapper.java", null);
        fv = cw.visitField(Opcodes.ACC_PRIVATE, "nsctxt", "[Ljava/lang/String;", null, null);
        fv.visitEnd();
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/util/Map;)V", "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V", null);
        mv.visitCode();
        Label l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "(Ljava/util/Map;)V", false);
        Label l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitInsn(Opcodes.RETURN);
        Label l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setContextualNamespaceDecls", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(47, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
        l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLineNumber(48, l1);
        mv.visitInsn(Opcodes.RETURN);
        l2 = helper.createLabel();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + slashedName + ";", null, l0, l2, 0);
        mv.visitLocalVariable("contextualNamespaceDecls", "[Ljava/lang/String;", null, l0, l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getContextualNamespaceDecls", "()[Ljava/lang/String;", null, null);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(51, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
        mv.visitInsn(Opcodes.ARETURN);
        l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + slashedName + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPreDeclaredNamespaceUris", "()[Ljava/lang/String;", null, null);
        mv.visitCode();
        l0 = helper.createLabel();
        mv.visitLabel(l0);
        mv.visitLineNumber(1036, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "getPreDeclaredNamespaceUris", "()[Ljava/lang/String;", false);
        mv.visitVarInsn(Opcodes.ASTORE, 1);
        l1 = helper.createLabel();
        mv.visitLabel(l1);
        mv.visitLineNumber(1037, l1);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
        l2 = helper.createLabel();
        mv.visitJumpInsn(Opcodes.IFNONNULL, l2);
        Label l3 = helper.createLabel();
        mv.visitLabel(l3);
        mv.visitLineNumber(1038, l3);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitLabel(l2);
        mv.visitLineNumber(1040, l2);
        mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "[Ljava/lang/String;" }, 0, null);
        mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;", false);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "(Ljava/util/Collection;)V", false);
        mv.visitVarInsn(Opcodes.ASTORE, 2);
        Label l4 = helper.createLabel();
        mv.visitLabel(l4);
        mv.visitLineNumber(1041, l4);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitVarInsn(Opcodes.ISTORE, 3);
        Label l5 = helper.createLabel();
        mv.visitLabel(l5);
        Label l6 = helper.createLabel();
        mv.visitJumpInsn(Opcodes.GOTO, l6);
        Label l7 = helper.createLabel();
        mv.visitLabel(l7);
        mv.visitLineNumber(1042, l7);
        mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] { "java/util/List", Opcodes.INTEGER }, 0, null);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
        mv.visitVarInsn(Opcodes.ILOAD, 3);
        mv.visitInsn(Opcodes.AALOAD);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "remove", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(Opcodes.POP);
        Label l8 = helper.createLabel();
        mv.visitLabel(l8);
        mv.visitLineNumber(1041, l8);
        mv.visitIincInsn(3, 2);
        mv.visitLabel(l6);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(Opcodes.ILOAD, 3);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
        mv.visitInsn(Opcodes.ARRAYLENGTH);
        mv.visitJumpInsn(Opcodes.IF_ICMPLT, l7);
        Label l9 = helper.createLabel();
        mv.visitLabel(l9);
        mv.visitLineNumber(1044, l9);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "size", "()I", true);
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", true);
        mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/String;");
        mv.visitInsn(Opcodes.ARETURN);
        Label l10 = helper.createLabel();
        mv.visitLabel(l10);
        mv.visitLocalVariable("this", "L" + slashedName + ";", null, l0, l10, 0);
        mv.visitLocalVariable("sup", "[Ljava/lang/String;", null, l1, l10, 1);
        mv.visitLocalVariable("s", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/String;>;", l4, l10, 2);
        mv.visitLocalVariable("x", "I", null, l5, l9, 3);
        mv.visitMaxs(3, 4);
        mv.visitEnd();
        cw.visitEnd();
        byte[] bts = cw.toByteArray();
        cls = helper.loadClass(className, mcls, bts);
    }
    try {
        return cls.getConstructor(Map.class).newInstance(map);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Label(org.apache.cxf.common.util.ASMHelper.Label) ASMHelper(org.apache.cxf.common.util.ASMHelper) FieldVisitor(org.apache.cxf.common.util.ASMHelper.FieldVisitor) Map(java.util.Map) HashMap(java.util.HashMap) ClassWriter(org.apache.cxf.common.util.ASMHelper.ClassWriter) MethodVisitor(org.apache.cxf.common.util.ASMHelper.MethodVisitor)

Aggregations

FieldVisitor (org.apache.cxf.common.util.ASMHelper.FieldVisitor)4 ASMHelper (org.apache.cxf.common.util.ASMHelper)3 ClassWriter (org.apache.cxf.common.util.ASMHelper.ClassWriter)3 Label (org.apache.cxf.common.util.ASMHelper.Label)3 MethodVisitor (org.apache.cxf.common.util.ASMHelper.MethodVisitor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Method (java.lang.reflect.Method)1 CorbaBindingException (org.apache.cxf.binding.corba.CorbaBindingException)1 AnnotationVisitor (org.apache.cxf.common.util.ASMHelper.AnnotationVisitor)1 Any (org.omg.CORBA.Any)1 ORB (org.omg.CORBA.ORB)1