use of org.objectweb.asm.ClassWriter in project lombok by rzwitserloot.
the class AsmUtil method fixJSRInlining.
static byte[] fixJSRInlining(byte[] byteCode) {
ClassReader reader = new ClassReader(byteCode);
ClassWriter writer = new FixedClassWriter(reader, 0);
ClassVisitor visitor = new ClassVisitor(Opcodes.ASM6, writer) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
return new JSRInlinerAdapter(super.visitMethod(access, name, desc, signature, exceptions), access, name, desc, signature, exceptions);
}
};
reader.accept(visitor, 0);
return writer.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project JikesRVM by JikesRVM.
the class AnnotationAdder method adaptClass.
/**
* Chain together a ClassReader than will read the given class and a
* ClassWriter to write out a new class with an adapter in the middle to add
* annotations
* @param fromName the name of the class we're coming from
*/
private static void adaptClass(String fromName) {
if (VERBOSE)
System.out.println("Adding annotations to class: " + fromName);
// gets an input stream to read the bytecode of the class
String resource = fromName.replace('.', '/') + ".class";
InputStream is = BootstrapClassLoader.getBootstrapClassLoader().getResourceAsStream(resource);
byte[] b;
// adapts the class on the fly
try {
ClassReader cr = new ClassReader(is);
ClassWriter cw = new ClassWriter(0);
ClassVisitor cv = new AddAnnotationClassAdapter(cw, fromName);
cr.accept(cv, 0);
b = cw.toByteArray();
} catch (Exception e) {
throw new Error("Couldn't find class " + fromName + " (" + resource + ")", e);
}
// store the adapted class on disk
try {
File file = new File(destinationDir + resource);
// ensure we have a directory to write to
new File(file.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(file);
fos.write(b);
fos.close();
} catch (Exception e) {
throw new Error("Error writing to " + destinationDir + resource + " to disk", e);
}
}
use of org.objectweb.asm.ClassWriter in project graal by oracle.
the class DeoptimizeOnExceptionTest method makeClazz.
private static byte[] makeClazz() {
// Code generated the class below using asm.
String clazzName = DeoptimizeOnExceptionTest.class.getName().replace('.', '/');
final ClassWriter w = new ClassWriter(0);
w.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "t/TestJSR", null, "java/lang/Object", new String[] { "java/lang/Runnable" });
MethodVisitor mv = w.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[] {});
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(10, 10);
mv.visitEnd();
mv = w.visitMethod(Opcodes.ACC_PUBLIC, "run", "()V", null, null);
mv.visitCode();
mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
Label l1 = new Label();
mv.visitJumpInsn(Opcodes.JSR, l1);
mv.visitInsn(Opcodes.RETURN);
mv.visitLabel(l1);
mv.visitVarInsn(Opcodes.ASTORE, 1);
Label lElse = new Label();
Label lEnd = new Label();
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false);
mv.visitInsn(Opcodes.POP2);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "getM", "()Ljava/lang/Object;", false);
mv.visitInsn(Opcodes.DUP);
mv.visitJumpInsn(Opcodes.IFNULL, lElse);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodA", "()V", false);
mv.visitJumpInsn(Opcodes.GOTO, lEnd);
mv.visitLabel(lElse);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, clazzName, "methodB", "()V", false);
mv.visitLabel(lEnd);
mv.visitVarInsn(Opcodes.RET, 1);
mv.visitMaxs(10, 10);
mv.visitEnd();
return w.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project wildfly-swarm by wildfly-swarm.
the class FaviconFactory method createFaviconServletExtension.
static byte[] createFaviconServletExtension(String name) throws IOException {
ClassReader reader = new ClassReader(FaviconServletExtension.class.getClassLoader().getResourceAsStream(FaviconServletExtension.class.getName().replace('.', '/') + ".class"));
String slashName = name.replace('.', '/');
ClassWriter writer = new ClassWriter(0);
Remapper remapper = new Remapper() {
@Override
public String map(String typeName) {
if (typeName.equals("org/wildfly/swarm/undertow/internal/FaviconServletExtension")) {
return slashName;
}
return super.map(typeName);
}
};
ClassRemapper adapter = new ClassRemapper(writer, remapper);
reader.accept(adapter, ClassReader.EXPAND_FRAMES);
writer.visitEnd();
return writer.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project Fracture by HellFirePvP.
the class FractureTransformer method transform.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (!isTransformationRequired(transformedName))
return bytes;
ClassNode node = new ClassNode();
ClassReader reader = new ClassReader(bytes);
reader.accept(node, 0);
for (SubClassTransformer subTransformer : subTransformers) {
try {
subTransformer.transformClassNode(node, transformedName, name);
} catch (ASMTransformationException asmException) {
FMLLog.warning("Access transformation failed for Transformer: " + subTransformer.getIdentifier());
FMLLog.warning("Transformer added information:");
subTransformer.addErrorInformation();
asmException.printStackTrace();
// Rethrow
throw asmException;
}
}
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
node.accept(writer);
bytes = writer.toByteArray();
if (false) {
try {
File f = new File("C:/FRTestClasses/" + transformedName + ".class");
f.getParentFile().mkdirs();
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
out.write(bytes);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return bytes;
}
Aggregations