Search in sources :

Example 16 with MethodVisitor

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

the class Cmp2Generator method createEjbActivate.

public void createEjbActivate() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "ejbActivate", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 17 with MethodVisitor

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

the class ValidationGenerator method generate.

public byte[] generate() throws ProxyGenerationException {
    generatedMethods.clear();
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    final String generatedClassName = getName().replace('.', '/');
    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, generatedClassName, null, "java/lang/Object", null);
    {
        // public constructor
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    generateMethods(cw);
    /**
     * Read all parent classes and copy the public methods we need
     * into our new class.
     */
    Class current = clazz;
    while (current != null && !current.equals(Object.class)) {
        try {
            final ClassReader classReader = new ClassReader(DynamicSubclass.readClassFile(current));
            classReader.accept(new CopyMethodAnnotations(), ClassReader.SKIP_CODE);
        } catch (final IOException e) {
            throw new ProxyGenerationException(e);
        }
        current = current.getSuperclass();
    }
    return cw.toByteArray();
}
Also used : ProxyGenerationException(org.apache.openejb.util.proxy.ProxyGenerationException) ClassReader(org.apache.xbean.asm9.ClassReader) IOException(java.io.IOException) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 18 with MethodVisitor

use of org.apache.xbean.asm9.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.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 19 with MethodVisitor

use of org.apache.xbean.asm9.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.asm9.Type) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 20 with MethodVisitor

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

the class DynamicSubclass method copyMethodAnnotations.

public 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.asm9.ClassReader) ClassVisitor(org.apache.xbean.asm9.ClassVisitor) IOException(java.io.IOException)

Aggregations

MethodVisitor (org.apache.xbean.asm9.MethodVisitor)30 Label (org.apache.xbean.asm9.Label)7 MethodVisitor (jodd.asm5.MethodVisitor)6 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)6 ClassWriter (org.apache.xbean.asm9.ClassWriter)6 Method (java.lang.reflect.Method)5 Type (org.apache.xbean.asm9.Type)4 IOException (java.io.IOException)2 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 ZipEntry (java.util.zip.ZipEntry)2 EmptyClassVisitor (jodd.asm.EmptyClassVisitor)2 EmptyMethodVisitor (jodd.asm.EmptyMethodVisitor)2 ProxyGenerationException (org.apache.openejb.util.proxy.ProxyGenerationException)2 AnnotationVisitor (org.apache.xbean.asm6.AnnotationVisitor)2 ClassWriter (org.apache.xbean.asm6.ClassWriter)2 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)2