ByteBuddy没有保留通过javassist动态添加的方法注释

问题描述 投票:1回答:1

使用bytebuddy时,转换后的类不包含方法注释:之前存在于方法上的@ProtectionContext(尽管通过javassist动态添加)。

注意:在类中硬编码注释时不会发生此问题。它仅在动态添加注释时发生

如您所见,在匹配器中正确检测到注释,因此这意味着字节伙伴通过方法注释接收了类。

但在调用unloaded.load(...)之后,方法注释无处可寻。

我已经尝试了一切,但它没有用

                        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

将@Inherited添加到@interface ProtectionContext也无法正常工作

byte-buddy
1个回答
1
投票

自己回答这个问题。

重载rebase方法以接受应该指定的ClassFileLocator,以从Javassist请求修改的字节代码

© www.soinside.com 2019 - 2024. All rights reserved.