Search code examples
byte-buddy

What is the difference between withEnclosingType() and withDeclaringType() in InstrumentedType?


I'm an experienced Byte Buddy user.

What practical effect does InstrumentedType#withDeclaringType(TypeDescription) have over InstrumentedType#withEnclosingType(TypeDescription)? When do I use one versus the other?


Solution

  • Anonymous types will have an enclosing type but not a declaring type. Member types on the other hand will have both a declaring type and an enclosing type.

    You can check the DynamicType class which maps these attributes to the Java representations within its builder API. If you create an InstrumentedType manually, you can combine attributes like you want, but they might not make sense to the Java reflection API.