I'm trying to create an extension for opentelemetry javaagent. I need some static variables outside the methods to handle spans. But I get the error java.lang.ClassNotFoundException for a class where static variables are defined. The simplest code to reproduce:
public static class MethodAdvice {
public static Integer CONST = 123;
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter() {
try {
System.out.println(CONST.toString());
// Class.forName(MethodAdvice.class.getName());
} catch (Throwable e) {
System.out.println("ERROR: " + e.getCause().toString());
throw new RuntimeException(e);
}
}
}
I get error when using CONST static variable for the first time. The same if I try to load the class. How can I use a static variable defined outside of the @Advice method? As I can see the opentelemetry library uses static variables outside the methods and it works, but I can't figure out how it is handled there.