I am writing an application using the JVMTI. I am trying to instrument the bytecode: by injecting method calls on every method entry.
I know how to do that, but the problem is in the instrument class, say it's called Proxy
, which I load using the JNI function DefineClass. My Proxy
has a few dependencies in Java Class Library, currently just java.lang.ThreadLocal<Boolean>
.
Now, say I have this, where inInstrumentMethod
is a plain boolean
:
public static void onEntry(int methodID)
{
if (inInstrumentMethod) {
return;
} else {
inInstrumentMethod = true;
}
System.out.println("Method ID: " + methodID);
inInstrumentMethod = false;
}
The code compiles and works. However, if I make inInstrumentMethod
a java.lang.ThreadLocal<Boolean>
, I get a NoClassDefFoundError. The code:
private static ThreadLocal<Boolean> inInstrumentMethod = new ThreadLocal<Boolean>() {
@Override protected Boolean initialValue() {
return Boolean.FALSE;
}
};
public static void onEntry(int methodID)
{
if (inInstrumentMethod.get()) {
return;
} else {
inInstrumentMethod.set(true);
}
System.out.println("Method ID: " + methodID);
inInstrumentMethod.set(false);
}
My guess is that the dependencies have not been resolved correctly, and java.lang.ThreadLocal
was not loaded (and thus could not be found). The question is, then, how do I force Java to load java.lang.ThreadLocal
? I don't think I could use DefineClass
in this case; is there an alternative?