public static void main ( String [ ] args) throws Throwable {
Sample sample = new Sample ( ) ;
Thread t = new Thread ( new Runnable ( ) {
@Override
public void run ( ) {
while ( true ) {
try {
TimeUnit . SECONDS. sleep ( 3 ) ;
int rand = RandomUtils . nextInt ( 0 , 2 ) ;
String result = sample. hello ( "test" , rand == 0 ) ;
System . out. println ( "call result: " + result) ;
} catch ( Throwable e) {
}
}
}
} ) ;
t. start ( ) ;
AgentUtils . install ( ) ;
DefaultInterceptorClassParser interceptorClassParser = new DefaultInterceptorClassParser ( ) ;
List < InterceptorProcessor > processors = interceptorClassParser. parse ( SampleInterceptor . class ) ;
ClassNode classNode = AsmUtils . loadClass ( Sample . class ) ;
for ( MethodNode methodNode : classNode. methods) {
if ( AsmUtils . isConstructor ( methodNode) ) {
continue ;
}
if ( AsmUtils . isAbstract ( methodNode) ) {
continue ;
}
MethodProcessor methodProcessor = new MethodProcessor ( classNode, methodNode) ;
for ( InterceptorProcessor interceptor : processors) {
interceptor. process ( methodProcessor) ;
}
}
byte [ ] bytes = AsmUtils . toBytes ( classNode) ;
AgentUtils . reTransform ( Sample . class , bytes) ;
CountDownLatch countDownLatch = new CountDownLatch ( 1 ) ;
countDownLatch. await ( ) ;
}
public class Sample {
public String hello ( String str, boolean exception) {
if ( exception) {
throw new RuntimeException ( ) ;
}
return "hello " + str;
}
}
public class SampleInterceptor {
@AtEnter ( inline = true )
public static void atEnter ( @Binding.This Object target,
@Binding.Class Class < ? > clazz,
@Binding.MethodName String method,
@Binding.Args Object [ ] args) {
System . out. println ( String . format ( "atEnter: %s %s" , method, args) ) ;
}
@AtInvoke ( inline = true , name = "atInvoke" )
public static void atInvoke ( @Binding.This Object target,
@Binding.Class Class < ? > clazz,
@Binding.Method Method method,
@Binding.Args Object [ ] args) {
System . out. println ( String . format ( "atInvoke: %s %s" , method, args) ) ;
}
@AtExit ( inline = true )
public static void atExit ( @Binding.This Object target,
@Binding.Class Class < ? > clazz,
@Binding.MethodName String method,
@Binding.Args Object [ ] args,
@Binding.Return Object returnObj) {
System . out. println ( String . format ( "atExit: %s %s %s" , method, args, returnObj) ) ;
}
@AtExceptionExit ( inline = true )
public static void atExceptionExit ( @Binding.This Object target,
@Binding.Class Class < ? > clazz,
@Binding.MethodName String method,
@Binding.Args Object [ ] args,
@Binding.Throwable Throwable throwable) {
System . out. println ( String . format ( "atExceptionExit: %s %s %s" , method, args, throwable) ) ;
}
}