Search in sources :

Example 1 with DeltaSpikeProxy

use of org.apache.deltaspike.proxy.spi.DeltaSpikeProxy in project deltaspike by apache.

the class AsmProxyClassGenerator method generateProxyClassBytes.

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) };
    }
    // 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();
}
Also used : DeltaSpikeProxy(org.apache.deltaspike.proxy.spi.DeltaSpikeProxy) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Type(org.objectweb.asm.Type) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 DeltaSpikeProxy (org.apache.deltaspike.proxy.spi.DeltaSpikeProxy)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 ClassWriter (org.objectweb.asm.ClassWriter)1 Type (org.objectweb.asm.Type)1