如何使Java代理和反射一起工作?

时间:2015-09-30 15:30:16

标签: java reflection instrumentation

我有一个项目(https://github.com/zhihan/janala2-gradle),它使用java-agent进行在线检测。我试着为运行时反射设置一个注释类。该程序与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)

错误的调用网站只是

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

如果我将程序更改为首先检测类,则将检测类写入.class文件,然后使用相同的类路径运行程序。然后运行正常。

注释声明为

public class Annotations {

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

注释的使用就像

  @Test
  public void testAnd() {

1 个答案:

答案 0 :(得分:1)

嗯,事实证明原因在于堆栈跟踪

at com.sun.proxy.$Proxy0.<clinit>(Unknown Source)

代理类由JVM自动生成,作为反射支持的一部分。默认情况下,仪表将检测所有内容。过滤此软件包可以解决问题。