Subsequent loading of Java ByteCode Injection

I am trying to connect the methods of other classes in a Java program, I know that this can be done using Java agents at boot time. But is there a way to do this as soon as the .class file is loaded into the JVM, similar to connecting a dll in C ++ using Read / Writeprocessmemory ()? Thanks.

+4
source share
1 answer

If you mean the interception method, there are two options

1) java.lang.reflect.Proxy. Below, the test makes proxies for the list and intercepts method calls. Please note that it only works with interfaces.

class Handler implements java.lang.reflect.InvocationHandler { Object target; Handler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Before " + method.getName()); Object res = method.invoke(target, args); System.out.println("After " + method.getName()); return res; } } List list = new ArrayList(); List proxy = (List) Proxy.newProxyInstance(Test.class.getClassLoader(), new Class[] { List.class }, new Handler(list)); proxy.add(1); 

prints

 Before add After add 

2) Aspect-oriented programming. The easiest way to start using it, in my opinion, is Spring http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html

+2
source

All Articles