use of jdk.internal.org.objectweb.asm.ClassReader in project openj9 by eclipse.
the class Transformer method transform.
public byte[] transform(ClassLoader loader, String className, Class<?> classToTransform, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
try {
if (flag == MODIFY_EXISTING_METHOD) {
// for fast HCR
AgentMain.logger.debug("Instrumenting " + className + " by modifying existing method.");
ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
ClassVisitor visitor = new CustomClassVisitor(cw, classToTransform.getCanonicalName());
reader.accept(visitor, ClassReader.EXPAND_FRAMES);
return cw.toByteArray();
} else if (flag == ADD_NEW_METHOD) {
// for extended HCR
AgentMain.logger.debug("Instrumenting " + className + " by adding a new method.");
ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
ClassVisitor cv = new MethodAddAdapter(new CheckClassAdapter(cw));
reader.accept(cv, ClassReader.SKIP_FRAMES);
return cw.toByteArray();
} else if (flag == ADD_NEW_FIELD) {
// for GCRetransform test
AgentMain.logger.debug("Instrumenting " + className + " by adding a new field.");
ClassReader reader = new ClassReader(classToTransform.getCanonicalName());
ClassWriter cw = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
FieldNode newField = new FieldNode(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "new_static_int_array", "[I", null, null);
ClassVisitor cv = new FieldAdder(new CheckClassAdapter(cw), newField, className, _methodName);
reader.accept(cv, ClassReader.SKIP_FRAMES);
return cw.toByteArray();
}
return null;
} catch (IOException e) {
return null;
}
}
use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.
the class ASMifier method main.
/**
* Prints the ASM source code to generate the given class to the standard
* output.
* <p>
* Usage: ASMifier [-debug] <binary class name or class file name>
*
* @param args
* the command line arguments.
*
* @throws Exception
* if the class cannot be found, or if an IO exception occurs.
*/
public static void main(final String[] args) throws Exception {
int i = 0;
int flags = ClassReader.SKIP_DEBUG;
boolean ok = true;
if (args.length < 1 || args.length > 2) {
ok = false;
}
if (ok && "-debug".equals(args[0])) {
i = 1;
flags = 0;
if (args.length != 2) {
ok = false;
}
}
if (!ok) {
System.err.println("Prints the ASM code to generate the given class.");
System.err.println("Usage: ASMifier [-debug] " + "<fully qualified class name or class file name>");
return;
}
ClassReader cr;
if (args[i].endsWith(".class") || args[i].indexOf('\\') > -1 || args[i].indexOf('/') > -1) {
cr = new ClassReader(new FileInputStream(args[i]));
} else {
cr = new ClassReader(args[i]);
}
cr.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(System.out)), flags);
}
use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.
the class CheckClassAdapter method main.
/**
* Checks a given class.
* <p>
* Usage: CheckClassAdapter <binary class name or class file name>
*
* @param args
* the command line arguments.
*
* @throws Exception
* if the class cannot be found, or if an IO exception occurs.
*/
public static void main(final String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Verifies the given class.");
System.err.println("Usage: CheckClassAdapter " + "<fully qualified class name or class file name>");
return;
}
ClassReader cr;
if (args[0].endsWith(".class")) {
cr = new ClassReader(new FileInputStream(args[0]));
} else {
cr = new ClassReader(args[0]);
}
verify(cr, false, new PrintWriter(System.err));
}
use of jdk.internal.org.objectweb.asm.ClassReader in project Bytecoder by mirkosertic.
the class ModuleInfoExtender method toByteArray.
/**
* Returns the bytes of the modified module-info.class.
* Once this method has been called then the Extender object should
* be discarded.
*/
public byte[] toByteArray() throws IOException {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
AttributeAddingClassVisitor cv = new AttributeAddingClassVisitor(Opcodes.ASM5, cw);
ClassReader cr = new ClassReader(in);
if (packages != null)
cv.addAttribute(new ModulePackagesAttribute(packages));
if (mainClass != null)
cv.addAttribute(new ModuleMainClassAttribute(mainClass));
if (targetPlatform != null)
cv.addAttribute(new ModuleTargetAttribute(targetPlatform));
if (hashes != null)
cv.addAttribute(new ModuleHashesAttribute(hashes));
if (moduleResolution != null)
cv.addAttribute(new ModuleResolutionAttribute(moduleResolution.value()));
List<Attribute> attrs = new ArrayList<>();
// prototypes of attributes that should be parsed
attrs.add(new ModuleAttribute(version));
attrs.add(new ModulePackagesAttribute());
attrs.add(new ModuleMainClassAttribute());
attrs.add(new ModuleTargetAttribute());
attrs.add(new ModuleHashesAttribute());
cr.accept(cv, attrs.toArray(new Attribute[0]), 0);
// add any attributes that didn't replace previous attributes
cv.finish();
return cw.toByteArray();
}
use of jdk.internal.org.objectweb.asm.ClassReader in project CorfuDB by CorfuDB.
the class Utils method printByteCode.
public static String printByteCode(byte[] bytes) {
ClassReader cr = new ClassReader(bytes);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
final List<MethodNode> methods = cn.methods;
StringBuilder sb = new StringBuilder();
for (MethodNode m : methods) {
InsnList inList = m.instructions;
sb.append(m.name);
for (int i = 0; i < inList.size(); i++) {
sb.append(insnToString(inList.get(i)));
}
}
return sb.toString();
}
Aggregations