Search in sources :

Example 1 with ClassWriter

use of net.bytebuddy.jar.asm.ClassWriter in project mockito by mockito.

the class ModuleUtil method moduleInfo.

private static byte[] moduleInfo(boolean isExported, boolean isOpened) {
    ClassWriter classWriter = new ClassWriter(OpenedClassReader.ASM_API);
    classWriter.visit(Opcodes.V9, Opcodes.ACC_MODULE, "module-info", null, null, null);
    ModuleVisitor mv = classWriter.visitModule("mockito.test", 0, null);
    mv.visitRequire("java.base", Opcodes.ACC_MANDATED, null);
    mv.visitPackage("sample");
    if (isExported) {
        mv.visitExport("sample", 0);
    }
    if (isOpened) {
        mv.visitOpen("sample", 0);
    }
    mv.visitEnd();
    classWriter.visitEnd();
    return classWriter.toByteArray();
}
Also used : ModuleVisitor(net.bytebuddy.jar.asm.ModuleVisitor) ClassWriter(net.bytebuddy.jar.asm.ClassWriter)

Example 2 with ClassWriter

use of net.bytebuddy.jar.asm.ClassWriter in project powermock by powermock.

the class ClassFactory method create.

static byte[] create(String className) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(49, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", null);
    cw.visitSource("Hello.java", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("hello");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : ClassWriter(net.bytebuddy.jar.asm.ClassWriter) MethodVisitor(net.bytebuddy.jar.asm.MethodVisitor)

Example 3 with ClassWriter

use of net.bytebuddy.jar.asm.ClassWriter in project powermock by powermock.

the class DefinalizingClassTransformer method transform.

public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
    if (loader == null || shouldIgnore(className)) {
        return null;
    }
    final ClassReader reader = new ClassReader(classfileBuffer);
    final ClassWriter writer = new ClassWriter(NO_FLAGS_OR_OPTIONS);
    reader.accept(new DefinalizingClassVisitor(writer), NO_FLAGS_OR_OPTIONS);
    return writer.toByteArray();
}
Also used : ClassReader(net.bytebuddy.jar.asm.ClassReader) ClassWriter(net.bytebuddy.jar.asm.ClassWriter)

Example 4 with ClassWriter

use of net.bytebuddy.jar.asm.ClassWriter 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

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