Byte Buddy 未加载 Classes 并抛出 Class 未找到

Byte Buddy Not Loading Classes and Throwing Class Not Found

我正在尝试为 springboot 应用程序构建自定义代理。 这是我的经纪人 premain 的样子

                .with(new AgentBuilder.InitializationStrategy.SelfInjection.Eager())
                .ignore(ElementMatchers.nameStartsWith("org.springframework.boot"))
                .type((ElementMatchers.any()))
                .transform((builder, typeDescription, classLoader, module) -> builder
                        .method(ElementMatchers.any())
                        .intercept(Advice.to(MyInterceptor.class))
                ).installOn(instrumentation);

我试图不在 boot loader

中使用方法

当我试图在启动时将代理附加到我的 spring 启动应用程序时,这是我遇到的错误

Exception in thread "main" java.lang.NoClassDefFoundError: sun/reflect/GeneratedMethodAccessor14
    at sun.reflect.GeneratedMethodAccessor14.<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:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.MethodAccessorGenerator.run(MethodAccessorGenerator.java:403)
    at sun.reflect.MethodAccessorGenerator.run(MethodAccessorGenerator.java:394)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393)
    at sun.reflect.MethodAccessorGenerator.generateMethod(MethodAccessorGenerator.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:53)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.misc.CompoundEnumeration.<clinit>(CompoundEnumeration.java)
    at java.lang.ClassLoader.getResources(ClassLoader.java:1144)
    at java.lang.ClassLoader.getResources(ClassLoader.java:1138)
    at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:348)
    at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393)
    at java.util.ServiceLoader.hasNext(ServiceLoader.java:474)
    at java.time.zone.ZoneRulesProvider.<clinit>(ZoneRulesProvider.java:165)
    at java.time.ZoneRegion.ofId(ZoneRegion.java:120)
    at java.time.ZoneId.of(ZoneId.java:411)
    at java.time.ZoneId.of(ZoneId.java:359)
    at java.time.ZoneId.of(ZoneId.java:315)
    at java.util.TimeZone.toZoneId(TimeZone.java:556)
    at sun.util.calendar.ZoneInfo.toZoneId(ZoneInfo.java)
    at java.time.ZoneId.systemDefault(ZoneId.java:274)
    at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.decodeMsDosFormatDateTime(CentralDirectoryFileHeader.java:130)
    at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.getTime(CentralDirectoryFileHeader.java:116)
    at org.springframework.boot.loader.jar.JarEntry.<init>(JarEntry.java:58)
    at org.springframework.boot.loader.jar.JarFileEntries.getEntry(JarFileEntries.java:316)
    at org.springframework.boot.loader.jar.JarFileEntries.access0(JarFileEntries.java:48)
    at org.springframework.boot.loader.jar.JarFileEntries$EntryIterator.next(JarFileEntries.java:366)
    at org.springframework.boot.loader.jar.JarFileEntries$EntryIterator.next(JarFileEntries.java:350)
    at org.springframework.boot.loader.jar.JarFile.nextElement(JarFile.java:201)
    at org.springframework.boot.loader.jar.JarFile.nextElement(JarFile.java:192)
    at org.springframework.boot.loader.archive.JarFileArchive$EntryIterator.next(JarFileArchive.java:184)
    at org.springframework.boot.loader.archive.JarFileArchive$EntryIterator.next(JarFileArchive.java:169)
    at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:85)
    at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:69)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.lang.ClassNotFoundException: sun.reflect.GeneratedMethodAccessor14
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 43 more

我是不是在忽略特定匹配器方面做错了什么? `

eager self-injecting 策略会过早地加载类型,你不应该使用它。如果您注册 AgentBuilder.Listener,您可能会看到导致此加载问题的错误。我认为您想将建议用作装饰器,而不是实现:

(builder, typeDescription, classLoader, module) -> builder.visit(Advice
    .to(MyInterceptor.class)
    .on(isMethod())

这样,方法实现被修饰而不是被替换。还有,去掉注入策略,这里没有注入的东西