Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code does more or less what you ask for - it is a simple proof of concept:</p> <ul> <li>a proper implementation needs to also deal with the <code>declaredAnnotations</code></li> <li>if the implementation of annotations in Class.java changes, the code will break (i.e. it can break at any time in the future)</li> <li>I have no idea if there are side effects...</li> </ul> <p>Output:</p> <blockquote> <p>oldAnnotation = some value<br> modifiedAnnotation = another value</p> </blockquote> <pre><code>public static void main(String[] args) throws Exception { final Something oldAnnotation = (Something) Foobar.class.getAnnotations()[0]; System.out.println("oldAnnotation = " + oldAnnotation.someProperty()); Annotation newAnnotation = new Something() { @Override public String someProperty() { return "another value"; } @Override public Class&lt;? extends Annotation&gt; annotationType() { return oldAnnotation.annotationType(); } }; Field field = Class.class.getDeclaredField("annotations"); field.setAccessible(true); Map&lt;Class&lt;? extends Annotation&gt;, Annotation&gt; annotations = (Map&lt;Class&lt;? extends Annotation&gt;, Annotation&gt;) field.get(Foobar.class); annotations.put(Something.class, newAnnotation); Something modifiedAnnotation = (Something) Foobar.class.getAnnotations()[0]; System.out.println("modifiedAnnotation = " + modifiedAnnotation.someProperty()); } @Something(someProperty = "some value") public static class Foobar { } @Retention(RetentionPolicy.RUNTIME) @interface Something { String someProperty(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload