Using BTrace to find when a class is created for the first time

526 Views Asked by At

I'm trying to use BTrace to find when a certain type is first instantiated in my program (Eclipse debugger isn't able to find it) as I'm seeing some strange behaviour (the Javolution XMLStreamWriterImpl is somehow adding elements to my XML before it should even have been created).

Anyway, I have the following method which I am using through JVisualVM, but nothing is showing up when running.

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import java.lang.String;

@BTrace
public class ClassLoad {
    @OnMethod(clazz = "javolution.xml.stream.XMLStreamWriterImpl", method = "<init>", location = @Location(value=Kind.NEW))
    public static void site(@ProbeMethodName(fqn=true) String caller) {
        println(strcat("Called from @", caller));
    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

You need a different @OnMethod definition.

@OnMethod(clazz="/.*/", method="/.*/", location=@Location(value=Kind.NEW, clazz="javolution.xml.stream.XMLStreamWriterImpl"))

Basically you specify that you want to inspect all the methods of all the classes for occurrences of new javolution.xml.stream.XMLStreamWriterImpl instructions.

The rest of the code can stay the same.