Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Its possible use <code>getInterfaces()</code> on the class object and query the result.</p> <p>parameter annotation</p> <pre><code>package mawi12345; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface B { int version() default 0; } </code></pre> <p>method annotation</p> <pre><code>package mawi12345; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Inherited public @interface Revision { int minor() default 0; int major() default 1; } </code></pre> <p>interface with annotations</p> <pre><code>package mawi12345; public interface Foo { @Revision(minor=1, major=3) public void setMark(@B(version=3) int mark); } </code></pre> <p>class with annotations and the Foo interface</p> <pre><code>package mawi12345; public class Test implements Foo { public void setMark(int mark) { } @Revision(minor=2, major=4) public boolean isPassed() { return true; } } </code></pre> <p>Test Class</p> <pre><code>package mawi12345; import java.lang.annotation.Annotation; import java.lang.reflect.Method; public class ReflectionTest { public static void printAnnotations(Class&lt;?&gt; clazz) { // array of methods Method[] methods = clazz.getDeclaredMethods(); System.out.println("found "+methods.length+" methods"); for (int i=0; i&lt;methods.length; i++) { // get the annotations of this method Annotation[] methodAnnotations = methods[i].getAnnotations(); // if you only wont to check for one annotation use getAnnotation(Class&lt;T&gt;) for (Annotation methodAnnotation : methodAnnotations) { System.out.println(methodAnnotation); } // get the parameter annotations (2d array) Annotation[][] parameterAnnotations = methods[i].getParameterAnnotations(); // get an array of parameters Class&lt;?&gt;[] parameters = methods[i].getParameterTypes(); for(int x=0; x&lt;parameterAnnotations.length; x++) { Class&lt;?&gt; parameter = parameters[x]; for(Annotation annotation : parameterAnnotations[x]){ // print the parameter name and his annotation System.out.println(parameter.getName() + " " + annotation); } } } } /** * @param args */ public static void main(String[] args) { // create Test object Test test = new Test(); // get the class Class&lt;?&gt; clazz = test.getClass(); System.out.println("Class Test"); // print annotations printAnnotations(clazz); System.out.println(); // get the interfaces of the class Class&lt;?&gt;[] interfaces = clazz.getInterfaces(); System.out.println("found "+interfaces.length+" interfaces"); // print annotations for each interface for (Class&lt;?&gt; type : interfaces) { System.out.println(type); printAnnotations(type); } } } </code></pre> <p>Output</p> <pre><code>Class Test found 2 methods @mawi12345.Revision(minor=2, major=4) found 1 interfaces interface mawi12345.Foo found 1 methods @mawi12345.Revision(minor=1, major=3) int @mawi12345.B(version=3) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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