Search in sources :

Example 16 with MethodVisitor

use of org.apache.xbean.asm6.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 17 with MethodVisitor

use of org.apache.xbean.asm6.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 18 with MethodVisitor

use of org.apache.xbean.asm6.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 19 with MethodVisitor

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

the class MakeTxLookup method createNewHibernateStrategy.

private static void createNewHibernateStrategy(final File basedir, final String target, final String abstractJtaPlatformPackage) throws Exception {
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, target.replace('.', '/'), null, abstractJtaPlatformPackage + "/AbstractJtaPlatform", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, abstractJtaPlatformPackage + "/AbstractJtaPlatform", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateTransactionManager", "()Ljavax/transaction/TransactionManager;", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateUserTransaction", "()Ljavax/transaction/UserTransaction;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        final Label l1 = new Label();
        final Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "javax/naming/NamingException");
        mv.visitLabel(l0);
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/loader/SystemInstance", "get", "()Lorg/apache/openejb/loader/SystemInstance;", false);
        mv.visitLdcInsn(Type.getType("Lorg/apache/openejb/spi/ContainerSystem;"));
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/openejb/loader/SystemInstance", "getComponent", "(Ljava/lang/Class;)Ljava/lang/Object;", false);
        mv.visitTypeInsn(CHECKCAST, "org/apache/openejb/spi/ContainerSystem");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/openejb/spi/ContainerSystem", "getJNDIContext", "()Ljavax/naming/Context;", true);
        mv.visitLdcInsn("comp/UserTransaction");
        mv.visitMethodInsn(INVOKEINTERFACE, "javax/naming/Context", "lookup", "(Ljava/lang/String;)Ljava/lang/Object;", true);
        mv.visitTypeInsn(CHECKCAST, "javax/transaction/UserTransaction");
        mv.visitLabel(l1);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "javax/naming/NamingException" });
        mv.visitVarInsn(ASTORE, 1);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(basedir, cw, target.replace('.', '/'));
}
Also used : Label(org.apache.xbean.asm9.Label) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 20 with MethodVisitor

use of org.apache.xbean.asm6.MethodVisitor in project jodd by oblac.

the class ProxettaClassBuilder method makeStaticInitBlock.

/**
	 * Creates static initialization block that simply calls all
	 * advice static init methods in correct order.
	 */
protected void makeStaticInitBlock() {
    if (wd.adviceClinits != null) {
        MethodVisitor mv = wd.dest.visitMethod(AsmUtil.ACC_STATIC, CLINIT, DESC_VOID, null, null);
        mv.visitCode();
        for (String name : wd.adviceClinits) {
            mv.visitMethodInsn(INVOKESTATIC, wd.thisReference, name, DESC_VOID, false);
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}
Also used : MethodVisitor(jodd.asm5.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm9.MethodVisitor)30 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 Label (org.apache.xbean.asm9.Label)5 Type (org.apache.xbean.asm9.Type)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 ZipEntry (java.util.zip.ZipEntry)2 EmptyClassVisitor (jodd.asm.EmptyClassVisitor)2 EmptyMethodVisitor (jodd.asm.EmptyMethodVisitor)2 AnnotationVisitor (org.apache.xbean.asm6.AnnotationVisitor)2 ClassWriter (org.apache.xbean.asm6.ClassWriter)2 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)2 InterceptorHandler (org.talend.sdk.component.api.service.interceptor.InterceptorHandler)2 IOException (java.io.IOException)1