Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. public class Test2
  2. {
  3.     public static void main(String[] args) throws Exception
  4.     {
  5.         // editing method annotations
  6.         {
  7.             Method targetMethod = Test2.class.getDeclaredMethod("foobar");
  8.             final Something oldAnnotation = (Something) targetMethod.getAnnotations()[0];
  9.             System.out.println("oldAnnotation = " + oldAnnotation.someProperty());
  10.             Annotation newAnnotation = new Something()
  11.             {
  12.  
  13.                 @Override
  14.                 public String someProperty()
  15.                 {
  16.                     return "value2";
  17.                 }
  18.  
  19.                 @Override
  20.                 public Class<? extends Annotation> annotationType()
  21.                 {
  22.                     return oldAnnotation.annotationType();
  23.                 }
  24.             };
  25.             Class<?> executableClass = Class.forName("java.lang.reflect.Executable");
  26.             Field field = executableClass.getDeclaredField("declaredAnnotations");
  27.             field.setAccessible(true);
  28.             Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>) field.get(targetMethod);
  29.             System.out.println(annotations.get(Something.class));
  30.             annotations.put(Something.class, newAnnotation);
  31.             System.out.println(annotations.get(Something.class));
  32.  
  33.             //targetMethod = Test2.class.getDeclaredMethod("foobar");
  34.             Something modifiedAnnotation = (Something) targetMethod.getAnnotations()[0];
  35.             System.out.println("modifiedAnnotation = " + modifiedAnnotation.someProperty());
  36.         }
  37.         // editing class annotations
  38.         System.out.println("====");
  39.         {
  40.             final Something oldAnnotation = (Something) Foobar.class.getAnnotations()[0];
  41.             System.out.println("oldAnnotation = " + oldAnnotation.someProperty());
  42.             Annotation newAnnotation = new Something()
  43.             {
  44.  
  45.                 @Override
  46.                 public String someProperty()
  47.                 {
  48.                     return "another value";
  49.                 }
  50.  
  51.                 @Override
  52.                 public Class<? extends Annotation> annotationType()
  53.                 {
  54.                     return oldAnnotation.annotationType();
  55.                 }
  56.             };
  57.             Method method = Class.class.getDeclaredMethod("getDeclaredAnnotationMap");
  58.             method.setAccessible(true);
  59.             Map<Class<? extends Annotation>, Annotation> annotations = (Map<Class<? extends Annotation>, Annotation>) method.invoke(Foobar.class);
  60.             annotations.put(Something.class, newAnnotation);
  61.  
  62.             Something modifiedAnnotation = (Something) Foobar.class.getAnnotations()[0];
  63.             System.out.println("modifiedAnnotation = " + modifiedAnnotation.someProperty());
  64.         }
  65.     }
  66.  
  67.     @Something(someProperty = "some value")
  68.     public static class Foobar
  69.     {
  70.     }
  71.  
  72.     @Something(someProperty = "value1")
  73.     public static void foobar()
  74.     {
  75.  
  76.     }
  77.  
  78.     @Retention(RetentionPolicy.RUNTIME)
  79.     @interface Something
  80.     {
  81.         String someProperty();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement