java - 如何让Java代理和反射协同工作?

标签 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() {

最佳答案

好吧,事实证明原因在堆栈跟踪中

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

代理类由 JVM 自动生成,作为反射支持的一部分。默认情况下,检测器会检测所有内容。过滤这个包就可以解决问题。

关于java - 如何让Java代理和反射协同工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32870098/

相关文章:

Android OREO 中的 java.lang.IllegalArgumentException : at android. graphics.Outline.setConvexPath

java - 建立在线白板的正确方法

java - 在单元测试中启动新的 JVM

c# - 如何迭代 C# 类查找特定类型的所有实例,然后在每个实例上调用一个方法

Scala Class[_$1] 其中类型 _$1

java - 执行 Broadleaf startadmin.bat 脚本时出错

go - 如何省略结构Gin gonic的一些参数

c# - .NET中是否有与Java的ClassFileTransformer等效的文件? (一种替换类的方法)

java - 使用 ClassFileTransformer.transform 的字节码检测

java - 无法以编程方式触发点击事件