use of org.objectweb.asm.tree.ClassNode in project Minechem by iopleke.
the class MinechemTransformer method injectBytes.
private byte[] injectBytes(Method method, InstructionNode instructionNode, byte[] data) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(data);
classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
MethodNode methodNode = getMethodByName(classNode, method);
AbstractInsnNode pos = findInstructionNode(instructionNode, methodNode);
if (instructionNode.replace) {
methodNode.instructions.insertBefore(pos, instructionNode.getInsnList());
methodNode.instructions.remove(pos);
} else {
methodNode.instructions.insertBefore(pos, instructionNode.getInsnList());
}
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classNode.accept(writer);
return writer.toByteArray();
}
use of org.objectweb.asm.tree.ClassNode in project enumerable by hraberg.
the class LambdaExpressionTrees method findMethodNode.
@SuppressWarnings("unchecked")
static MethodNode findMethodNode(Method method) throws IOException {
String className = method.getDeclaringClass().getName();
ClassReader cr;
if (InMemoryCompiler.bytesByClassName.containsKey(className))
cr = new ClassReader(InMemoryCompiler.bytesByClassName.get(className));
else
cr = new ClassReader(className);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
String descriptor = getMethodDescriptor(method);
for (MethodNode mn : (List<MethodNode>) cn.methods) {
if (method.getName().equals(mn.name) && descriptor.equals(mn.desc))
return mn;
}
throw new IllegalStateException("Cannot find method which does exist");
}
use of org.objectweb.asm.tree.ClassNode in project pinpoint by naver.
the class ASMClassNodeAdapter method get.
public static ASMClassNodeAdapter get(final InstrumentContext pluginContext, final ClassLoader classLoader, final String classInternalName, final boolean skipCode) {
if (pluginContext == null || classInternalName == null) {
throw new IllegalArgumentException("plugin context or class name must not be null.");
}
InputStream in = null;
try {
in = pluginContext.getResourceAsStream(classLoader, classInternalName + ".class");
if (in != null) {
final ClassReader classReader = new ClassReader(in);
final ClassNode classNode = new ClassNode();
if (skipCode) {
classReader.accept(classNode, ClassReader.SKIP_CODE);
} else {
classReader.accept(classNode, 0);
}
return new ASMClassNodeAdapter(pluginContext, classLoader, classNode, skipCode);
}
} catch (IOException ignored) {
// not found class.
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
return null;
}
use of org.objectweb.asm.tree.ClassNode in project pinpoint by naver.
the class ASMEngine method getClass.
@Override
public InstrumentClass getClass(InstrumentContext instrumentContext, ClassLoader classLoader, String className, byte[] classFileBuffer) throws NotFoundInstrumentException {
if (className == null) {
throw new NullPointerException("class name must not be null.");
}
try {
if (classFileBuffer == null) {
ASMClassNodeAdapter classNode = ASMClassNodeAdapter.get(instrumentContext, classLoader, JavaAssistUtils.javaNameToJvmName(className));
if (classNode == null) {
return null;
}
ApiMetaDataService apiMetaDataService = this.apiMetaDataService.get();
return new ASMClass(objectBinderFactory, instrumentContext, interceptorRegistryBinder, apiMetaDataService, classLoader, classNode);
}
// Use ASM tree api.
final ClassReader classReader = new ClassReader(classFileBuffer);
final ClassNode classNode = new ClassNode();
classReader.accept(classNode, 0);
ApiMetaDataService apiMetaDataService = this.apiMetaDataService.get();
return new ASMClass(objectBinderFactory, instrumentContext, interceptorRegistryBinder, apiMetaDataService, classLoader, classNode);
} catch (Exception e) {
throw new NotFoundInstrumentException(e);
}
}
use of org.objectweb.asm.tree.ClassNode in project pinpoint by naver.
the class ASMMethodNodeAdapterRenameTest method rename.
@Test
public void rename() throws Exception {
final String targetClassName = "com.navercorp.pinpoint.profiler.instrument.mock.NormalClass";
classLoader.setTrace(false);
classLoader.setTargetClassName(targetClassName);
classLoader.setCallbackHandler(new ASMClassNodeLoader.CallbackHandler() {
@Override
public void handle(ClassNode classNode) {
List<MethodNode> methodNodes = classNode.methods;
for (MethodNode methodNode : methodNodes) {
ASMMethodNodeAdapter adapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
if (!adapter.isConstructor()) {
adapter.rename(adapter.getName() + "_rename");
}
}
}
});
Class<?> clazz = classLoader.loadClass(targetClassName);
Method method = clazz.getDeclaredMethod("sum_rename", int.class);
method.invoke(clazz.newInstance(), 10);
}
Aggregations