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

Can ByteBuddy make a non-static field be final?

Open jimshowalter opened this issue 1 year ago • 7 comments

After using ByteBuddy to add a Set to our entities to track which fields are set, and calling newInstance, the resulting code looks like this:

public class Foo$ByteBuddy$Te6Wfjue extends Foo { private Set<String> _modified$Fields$Tracker;

We then use reflection to set the Set:

public class Foo$ByteBuddy$Te6Wfjue extends Foo { private Set<String> _modified$Fields$Tracker = new HashSet<>();

This is almost conformant with our coding standards, but "final" is missing from the Set.

Is there a way to declare non-static fields final in classes generated by ByteBuddy?

If not, is there a way to set fields to final post-construction?

jimshowalter avatar May 06 '24 15:05 jimshowalter

Sure. Use .subclass(..., ConstructorStrategy.Defaults.NONE).

Byte Buddy will then not create implicit constructors. Instead, you create them explicitly. I assume your proxy has only a default constructor? If so:

MethodCall.invoke(subclass.getConstructor()).onSuper().andThen(MethodCall.invoke(HashSet.class.getConstructor()).setField("_modified$Fields$Tracker")

raphw avatar May 08 '24 07:05 raphw