mercredi 30 septembre 2015

How to make Java agent and reflection work together?

I have a project (http://ift.tt/1M1JxjO) that uses java-agent for online instrumentation. I tried to have an annotation class for runtime reflection. The program crashes with NoClassDefFoundError

Exception in thread "main" java.lang.NoClassDefFoundError: janala/logger/DJVM
at com.sun.proxy.$Proxy0.<clinit>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739)
at sun.reflect.annotation.AnnotationParser$1.run(AnnotationParser.java:305)
at sun.reflect.annotation.AnnotationParser$1.run(AnnotationParser.java:303)
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.annotation.AnnotationParser.annotationForMap(AnnotationParser.java:303)
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:293)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
at sun.reflect.annotation.AnnotationParser.parseSelectAnnotations(AnnotationParser.java:101)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:139)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
at java.lang.reflect.Executable.declaredAnnotations(Executable.java:546)
at java.lang.reflect.Executable.getAnnotation(Executable.java:520)
at java.lang.reflect.Method.getAnnotation(Method.java:607)
at janala.utils.ClassRunner.run(ClassRunner.java:20)
at janala.utils.ClassRunner.main(ClassRunner.java:33)

The call site of the error is simply

Test annotation = method.getAnnotation(Test.class);

If I change the program to first instrument the class, write the instrumented class to a .class file and then run the program with the same classpath. Then it runs fine.

The annotation is declared as

public class Annotations {

   /**
   * A CATG test.
   */
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.METHOD)
  public @interface Test {}
}

And the use of the annotation is like

  @Test
  public void testAnd() {





Aucun commentaire:

Enregistrer un commentaire