使用byte-buddy-agent修改java.util类

时间:2019-06-18 04:05:36

标签: java bytecode threadpoolexecutor byte-buddy javaagents

是否可以使用字节伙伴在java.util类中添加字段?

我试图在java.util.concurrent.FutureTask中添加一个字段,并拦截构造函数和一个任意方法来设置和获取字段值。简而言之,我正在尝试向FutureTask添加一个字段,以便即使它们在线程池中运行,也可以将某些值从父级传递给子线程。无法在FutureTask中添加字段?

FutureTaskTransofrmer

@Override
    protected ElementMatcher.Junction<TypeDescription> getNarrowTypesMatcher() {
        return named("java.util.concurrent.FutureTask");
    }

    @Override
    public AgentBuilder.Transformer getTransformer() {

        return (builder, typeDescription, classLoader, module) -> {
            beforeTransformation(typeDescription, classLoader);

            return builder
                    .defineField("pit", String.class)
                    .constructor(ElementMatchers.any())
                    .intercept(Advice.to(SetPitAdvice.class))
                    .method(named("run"))
                    .intercept(Advice.to(GetPitAdvice.class))
                    ;
        };

    }

GetPitAdvice

    @Advice.OnMethodEnter
    public static void getValues(@Advice.FieldValue(value = "pit") String pit)
            throws Exception {

        logger.info("pit in future Task {}", pit);
    }

SetPitAdvice

@Advice.OnMethodExit
    public static void setValues(
            @Advice.FieldValue(value = "pit", readOnly = false) String pit
    )
            throws Exception {

        logger.debug("Setting pit field in FutureTask");

        pit = SimulationRequestContextHolder.get("pit");

    }

AgentBuilder

    private static AgentBuilder createAgentBuilder(AutoEvictingCachingBinaryLocator binaryLocator) {
        final ByteBuddy byteBuddy = new ByteBuddy()
                .with(TypeValidation.of(AgentProperties.PROPERTIES.isDebugInstrumentation()))
                .with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE);

        return new AgentBuilder.Default(byteBuddy)
                .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
                .with(getListener())
                .with(binaryLocator)
                .ignore(any(), ClassLoaderNameMatcher.isReflectionClassLoader())
                .or(any(), ClassLoaderNameMatcher.classLoaderWithName("org.codehaus.groovy.runtime.callsite.CallSiteClassLoader"))
                .or(any(), new IsIgnoredClassLoaderElementMatcher())
                .or(nameStartsWith("org.aspectj.")
                        .or(nameStartsWith("org.groovy."))
                        .or(nameStartsWith("com.sun."))
                        .or(nameStartsWith("com.p6spy."))
                        .or(nameStartsWith("net.bytebuddy."))
                        .or(nameStartsWith("org.slf4j.").and(not(nameStartsWith("org.slf4j.impl."))))
                        .or(nameContains("javassist"))
                        .or(nameContains(".asm."))
                        .or(nameStartsWith("com.java.agent.sims")
                        ))
//                .disableClassFormatChanges()
                .enableUnsafeBootstrapInjection()
                ;
    }

onTransformListener显示类已转换

11:27:24.141 [main] INFO com.java.agent.sims.ApplicationClassLoaderMatcher - Instrumenting ClassLoader null: true
11:27:24.186 [main] DEBUG com.java.agent.sims.transformers.ByteBuddyTransformer - TRANSFORM java.util.concurrent.FutureTask (FutureTaskTransformer)
11:27:24.466 [main] INFO com.java.agent.sims.instrument.TransformListener - Class modified by Byte Buddy: java.util.concurrent.FutureTask

但是我的建议拦截都没有被调用。

1 个答案:

答案 0 :(得分:1)

如果已经加载了该类,则不可能。相反,您可以使用Instrumentation API将类注入到引导类加载器中,并具有一个包含弱键的静态映射存储在此类中,您可以在其中放置每个实例的字段值。

此外,请注意, until 类是由引导类加载器加载的,而引导类加载器无法查看由代理所包含的由系统类加载器加载的任何类。因此,建议使用的所有API都必须注入到引导加载程序中。