byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

can add self var to a object

Open 316xu opened this issue 2 months ago • 1 comments

        new AgentBuilder.Default()            
                .type(ElementMatchers.is(HikariDataSource.class))
                .transform(new Transformer() {
                    @Override
                    public Builder<?> transform(Builder<?> builder, TypeDescription typeDescription,
                            ClassLoader classLoader, JavaModule module, ProtectionDomain protectionDomain) {
                        return builder
                        .defineField("ds1", HikariDataSource.class, Visibility.PUBLIC)
                        .defineField("proxy", Boolean.class, Visibility.PUBLIC)
                        .defineField("current", Integer.class, Visibility.PUBLIC);

                    }
                })
                .with(AgentBuilder.Listener.StreamWriting.toSystemError())
                .with(AgentBuilder.InstallationListener.StreamWriting.toSystemError())
                .with(RedefinitionStrategy.RETRANSFORMATION)
                .installOn(inst);

when run it does not work, and log:

[Byte Buddy] DISCOVERY com.zaxxer.hikari.HikariDataSource [sun.misc.Launcher$AppClassLoader@18b4aac2, null, Thread[main,5,main], loaded=true] [Byte Buddy] TRANSFORM com.zaxxer.hikari.HikariDataSource [sun.misc.Launcher$AppClassLoader@18b4aac2, null, Thread[main,5,main], loaded=true] [Byte Buddy] COMPLETE com.zaxxer.hikari.HikariDataSource [sun.misc.Launcher$AppClassLoader@18b4aac2, null, Thread[main,5,main], loaded=true]

when remove the ds1 field, all works fine

how to reload the loaded class, thanks

316xu avatar Nov 10 '25 08:11 316xu

You should use a TypePool to describe types without loading. Avoid referencing HikariDataSource and rather match the type using "named". Then use TargetType.class instead of HikariDataSource.class. This way you can even manage without a type pool.

raphw avatar Nov 10 '25 23:11 raphw