Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a annotation to a method #627

Closed
Aleishus opened this issue Mar 18, 2019 · 5 comments
Closed

Add a annotation to a method #627

Aleishus opened this issue Mar 18, 2019 · 5 comments
Assignees
Milestone

Comments

@Aleishus
Copy link

Hi,
I want to add an annotation to a method and not to change the original method body.

example:

origin

 public class Bar {


  public static String make() {
   return "sss";
  }

  public  String m(){
   System.out.println("haha");
   return "bar" ;
  }
}

redefined

 public class Bar {


  public static String make() {
   return "sss";
  }

 
@MyAnnotation
  public  String m(){
   System.out.println("haha");
   return "bar" ;
  }
}
byteBuddy
                .redefine(Bar.class)
                .method(ElementMatchers.named("m")).intercept(//how to use)
                .annotateMethod(new MyAnnotation() {

                    @Override
                    public Class<? extends Annotation> annotationType() {
                        return MyAnnotation.class;
                    }
                })
                .make().toJar(new File("/tmp/sss.jar"));

and I don't know how to write the intercept method that don't change the method body just add an Annotation.

THX!

@raphw
Copy link
Owner

raphw commented Mar 18, 2019

This is an emberassing hole in the API as this should of course be possible but was not thought of as the annotation capabilities were added after the method interception. I will need to adress this some time.

If it works for you, you can rebase a class and use SuperMethodCall.INSTANCE as the interception.

@raphw raphw self-assigned this Mar 18, 2019
@raphw raphw added this to the 1.9.10 milestone Mar 18, 2019
@raphw raphw added the question label Mar 18, 2019
@raphw
Copy link
Owner

raphw commented Aug 2, 2019

From Byte Buddy 1.10.0 it will be possible to do so using MemberAttributeExtension.ForMethod.

@raphw raphw closed this as completed Aug 2, 2019
@chook
Copy link

chook commented Aug 5, 2019

@raphw i believe intercepting and changing a method to an ExceptionMethod did remove the original method annotation, can this be fixed with the MemberAttributeExtension.ForMethod latest addition? should I file a separate issue?

@raphw
Copy link
Owner

raphw commented Aug 6, 2019

This should not happen, are you sure about this? Could you make a reproducer in this case?

@chook
Copy link

chook commented Aug 6, 2019

looks like i misunderstood a Spring annotation.. it's probably there and i thought it will trigger something but it did not. thanks

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

No branches or pull requests

3 participants