use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.
the class MethodInterfaceTest method addInterceptor.
@Test
public void addInterceptor() throws Exception {
final String targetInterfaceName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterface";
final String targetClassName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterfaceClass";
logger.debug("Add interceptor interface={}, class={}", targetInterfaceName, targetClassName);
final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new SimpleInterceptor());
final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(SimpleInterceptor.class);
final List<String> methodNameList = Arrays.asList("currentTimeMillis", "foo");
TestClassLoader classLoader = new TestClassLoader();
classLoader.addTargetClassName(targetClassName);
classLoader.addTargetClassName(targetInterfaceName);
classLoader.setCallbackHandler(new CallbackHandler() {
@Override
public void handle(ClassNode classNode) {
List<MethodNode> methodNodes = classNode.methods;
for (MethodNode methodNode : methodNodes) {
logger.debug("Handle class={}, method={}", classNode.name, methodNode.name);
if (methodNode.name.equals("<clinit>")) {
continue;
}
ASMMethodNodeAdapter methodNodeAdapter = new ASMMethodNodeAdapter(classNode.name, methodNode);
if (methodNodeAdapter.isAbstract() || methodNodeAdapter.isNative()) {
continue;
}
if (!methodNameList.contains(methodNode.name)) {
continue;
}
methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, 99);
logger.debug("Add before interceptor in method={}", methodNode.name);
methodNodeAdapter.addAfterInterceptor(interceptorId, interceptorDefinition, 99);
logger.debug("Add after interceptor in method={}", methodNode.name);
}
}
});
// static method
Assert.assertFalse(SimpleInterceptor.before);
logger.debug("Interface static method");
Class<?> clazz = classLoader.loadClass(targetInterfaceName);
Method method = clazz.getDeclaredMethod("currentTimeMillis");
method.invoke(null);
assertTrue(SimpleInterceptor.before);
// reset
SimpleInterceptor.before = false;
// default method
Assert.assertFalse(SimpleInterceptor.before);
logger.debug("Interface default method");
clazz = classLoader.loadClass(targetClassName);
method = clazz.getDeclaredMethod("bar");
method.invoke(clazz.newInstance());
assertTrue(SimpleInterceptor.before);
}
use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.
the class ASMClassNodeAdapter method addDelegatorMethod.
public ASMMethodNodeAdapter addDelegatorMethod(final ASMMethodNodeAdapter superMethodNode) {
Objects.requireNonNull(superMethodNode, "superMethodNode");
final String[] exceptions = getSuperMethodExceptions(superMethodNode);
final MethodNode rawMethodNode = new MethodNode(superMethodNode.getAccess(), superMethodNode.getName(), superMethodNode.getDesc(), superMethodNode.getSignature(), exceptions);
final ASMMethodNodeAdapter methodNode = new ASMMethodNodeAdapter(getInternalName(), rawMethodNode);
methodNode.addDelegator(superMethodNode.getDeclaringClassInternalName());
addMethodNode0(methodNode.getMethodNode());
return methodNode;
}
use of org.objectweb.asm.tree.MethodNode in project pinpoint by naver.
the class ASMClassNodeAdapter method addSetterMethod.
public void addSetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
Objects.requireNonNull(methodName, "methodName");
Objects.requireNonNull(fieldNode, "fieldNode");
// void is V.
final String desc = "(" + fieldNode.getDesc() + ")V";
final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
final InsnList instructions = getInsnList(methodNode);
// load this.
instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
final Type type = Type.getType(fieldNode.getDesc());
// put field.
instructions.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 1));
instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
// return.
instructions.add(new InsnNode(Opcodes.RETURN));
addMethodNode0(methodNode);
}
use of org.objectweb.asm.tree.MethodNode in project Engine by VoltzEngine-Project.
the class InjectionTemplate method init.
// Called late to avoid loading class data in the constructor
private boolean init() {
if (!init) {
init = true;
try {
final ClassNode cnode = ASMHelper.createClassNode(((LaunchClassLoader) InjectionTemplate.class.getClassLoader()).getClassBytes(className.replace('/', '.')));
for (MethodNode method : cnode.methods) {
this.methodImplementations.add(method);
method.desc = new ObfMapping(cnode.name, method.name, method.desc).toRuntime().s_desc;
}
} catch (IOException e) {
// TODO error out in dev mode
// TODO make notation to users that this injector failed
e.printStackTrace();
failedToLoadClass = true;
}
}
return failedToLoadClass;
}
use of org.objectweb.asm.tree.MethodNode in project atlas by alibaba.
the class TBIncrementalChangeVisitor method visitMethod.
/**
* Generates new delegates for all 'patchable' methods in the visited class. Delegates
* are static methods that do the same thing the visited code does, but from outside the class.
* For instance methods, the instance is passed as the first argument. Note that:
* <ul>
* <li>We ignore the class constructor as we don't support it right now</li>
* <li>We skip abstract methods.</li>
* <li>For constructors split the method body into super arguments and the rest of
* the method body, see {@link ConstructorBuilder}</li>
* </ul>
*/
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
if (instantRunDisabled || !isAccessCompatibleWithInstantRun(access)) {
// Nothing to generate.
return null;
}
if (name.equals(ByteCodeUtils.CLASS_INITIALIZER)) {
// we skip the class init as it can reset static fields which we don't support right now
return null;
}
boolean isStatic = (access & Opcodes.ACC_STATIC) != 0;
String newDesc = computeOverrideMethodDesc(desc, isStatic);
if (DEBUG) {
System.out.println(">>> Visiting method " + visitedClassName + ":" + name + ":" + desc);
if (exceptions != null) {
for (String exception : exceptions) {
System.out.println("> Exception thrown : " + exception);
}
}
}
if (DEBUG) {
System.out.println("New Desc is " + newDesc + ":" + isStatic);
}
// Do not carry on any access flags from the original method. For example synchronized
// on the original method would translate into a static synchronized method here.
access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;
MethodNode method = getMethodByNameInClass(name, desc, classNode);
if (name.equals(ByteCodeUtils.CONSTRUCTOR)) {
Constructor constructor = ConstructorBuilder.build(visitedClassName, method);
MethodVisitor mv = createMethodAdapter(access, constructor.args.name, constructor.args.desc, constructor.args.desc, constructor.args.signature, exceptions, isStatic, true);
constructor.args.accept(mv);
mv = createMethodAdapter(access, constructor.body.name, constructor.body.desc, newDesc, constructor.body.signature, exceptions, isStatic, true);
constructor.body.accept(mv);
// Remember our created methods so we can generated the access$dispatch for them.
addedMethods.add(constructor.args);
addedMethods.add(constructor.body);
return null;
} else {
String newName = isStatic ? computeOverrideMethodName(name, desc) : name;
return createMethodAdapter(access, newName, newDesc, newDesc, signature, exceptions, isStatic, false);
}
}
Aggregations