Search in sources :

Example 16 with MethodVisitor

use of org.apache.xbean.asm5.MethodVisitor in project tomee by apache.

the class DynamicSubclass method copyMethodAnnotations.

private static void copyMethodAnnotations(final Class<?> classToProxy, final Map<String, MethodVisitor> visitors) throws ProxyGenerationException {
    // Move all the annotations onto the newly implemented methods
    // Ensures CDI and JAX-RS and JAX-WS still work
    Class clazz = classToProxy;
    while (clazz != null && !clazz.equals(Object.class)) {
        try {
            final ClassReader classReader = new ClassReader(readClassFile(clazz));
            final ClassVisitor copyMethodAnnotations = new CopyMethodAnnotations(visitors);
            classReader.accept(copyMethodAnnotations, ClassReader.SKIP_CODE);
        } catch (final IOException e) {
            throw new ProxyGenerationException(e);
        }
        clazz = clazz.getSuperclass();
    }
}
Also used : ProxyGenerationException(org.apache.openejb.util.proxy.ProxyGenerationException) ClassReader(org.apache.xbean.asm5.ClassReader) ClassVisitor(org.apache.xbean.asm5.ClassVisitor) IOException(java.io.IOException)

Example 17 with MethodVisitor

use of org.apache.xbean.asm5.MethodVisitor in project tomee by apache.

the class DynamicSubclass method visitConstructor.

private static MethodVisitor visitConstructor(final ClassWriter cw, final String proxyClassFileName, final String classFileName, final Constructor<?> constructor) {
    final String descriptor = Type.getConstructorDescriptor(constructor);
    final String[] exceptions = new String[constructor.getExceptionTypes().length];
    for (int i = 0; i < exceptions.length; i++) {
        exceptions[i] = Type.getInternalName(constructor.getExceptionTypes()[i]);
    }
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    int index = 1;
    for (final Type type : Type.getArgumentTypes(descriptor)) {
        mv.visitVarInsn(type.getOpcode(ILOAD), index);
        index += size(type);
    }
    mv.visitMethodInsn(INVOKESPECIAL, classFileName, "<init>", descriptor, false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(PUTFIELD, proxyClassFileName, "this$handler", "Ljava/lang/reflect/InvocationHandler;");
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 1);
    return mv;
}
Also used : Type(org.apache.xbean.asm5.Type) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 18 with MethodVisitor

use of org.apache.xbean.asm5.MethodVisitor in project tomee by apache.

the class ServiceClasspathTest method subclass.

public static File subclass(final Class<?> parent, final String subclassName) throws Exception {
    final String subclassNameInternal = subclassName.replace('.', '/');
    final byte[] bytes;
    {
        final ClassWriter cw = new ClassWriter(0);
        final String parentClassNameInternal = parent.getName().replace('.', '/');
        cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + ACC_SUPER, subclassNameInternal, null, parentClassNameInternal, null);
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, parentClassNameInternal, "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
        cw.visitEnd();
        bytes = cw.toByteArray();
    }
    return Archive.archive().add(subclassNameInternal + ".class", bytes).asJar();
}
Also used : ClassWriter(org.apache.xbean.asm5.ClassWriter) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 19 with MethodVisitor

use of org.apache.xbean.asm5.MethodVisitor in project tomee by apache.

the class Cmp1Generator method createConstructor.

/**
 * Create a default constructor for our bean super
 * class.  This is just a forwarding constructor that
 * calls the superclass (the bean implementation class)
 * default constructor.
 */
private void createConstructor() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, beanClassName, "<init>", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 20 with MethodVisitor

use of org.apache.xbean.asm5.MethodVisitor in project tomee by apache.

the class Cmp2Generator method createOpenEJB_removeCmr.

private void createOpenEJB_removeCmr() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "OpenEJB_removeCmr", "(Ljava/lang/String;Ljava/lang/Object;)V", null, null);
    mv.visitCode();
    // if (deleted) return;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, implClassName, DELETED, "Z");
    final Label notDeleted = new Label();
    mv.visitJumpInsn(IFEQ, notDeleted);
    mv.visitInsn(RETURN);
    mv.visitLabel(notDeleted);
    for (final CmrField cmrField : cmrFields) {
        // if ("${cmrField.name}".equals(name)) {
        // ${cmrField.name}.remove(value);
        // return;
        // }
        // 
        // OR
        // 
        // if ("${cmrField.name}".equals(name)) {
        // ${cmrField.name} = null;
        // return;
        // }
        createOpenEJB_removeCmr(mv, cmrField);
    }
    // throw new IllegalArgumentException("Unknown cmr field " + name + " on entity bean of type " + getClass().getName());
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
    mv.visitLdcInsn("Unknown cmr field ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitLdcInsn(" on entity bean of type ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Label(org.apache.xbean.asm5.Label) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm5.MethodVisitor)28 Label (org.apache.xbean.asm5.Label)7 MethodVisitor (jodd.asm5.MethodVisitor)6 ClassWriter (org.apache.xbean.asm5.ClassWriter)6 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 EmptyClassVisitor (jodd.asm.EmptyClassVisitor)2 EmptyMethodVisitor (jodd.asm.EmptyMethodVisitor)2 Type (org.apache.xbean.asm5.Type)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 MethodAdapter (jodd.asm.MethodAdapter)1 FieldVisitor (jodd.asm5.FieldVisitor)1 Label (jodd.asm5.Label)1 ProxettaException (jodd.proxetta.ProxettaException)1 ProxyGenerationException (org.apache.openejb.util.proxy.ProxyGenerationException)1