Tony
Tony

Reputation: 33

ByteBuddy not retaining method annotation that was dynamically added via javassist

When using bytebuddy, the transformed class does not contain the method annotation: @ProtectionContext that was present on the method before (though added dynamically via javassist).

Note: this issue does not happen when the annotation is hardcoded in the class. It only happens when the annotations are added dynamically

As you can see, the annotations are detected properly in the matcher, so it means that byte buddy received the class with the method annotations.

But after calling unloaded.load(...), the method annotations are no where to be found.

I 've tried everything, but it's not working

                        ElementMatcher<MethodDescription> matcher = new 
                        ElementMatcher<MethodDescription>() {

                        @Override
                        public boolean matches(MethodDescription target) {

                            if (target.isAbstract()) {
                                return false;
                            }

                            for (AnnotationDescription a : target.getDeclaredAnnotations()) {
                                if (a.getAnnotationType().getTypeName().equals(ProtectionContext.class.getName())) {
                                    // System.out.println(target);
                                    return true;
                                }
                            }
                            return false;
                        }
                    };

                   Unloaded<?> unloaded = new ByteBuddy()
                            .with(AnnotationRetention.ENABLED)
                            .rebase(c)
                            .method(matcher)

                      .intercept(MethodDelegation.to(Interceptor.class)
                                 .andThen(SuperMethodCall.INSTANCE)
                       )
                            .make();


                Class<?> c = unloaded.load(...).getLoaded();

               // Annotation not found on c

Adding @Inherited to the @interface ProtectionContext is also not working

Upvotes: 1

Views: 90

Answers (1)

Tony
Tony

Reputation: 33

Answering this myself.

The rebase method is overloaded to accept a ClassFileLocator that should be specified to request the modified byte code form Javassist

Upvotes: 1

Related Questions