DEV Community

Query Filter
Query Filter

Posted on

bridge55

new AgentBuilder.Default()
    .with(AgentBuilder.TypeStrategy.Default.REDEFINE)
    .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
    // This allows the agent to find class files even in child/isolated loaders
    .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE) 
    .ignore(ElementMatchers.none())
    .type(typeDescription -> targetClassSet.contains(typeDescription.getName()))
    .transform((builder, typeDescription, classLoader, module) -> {
        System.out.println("PROFILER: [ATTACH-MATCH] " + typeDescription.getName());
        return builder.method(ElementMatchers.any()
                .and(ElementMatchers.not(ElementMatchers.isAbstract()))
                .and(ElementMatchers.not(ElementMatchers.isNative())))
                .intercept(Advice.to(comet.agent.ProfilerAgent.ProfilerAdvice.class));
    })
    .installOn(inst);

// MANUAL PUSH: If the above doesn't trigger, we force a retransform of already loaded classes
for (Class<?> clazz : inst.getAllLoadedClasses()) {
    if (targetClassSet.contains(clazz.getName())) {
        try {
            System.out.println("PROFILER: Force-Retransforming: " + clazz.getName());
            inst.retransformClasses(clazz);
        } catch (Exception e) {
            System.err.println("PROFILER: Failed to force " + clazz.getName() + " : " + e.getMessage());
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)