Dynamically add annotation to an existing class

Posted by ng on Stack Overflow See other posts from Stack Overflow or by ng
Published on 2010-05-31T16:48:23Z Indexed on 2010/05/31 16:53 UTC
Read the original article Hit count: 190

Filed under:

I have the following class

public class Person {
   ...
}

I would like to create another class that would look like this.

@SomeAnnotation
public class Person {
   ...
}

Via a simple method like so.

public static Class addAnnotation(Class originalType, Class<? extends Annotation> annotation) {
   // what goes here?
}

Is there an easy way to do this via ASM for example? What dependencies would I need. I have tried to Google this however the examples I have found are either incomplete or are doing something else. Other frameworks such as javassist would be just as good.

© Stack Overflow or respective owner

Related posts about java