Search in sources :

Example 1 with OpcodesProxy

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

the class NamespaceClassGenerator method doCreateEclipseNamespaceMapper.

/*
    // 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 byte[] doCreateEclipseNamespaceMapper() {
    OpcodesProxy Opcodes = helper.getOpCodes();
    String slashedName = "org/apache/cxf/jaxb/EclipseNamespaceMapper";
    ASMHelper.ClassWriter cw = helper.createClassWriter();
    if (cw == null) {
        return null;
    }
    String superName = "org/eclipse/persistence/internal/oxm/record/namespaces/MapNamespacePrefixMapper";
    ASMHelper.FieldVisitor fv;
    ASMHelper.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();
    ASMHelper.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);
    ASMHelper.Label l1 = helper.createLabel();
    mv.visitLabel(l1);
    mv.visitInsn(Opcodes.RETURN);
    ASMHelper.Label l2 = helper.createLabel();
    mv.visitLabel(l2);
    mv.visitMaxs(0, 0);
    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(0, 0);
    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(0, 0);
    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);
    ASMHelper.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);
    ASMHelper.Label l4 = helper.createLabel();
    mv.visitLabel(l4);
    mv.visitLineNumber(1041, l4);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitVarInsn(Opcodes.ISTORE, 3);
    ASMHelper.Label l5 = helper.createLabel();
    mv.visitLabel(l5);
    ASMHelper.Label l6 = helper.createLabel();
    mv.visitJumpInsn(Opcodes.GOTO, l6);
    ASMHelper.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);
    ASMHelper.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);
    ASMHelper.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);
    ASMHelper.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(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) ASMHelper(org.apache.cxf.common.util.ASMHelper)

Example 2 with OpcodesProxy

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

the class WrapperHelperClassGenerator method addConstructor.

private void addConstructor(String newClassName, Class<?> objectFactoryCls, ASMHelper asmhelper, ASMHelper.ClassWriter cw) {
    if (objectFactoryCls != null) {
        String ofName = "L" + StringUtils.periodToSlashes(objectFactoryCls.getName()) + ";";
        ASMHelper.FieldVisitor fv = cw.visitField(0, "factory", ofName, null, null);
        fv.visitEnd();
    }
    OpcodesProxy opCodes = asmhelper.getOpCodes();
    ASMHelper.MethodVisitor mv = cw.visitMethod(opCodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    ASMHelper.Label l0 = asmhelper.createLabel();
    mv.visitLabel(l0);
    mv.visitLineNumber(102, l0);
    mv.visitVarInsn(opCodes.ALOAD, 0);
    mv.visitMethodInsn(opCodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    if (objectFactoryCls != null) {
        mv.visitVarInsn(opCodes.ALOAD, 0);
        mv.visitTypeInsn(opCodes.NEW, StringUtils.periodToSlashes(objectFactoryCls.getName()));
        mv.visitInsn(opCodes.DUP);
        mv.visitMethodInsn(opCodes.INVOKESPECIAL, StringUtils.periodToSlashes(objectFactoryCls.getName()), "<init>", "()V", false);
        mv.visitFieldInsn(opCodes.PUTFIELD, StringUtils.periodToSlashes(newClassName), "factory", "L" + StringUtils.periodToSlashes(objectFactoryCls.getName()) + ";");
    }
    mv.visitInsn(opCodes.RETURN);
    ASMHelper.Label l1 = asmhelper.createLabel();
    mv.visitLabel(l1);
    mv.visitLineNumber(103, l0);
    mv.visitLocalVariable("this", "L" + newClassName + ";", null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) ASMHelper(org.apache.cxf.common.util.ASMHelper)

Example 3 with OpcodesProxy

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

the class WrapperHelperClassGenerator method createObjectWrapper.

private void createObjectWrapper(ASMHelper.MethodVisitor mv, Class<?> cl, ASMHelper asmhelper) {
    OpcodesProxy opCodes = asmhelper.getOpCodes();
    mv.visitMethodInsn(opCodes.INVOKESTATIC, asmhelper.getNonPrimitive(cl), "valueOf", "(" + asmhelper.getPrimitive(cl) + ")L" + asmhelper.getNonPrimitive(cl) + ";", false);
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy)

Example 4 with OpcodesProxy

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

the class WrapperHelperClassGenerator method compile.

public WrapperHelper compile(Class<?> wrapperType, Method[] setMethods, Method[] getMethods, Method[] jaxbMethods, Field[] fields, Object objectFactory) {
    ASMHelper asmhelper = bus.getExtension(ASMHelper.class);
    ASMHelper.ClassWriter cw = asmhelper.createClassWriter();
    if (cw == null) {
        return null;
    }
    int count = 1;
    String newClassName = wrapperType.getName() + "_WrapperTypeHelper" + count;
    newClassName = newClassName.replaceAll("\\$", ".");
    newClassName = StringUtils.periodToSlashes(newClassName);
    Class<?> cls = findClass(StringUtils.slashesToPeriod(newClassName), wrapperType);
    while (cls != null) {
        try {
            WrapperHelper helper = WrapperHelper.class.cast(cls.newInstance());
            if (!helper.getSignature().equals(computeSignature(setMethods, getMethods))) {
                count++;
                newClassName = wrapperType.getName() + "_WrapperTypeHelper" + count;
                newClassName = newClassName.replaceAll("\\$", ".");
                newClassName = StringUtils.periodToSlashes(newClassName);
                cls = findClass(StringUtils.slashesToPeriod(newClassName), wrapperType);
            } else {
                return helper;
            }
        } catch (Exception e) {
            return null;
        }
    }
    OpcodesProxy opCodes = asmhelper.getOpCodes();
    cw.visit(opCodes.V1_5, opCodes.ACC_PUBLIC | opCodes.ACC_SUPER, newClassName, null, "java/lang/Object", new String[] { StringUtils.periodToSlashes(WrapperHelper.class.getName()) });
    addConstructor(newClassName, objectFactory == null ? null : objectFactory.getClass(), asmhelper, cw);
    boolean b = addSignature(setMethods, getMethods, asmhelper, cw);
    if (b) {
        b = addCreateWrapperObject(newClassName, objectFactory == null ? null : objectFactory.getClass(), wrapperType, setMethods, getMethods, jaxbMethods, fields, asmhelper, cw);
    }
    if (b) {
        b = addGetWrapperParts(newClassName, wrapperType, getMethods, fields, asmhelper, cw);
    }
    try {
        if (b) {
            cw.visitEnd();
            byte[] bt = cw.toByteArray();
            Class<?> cl = loadClass(StringUtils.slashesToPeriod(newClassName), wrapperType, bt);
            Object o = cl.newInstance();
            return WrapperHelper.class.cast(o);
        }
    } catch (Throwable e) {
    // ignore, we'll just fall down to reflection based
    }
    return null;
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) ASMHelper(org.apache.cxf.common.util.ASMHelper) WrapperHelper(org.apache.cxf.databinding.WrapperHelper)

Example 5 with OpcodesProxy

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

the class WrapperHelperClassGenerator method addCreateWrapperObject.

// CHECKSTYLE:OFF
private boolean addCreateWrapperObject(String newClassName, Class<?> objectFactoryClass, Class<?> wrapperType, Method[] setMethods, Method[] getMethods, Method[] jaxbMethods, Field[] fields, ASMHelper asmhelper, ASMHelper.ClassWriter cw) {
    OpcodesProxy opCodes = asmhelper.getOpCodes();
    ASMHelper.MethodVisitor mv = cw.visitMethod(opCodes.ACC_PUBLIC, "createWrapperObject", "(Ljava/util/List;)Ljava/lang/Object;", "(Ljava/util/List<*>;)Ljava/lang/Object;", new String[] { "org/apache/cxf/interceptor/Fault" });
    mv.visitCode();
    ASMHelper.Label lBegin = asmhelper.createLabel();
    mv.visitLabel(lBegin);
    mv.visitLineNumber(104, lBegin);
    mv.visitTypeInsn(opCodes.NEW, StringUtils.periodToSlashes(wrapperType.getName()));
    mv.visitInsn(opCodes.DUP);
    mv.visitMethodInsn(opCodes.INVOKESPECIAL, StringUtils.periodToSlashes(wrapperType.getName()), "<init>", "()V", false);
    mv.visitVarInsn(opCodes.ASTORE, 2);
    for (int x = 0; x < setMethods.length; x++) {
        if (getMethods[x] == null) {
            if (setMethods[x] == null && fields[x] == null) {
                // null placeholder
                continue;
            }
            return false;
        }
        Class<?> tp = getMethods[x].getReturnType();
        mv.visitVarInsn(opCodes.ALOAD, 2);
        if (List.class.isAssignableFrom(tp)) {
            doCollection(mv, x, wrapperType, setMethods, getMethods, asmhelper);
        } else {
            if (JAXBElement.class.isAssignableFrom(tp)) {
                mv.visitVarInsn(opCodes.ALOAD, 0);
                mv.visitFieldInsn(opCodes.GETFIELD, StringUtils.periodToSlashes(newClassName), "factory", "L" + StringUtils.periodToSlashes(objectFactoryClass.getName()) + ";");
            }
            mv.visitVarInsn(opCodes.ALOAD, 1);
            mv.visitIntInsn(opCodes.SIPUSH, x);
            mv.visitMethodInsn(opCodes.INVOKEINTERFACE, "java/util/List", "get", "(I)Ljava/lang/Object;", true);
            if (tp.isPrimitive()) {
                mv.visitTypeInsn(opCodes.CHECKCAST, asmhelper.getNonPrimitive(tp));
                ASMHelper.Label l45 = asmhelper.createLabel();
                ASMHelper.Label l46 = asmhelper.createLabel();
                mv.visitInsn(opCodes.DUP);
                mv.visitJumpInsn(opCodes.IFNULL, l45);
                mv.visitMethodInsn(opCodes.INVOKEVIRTUAL, asmhelper.getNonPrimitive(tp), tp.getName() + "Value", "()" + asmhelper.getPrimitive(tp), false);
                mv.visitMethodInsn(opCodes.INVOKEVIRTUAL, StringUtils.periodToSlashes(wrapperType.getName()), setMethods[x].getName(), "(" + asmhelper.getClassCode(tp) + ")V", false);
                mv.visitJumpInsn(opCodes.GOTO, l46);
                mv.visitLabel(l45);
                mv.visitInsn(opCodes.POP);
                mv.visitLabel(l46);
            } else if (JAXBElement.class.isAssignableFrom(tp)) {
                mv.visitTypeInsn(opCodes.CHECKCAST, StringUtils.periodToSlashes(jaxbMethods[x].getParameterTypes()[0].getName()));
                mv.visitMethodInsn(opCodes.INVOKEVIRTUAL, StringUtils.periodToSlashes(objectFactoryClass.getName()), jaxbMethods[x].getName(), asmhelper.getMethodSignature(jaxbMethods[x]), false);
                mv.visitMethodInsn(opCodes.INVOKEVIRTUAL, StringUtils.periodToSlashes(wrapperType.getName()), setMethods[x].getName(), "(" + asmhelper.getClassCode(tp) + ")V", false);
            } else if (tp.isArray()) {
                mv.visitTypeInsn(opCodes.CHECKCAST, asmhelper.getClassCode(tp));
                mv.visitMethodInsn(opCodes.INVOKEVIRTUAL, StringUtils.periodToSlashes(wrapperType.getName()), setMethods[x].getName(), "(" + asmhelper.getClassCode(tp) + ")V", false);
            } else {
                mv.visitTypeInsn(opCodes.CHECKCAST, StringUtils.periodToSlashes(tp.getName()));
                mv.visitMethodInsn(opCodes.INVOKEVIRTUAL, StringUtils.periodToSlashes(wrapperType.getName()), setMethods[x].getName(), "(" + asmhelper.getClassCode(tp) + ")V", false);
            }
        }
    }
    mv.visitVarInsn(opCodes.ALOAD, 2);
    mv.visitInsn(opCodes.ARETURN);
    ASMHelper.Label lEnd = asmhelper.createLabel();
    mv.visitLabel(lEnd);
    mv.visitLocalVariable("this", "L" + newClassName + ";", null, lBegin, lEnd, 0);
    mv.visitLocalVariable("lst", "Ljava/util/List;", "Ljava/util/List<*>;", lBegin, lEnd, 1);
    mv.visitLocalVariable("ok", "L" + StringUtils.periodToSlashes(wrapperType.getName()) + ";", null, lBegin, lEnd, 2);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    return true;
}
Also used : OpcodesProxy(org.apache.cxf.common.util.OpcodesProxy) ASMHelper(org.apache.cxf.common.util.ASMHelper) JAXBElement(javax.xml.bind.JAXBElement)

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