Skip to content

How to determine type initializer existence? #1752

Open
@eaglemartin

Description

Does bytebuddy have API to determine TypeInitializer method exists? So I could check it before instrument TypeInitializer as below,

new AgentBuilder.Transformer.ForAdvice().advice( isTypeInitializer(), "TypeInitializerAdvice" );

Activity

raphw

raphw commented on Jan 17, 2025

@raphw
Owner

This is more tricky than it might seem. If there is a type initializer from before, the above code will work. If not, you will need to add another transformation as:

builder
  .invokable(isTypeInitializer())
  .intercept(StubMethod.INSTANCE)

This will define an empty type initializer. This does however count as changing the method structure, and a redefinition will fail, as the JVM does not allow for that.

self-assigned this
on Jan 17, 2025
added this to the 1.15.11 milestone on Jan 17, 2025
eaglemartin

eaglemartin commented on Jan 17, 2025

@eaglemartin
Author

I does not want to add a type intializer which will change the class layout, and just need to know there is a type initializer for a type.

I think bytebuddy might add a method such as hasTypeInitializer() to LazyTypeDescription since it is easy to determine this when visiting method in TypeExtractor via ASM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

    Participants

    @raphw@eaglemartin

    Issue actions

      How to determine type initializer existence? · Issue #1752 · raphw/byte-buddy