Search in sources :

Example 1 with ClassVisitor

use of net.bytebuddy.jar.asm.ClassVisitor in project reactor-core by reactor.

the class ReactorDebugAgent method instrument.

private static void instrument(Instrumentation instrumentation) {
    ClassFileTransformer transformer = new ClassFileTransformer() {

        @Override
        public byte[] transform(ClassLoader loader, String className, Class<?> clazz, ProtectionDomain protectionDomain, byte[] bytes) {
            if (loader == null) {
                return null;
            }
            if (className == null || className.startsWith("java/") || className.startsWith("jdk/") || className.startsWith("sun/") || className.startsWith("com/sun/") || className.startsWith("reactor/core/")) {
                return null;
            }
            if (clazz != null && (clazz.isPrimitive() || clazz.isArray() || clazz.isAnnotation() || clazz.isSynthetic())) {
                return null;
            }
            ClassReader cr = new ClassReader(bytes);
            ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
            AtomicBoolean changed = new AtomicBoolean();
            ClassVisitor classVisitor = new ReactorDebugClassVisitor(cw, changed);
            try {
                cr.accept(classVisitor, 0);
            } catch (Throwable e) {
                e.printStackTrace();
                throw e;
            }
            if (!changed.get()) {
                return null;
            }
            return cw.toByteArray();
        }
    };
    instrumentation.addTransformer(transformer, true);
}
Also used : ProtectionDomain(java.security.ProtectionDomain) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClassFileTransformer(java.lang.instrument.ClassFileTransformer) ClassReader(net.bytebuddy.jar.asm.ClassReader) ClassVisitor(net.bytebuddy.jar.asm.ClassVisitor) ClassWriter(net.bytebuddy.jar.asm.ClassWriter)

Aggregations

ClassFileTransformer (java.lang.instrument.ClassFileTransformer)1 ProtectionDomain (java.security.ProtectionDomain)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClassReader (net.bytebuddy.jar.asm.ClassReader)1 ClassVisitor (net.bytebuddy.jar.asm.ClassVisitor)1 ClassWriter (net.bytebuddy.jar.asm.ClassWriter)1