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();
}
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();
}
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();
}
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);
}
Aggregations