Search code examples
javaspringjerseyjersey-2.0byte-buddy

Use ByteBuddy to change package of annotated class in Spring app


I want to change the package of classes that have been annotated with a particular annotation. I want to do this so that they can't be picked up by Jersey. I'm wondering if this is achievable within a Spring (Boot) application. If so, where is the best place to add this? I had originally wanted to do this in a ImportBeanDefinitionRegistrar so I could add an EnableXXX annotation on a config class, but I think that might be too late in the Spring lifecycle to redefine classes.


Solution

  • Yes, Byte Buddy allows you to change the name of any instrumented type. You can create an AgentBuilder to match all types that carry your annotation and change their package name using the DSL.

    Do however note that any references to this class would not be updated automatically and other implications of a package change would be rather unpredictable. Generally, I would not recommend you to go down that route.