Example usage for org.objectweb.asm Opcodes ASM5

List of usage examples for org.objectweb.asm Opcodes ASM5

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ASM5.

Prototype

int ASM5

To view the source code for org.objectweb.asm Opcodes ASM5.

Click Source Link

Usage

From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java

License:Apache License

private static byte[] generateProxyClassBytes(Class<?> targetClass, String proxyName,
        String superAccessorMethodSuffix, Class<?>[] additionalInterfaces,
        java.lang.reflect.Method[] delegateMethods, java.lang.reflect.Method[] interceptMethods) {
    Class<?> superClass = targetClass;
    String[] interfaces = new String[] {};

    if (targetClass.isInterface()) {
        superClass = Object.class;
        interfaces = new String[] { Type.getInternalName(targetClass) };
    }/*from   w  w w. ja  v a  2  s.  c o  m*/

    // add DeltaSpikeProxy as interface
    interfaces = Arrays.copyOf(interfaces, interfaces.length + 1);
    interfaces[interfaces.length - 1] = Type.getInternalName(DeltaSpikeProxy.class);

    if (additionalInterfaces != null && additionalInterfaces.length > 0) {
        interfaces = Arrays.copyOf(interfaces, interfaces.length + additionalInterfaces.length);
        for (int i = 0; i < additionalInterfaces.length; i++) {
            interfaces[(interfaces.length - 1) + i] = Type.getInternalName(additionalInterfaces[i]);
        }
    }

    Type superType = Type.getType(superClass);
    Type proxyType = Type.getObjectType(proxyName);

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, proxyType.getInternalName(), null,
            superType.getInternalName(), interfaces);

    defineDefaultConstructor(cw, proxyType, superType);
    defineDeltaSpikeProxyFields(cw);
    defineDeltaSpikeProxyMethods(cw, proxyType);

    if (delegateMethods != null) {
        for (java.lang.reflect.Method method : delegateMethods) {
            defineMethod(cw, method, proxyType);
        }
    }

    if (interceptMethods != null) {
        for (java.lang.reflect.Method method : interceptMethods) {
            defineSuperAccessorMethod(cw, method, superType, superAccessorMethodSuffix);
            defineMethod(cw, method, proxyType);
        }
    }

    // copy all annotations from the source class
    try {
        // ClassVisitor to intercept all annotation visits on the class
        ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                return new CopyAnnotationVisitorAdapter(super.visitAnnotation(desc, visible),
                        cw.visitAnnotation(desc, visible));
            }
        };

        // visit class to proxy with our visitor to copy all annotations from the source class to our ClassWriter
        String sourceClassFilename = targetClass.getName().replace('.', '/') + ".class";
        ClassReader cr = new ClassReader(targetClass.getClassLoader().getResourceAsStream(sourceClassFilename));
        cr.accept(cv, 0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return cw.toByteArray();
}

From source file:org.apache.deltaspike.proxy.impl.AsmProxyClassGenerator.java

License:Apache License

private static byte[] generateProxyClassBytes(Class<?> targetClass, String proxyName,
        String superAccessorMethodSuffix, Class<?>[] additionalInterfaces,
        java.lang.reflect.Method[] delegateMethods, java.lang.reflect.Method[] interceptMethods) {
    Class<?> superClass = targetClass;
    String[] interfaces = new String[] {};

    if (targetClass.isInterface()) {
        superClass = Object.class;
        interfaces = new String[] { Type.getInternalName(targetClass) };
    }//from w  w  w. j a  v a2 s.com

    // add DeltaSpikeProxy as interface
    interfaces = Arrays.copyOf(interfaces, interfaces.length + 1);
    interfaces[interfaces.length - 1] = Type.getInternalName(DeltaSpikeProxy.class);

    if (additionalInterfaces != null && additionalInterfaces.length > 0) {
        interfaces = Arrays.copyOf(interfaces, interfaces.length + additionalInterfaces.length);
        for (int i = 0; i < additionalInterfaces.length; i++) {
            interfaces[(interfaces.length - 1) + i] = Type.getInternalName(additionalInterfaces[i]);
        }
    }

    Type superType = Type.getType(superClass);
    Type proxyType = Type.getObjectType(proxyName);
    Type delegateInvocationHandlerType = Type.getType(InvocationHandler.class);

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, proxyType.getInternalName(), null,
            superType.getInternalName(), interfaces);

    defineInvocationHandlerField(cw, delegateInvocationHandlerType);
    defineDefaultConstructor(cw, proxyType, superType);
    defineDelegateInvocationHandlerConstructor(cw, proxyType, superType, delegateInvocationHandlerType);
    defineDeltaSpikeProxyMethods(cw, proxyType, delegateInvocationHandlerType);

    if (delegateMethods != null) {
        for (java.lang.reflect.Method method : delegateMethods) {
            defineMethod(cw, method, DelegateManualInvocationHandler.class);
        }
    }

    if (interceptMethods != null) {
        for (java.lang.reflect.Method method : interceptMethods) {
            defineSuperAccessorMethod(cw, method, superType, superAccessorMethodSuffix);
            defineMethod(cw, method, InterceptManualInvocationHandler.class);
        }
    }

    // copy all annotations from the source class
    try {
        // ClassVisitor to intercept all annotation visits on the class
        ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                return new CopyAnnotationVisitorAdapter(super.visitAnnotation(desc, visible),
                        cw.visitAnnotation(desc, visible));
            }
        };

        // visit class to proxy with our visitor to copy all annotations from the source class to our ClassWriter
        String sourceClassFilename = targetClass.getName().replace('.', '/') + ".class";
        ClassReader cr = new ClassReader(targetClass.getClassLoader().getResourceAsStream(sourceClassFilename));
        cr.accept(cv, 0);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return cw.toByteArray();
}

From source file:org.apache.deltaspike.proxy.impl.CopyAnnotationVisitorAdapter.java

License:Apache License

public CopyAnnotationVisitorAdapter(AnnotationVisitor from, AnnotationVisitor copyTo) {
    super(Opcodes.ASM5);

    this.from = from;
    this.to = copyTo;
}

From source file:org.apache.felix.ipojo.extender.internal.processor.ConfigurationAnnotationScanner.java

License:Apache License

public ConfigurationAnnotationScanner() {
    super(Opcodes.ASM5);
}

From source file:org.apache.felix.ipojo.manipulation.ClassCheckerTestCase.java

License:Apache License

private void assertAnnotationIsAlone(MethodDescriptor method, String desc) {
    List<ClassChecker.AnnotationDescriptor> annotations = method.getAnnotations();

    assertEquals(1, annotations.size());
    ClassChecker.AnnotationDescriptor annotationDescriptor = annotations.get(0);
    MethodVisitor mv = mock(MethodVisitor.class);
    when(mv.visitAnnotation(desc, true)).thenReturn(new AnnotationVisitor(Opcodes.ASM5) {
    });//from  www  .j av a2  s. c  o  m
    annotationDescriptor.visitAnnotation(mv);
}

From source file:org.apache.felix.ipojo.manipulation.ClassManipulator.java

License:Apache License

/**
 * Constructor./*from   w  w  w. ja v a  2s .  c o m*/
 * @param visitor : class visitor.
 * @param manipulator : the manipulator having analyzed the class.
 */
public ClassManipulator(ClassVisitor visitor, Manipulator manipulator) {
    super(Opcodes.ASM5, visitor);
    m_manipulator = manipulator;
    m_fields = manipulator.getFields().keySet();
    m_visitedMethods = manipulator.getMethods();
}

From source file:org.apache.felix.ipojo.manipulation.InnerClassChecker.java

License:Apache License

public InnerClassChecker(String name, Manipulator manipulator) {
    super(Opcodes.ASM5);
    m_name = name;
    m_manipulator = manipulator;
}

From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.ClassMetadataCollector.java

License:Apache License

public ClassMetadataCollector(BindingRegistry registry, Reporter reporter) {
    super(Opcodes.ASM5);
    this.registry = registry;
    this.reporter = reporter;
    node = new ClassNode();
}

From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.FieldMetadataCollector.java

License:Apache License

public FieldMetadataCollector(ComponentWorkbench workbench, FieldNode node) {
    super(Opcodes.ASM5);
    this.workbench = workbench;
    this.node = node;
    this.registry = workbench.getBindingRegistry();
}

From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.MethodMetadataCollector.java

License:Apache License

public MethodMetadataCollector(ComponentWorkbench workbench, MethodNode node, Reporter reporter) {
    super(Opcodes.ASM5);
    this.workbench = workbench;
    this.node = node;
    this.registry = workbench.getBindingRegistry();
}