how to catch thread Exception?
Hi,author i want use byte-buddy for catch code Exception,but my code use new Thread ,byte-buddy cannot catch new Thread “Cooker" Obejct:
public class Cooker {
public void hello() {
System.out.println("+ method name : public void hello();");
new Thread(new Task()) .start();
}
}
Task Object:
public class Task implements Runnable{
@Override
public void run() {
System.out.println("jay");
int a = 1/0;
}
}
Advice Object:
public class AdviceLogic {
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(@Advice.Thrown Throwable thrown) {
if (thrown != null){
System.out.println("- - - - -exception - - - - - -");
System.out.println("exit!"+ thrown.getClass().getName());
}
}
}
TestAdvice Object:
public class TestAdvice {
public static TestAdvice INSTANCE = new TestAdvice();
ClassLoader classLoader;
public void initClassLoader() {
classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(),
ClassFileLocator.ForClassLoader.readToNames(Cooker.class),
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
}
public void modifyTarget() throws Exception {
ByteBuddyAgent.install();
new AgentBuilder.Default()
.with(AgentBuilder.PoolStrategy.Default.EXTENDED)
.with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
.type(ElementMatchers.is(Cooker.class), ElementMatchers.is(classLoader)).transform(new AgentBuilder.Transformer() {
@Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule module) {
return builder.visit(Advice.to(AdviceLogic.class).on(ElementMatchers.not(ElementMatchers.isConstructor()).and(ElementMatchers.any())));
}
})
.installOnByteBuddyAgent();
}
public void print() throws Exception {
Class<Cooker> cookerType = (Class<Cooker>) classLoader.loadClass(Cooker.class.getName());
Object Cooker = cookerType.getDeclaredConstructor().newInstance();
cookerType.getDeclaredMethod("hello").invoke(Cooker);
}
public static void main(String[] args) throws Exception {
INSTANCE.initClassLoader();
INSTANCE.modifyTarget();
INSTANCE.print();
}
}
i want to catch the exception in the task ,but i cant
That's a JVM restriction, unfortunately. One cannot use onThrowable within a constructor.
That's a JVM restriction, unfortunately. One cannot use onThrowable within a constructor.
Is there a way to catch the exception of the thread inside the method?
you could instrument the method that calls the constructor.
you could instrument the method that calls the constructor.
sorry sir,I don't understand too much. Can you elaborate on it
If you need to catch an exception thrown from a constructor, with advice, you cannot instrument the constructor itself. But if the constructor is called from a method, for example a static factory, you can instrument this factory.
sorry i am a litter confused. if I don't know that there are threads in the code, but I need to catch exceptions including thread stack exception like this: `public class Cooker { public void hello() { System.out.println("+ method name : public void hello();"); //more Thread // Thread thread1 = new Thread(new Task()); // Thread thread2 = new Thread(new Task()); // Thread thread3 = new Thread(new Task()); // Thread thread4 = new Thread(new Task());
}
} ` How should I do it?
You could instrument the Task.run method, for instance. If an exception was thrown, advice would capture it.
You could instrument the Task.run method, for instance. If an exception was thrown, advice would capture it.
If the thread accepts a lambad, the lambad cannot be instrument?
No, lambdas cannot normally be instrumented.
No, lambdas cannot normally be instrumented.
I see, thank you sir