gpt4 book ai didi

java - 使用 ByteBuddy 装饰 ClassLoader 方法

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:54 25 4
gpt4 key购买 nike

我有一个项目,允许在 JVM 加载类时记录它们 ( https://github.com/jdeppe-pivotal/classload-tracer )。这样做的限制是类必须成功加载才能被记录。

现在我想尝试扩展它并使用各种 ClassLoader 方法来记录尝试加载的类(无论是否成功)。我希望为此使用 ByteBuddy。不幸的是我运气不太好。

这是代理的代码:

    public class ScratchAgent {

private static final PrintWriter out;
public static final ByteArrayOutputStream baos;

static {
baos = new ByteArrayOutputStream(10);
out = new PrintWriter(baos);
}

public static void premain(String arg, Instrumentation inst) throws Exception {
File temp = Files.createTempDirectory("tmp").toFile();

ClassInjector.UsingInstrumentation
.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, inst)
.inject(Collections.singletonMap(
new TypeDescription.ForLoadedType(MyInterceptor.class),
ClassFileLocator.ForClassLoader.read(MyInterceptor.class)));

new AgentBuilder.Default()
.disableClassFormatChanges()
.with(AgentBuilder.Listener.StreamWriting.toSystemOut().withTransformationsOnly())
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentBuilder.TypeStrategy.Default.REDEFINE)
.with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
.ignore(ElementMatchers.nameStartsWith("net.bytebuddy."))
.enableBootstrapInjection(inst, temp)

.type(is(TestClass.class))
.or(ElementMatchers.isSubTypeOf(ClassLoader.class)
.or(ElementMatchers.nameContainsIgnoreCase("classloader"))
)

.transform((builder, type, classLoader, module) -> builder
.visit(Advice.to(MyInterceptor.class)
.on(hasMethodName("fooMethod")))
.visit(Advice.to(MyInterceptor.class)
.on(hasMethodName("loadClass"))))
.installOn(inst);
}

public static class MyInterceptor {
@Advice.OnMethodEnter
public static void decorate(
@Advice.Argument(0) String arg,
@Advice.This Object thisThis,
@Advice.Origin Method method,
@Advice.Origin Class<?> clazz) {
System.out.println("--->>> OK " + method.getName()
+ "(" + arg + ") " + thisThis);
out.println("--->>> OK " + arg);
out.flush();
}
}
}

这是一个测试:

    @Test
public void sanity() throws Exception {
ScratchAgent.premain(null, ByteBuddyAgent.install());

String result = new TestClass().fooMethod("world");
assertThat(result).isEqualTo("Hello world");
assertThat(ScratchAgent.baos.toString()).isEqualTo("--->>> OK world\n");
}

但这会产生以下错误:

[Byte Buddy] TRANSFORM io.pivotal.test.TestClass [sun.misc.Launcher$AppClassLoader@18b4aac2, null, loaded=true]
[Byte Buddy] TRANSFORM java.net.URLClassLoader$2 [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.net.URLClassLoader$3$1 [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.net.URLClassLoader$3 [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.lang.ClassLoader$2 [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.net.URLClassLoader$1 [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.lang.SystemClassLoaderAction [null, null, loaded=true]
[Byte Buddy] TRANSFORM sun.misc.Launcher$AppClassLoader$1 [null, null, loaded=true]
[Byte Buddy] TRANSFORM sun.misc.Launcher$ExtClassLoader$1 [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.net.URLClassLoader$7 [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.lang.ClassLoader$ParallelLoaders [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.lang.ClassLoader$NativeLibrary [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.lang.ClassLoader$3 [null, null, loaded=true]
[Byte Buddy] TRANSFORM sun.misc.Launcher$ExtClassLoader [null, null, loaded=true]
[Byte Buddy] TRANSFORM sun.misc.Launcher$AppClassLoader [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.net.URLClassLoader [null, null, loaded=true]
[Byte Buddy] TRANSFORM sun.reflect.DelegatingClassLoader [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.security.SecureClassLoader [null, null, loaded=true]
[Byte Buddy] TRANSFORM java.lang.ClassLoader [null, null, loaded=true]
--->>> OK fooMethod(world) io.pivotal.test.TestClass@2ca26d77
--->>> OK loadClass(org.assertj.core.api.Assertions) sun.misc.Launcher$AppClassLoader@18b4aac2
--->>> OK loadClass(org.junit.runners.model.MultipleFailureException) sun.misc.Launcher$AppClassLoader@18b4aac2
--->>> OK loadClass(org.junit.runner.notification.RunNotifier$7) sun.misc.Launcher$AppClassLoader@18b4aac2
--->>> OK loadClass(net.bytebuddy.pool.TypePool$Default$WithLazyResolution$LazyResolution) sun.misc.Launcher$AppClassLoader@18b4aac2
[Byte Buddy] ERROR java.lang.Throwable$WrappedPrintStream [null, null, loaded=false]
[Byte Buddy] ERROR java.lang.Throwable$PrintStreamOrWriter [null, null, loaded=false]
[Byte Buddy] ERROR java.util.IdentityHashMap$KeySet [null, null, loaded=false]
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runners/model/MultipleFailureException
at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:369)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
[Byte Buddy] ERROR java.lang.Shutdown [null, null, loaded=false]
java.lang.NoClassDefFoundError: net/bytebuddy/pool/TypePool$Default$WithLazyResolution$LazyResolution
at net.bytebuddy.pool.TypePool$Default$WithLazyResolution.doDescribe(TypePool.java:1319)
at net.bytebuddy.pool.TypePool$AbstractBase.describe(TypePool.java:408)
at net.bytebuddy.pool.TypePool$AbstractBase$Hierarchical.describe(TypePool.java:471)
at net.bytebuddy.agent.builder.AgentBuilder$DescriptionStrategy$Default$1.apply(AgentBuilder.java:3373)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.resolve(AgentBuilder.java:10499)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.doTransform(AgentBuilder.java:10469)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:10432)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.access$1500(AgentBuilder.java:10198)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:10807)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:10754)
at java.security.AccessController.doPrivileged(Native Method)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:10355)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
[Byte Buddy] ERROR java.lang.Shutdown$Lock [null, null, loaded=false]
java.lang.NoClassDefFoundError: net/bytebuddy/pool/TypePool$Default$WithLazyResolution$LazyResolution
at net.bytebuddy.pool.TypePool$Default$WithLazyResolution.doDescribe(TypePool.java:1319)
at net.bytebuddy.pool.TypePool$AbstractBase.describe(TypePool.java:408)
at net.bytebuddy.pool.TypePool$AbstractBase$Hierarchical.describe(TypePool.java:471)
at net.bytebuddy.agent.builder.AgentBuilder$DescriptionStrategy$Default$1.apply(AgentBuilder.java:3373)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.resolve(AgentBuilder.java:10499)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.doTransform(AgentBuilder.java:10469)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:10432)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.access$1500(AgentBuilder.java:10198)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:10807)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer$LegacyVmDispatcher.run(AgentBuilder.java:10754)
at java.security.AccessController.doPrivileged(Native Method)
at net.bytebuddy.agent.builder.AgentBuilder$Default$ExecutingTransformer.transform(AgentBuilder.java:10355)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at java.lang.Shutdown.<clinit>(Shutdown.java:61)

Process finished with exit code 1

我意识到我的测试没有做任何事情来测试 ClassLoader 装饰,但我只是首先迭代使用“普通”类,然后慢慢添加必要的位来检测 ClassLoader 类。

我做错了什么?检测类加载器是否可行?谢谢!

最佳答案

当您检测一个类时,可能会在检测期间加载其他类。如果这会触发您所依赖的类的类加载,例如您自己的代理的类(不在 Byte Buddy 命名空间中)或引导加载程序的类,则可能会触发检测循环,从而导致您遇到 no class def found 错误。

尝试使用 POOL_ONLY TypeStrategy 来最大程度地避免类加载,您可能需要减少检测的类数量,以避免检测建立日志记录所需的类。

关于java - 使用 ByteBuddy 装饰 ClassLoader 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com