Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The method <strong><code>getAllAnnotationsOfPackage()</code></strong> does the trick.</p> <p>It gets all annotations for a given <code>AnnotatedElement</code> (such as <code>Class</code>, <code>Method</code> and <code>Field</code>) that belong to the <code>annotationsPackage</code> package:</p> <pre><code>public static List&lt;Annotation&gt; getAllAnnotationsOfPackage(AnnotatedElement annotatedElement, String annotationsPackage) { Annotation[] as = annotatedElement.getAnnotations(); List&lt;Annotation&gt; asList = new LinkedList&lt;Annotation&gt;(); for (int i = 0; i &lt; as.length; i++) { if (as[i].annotationType().getPackage().getName() .startsWith(annotationsPackage)) { asList.add(as[i]); } } return asList; } </code></pre> <p>Here's a working code (paste it on a GetAnnotationsOfPackage.java file) that <strong>goes through all methods and fields of a given class and gets all annotations</strong> of the given package:</p> <pre><code>import java.lang.annotation.Annotation; import java.lang.reflect.*; import java.util.*; import javax.xml.bind.annotation.*; public class GetAnnotationsOfPackage { @XmlRootElement(name="RootElement") @XmlAccessorType(XmlAccessType.FIELD) public class Root { @XmlElement(name="SubElement") public String subElement; } public static void main(String[] args) { List&lt;Annotation&gt; as = getAnnotationsOfPackage(Root.class, "javax.xml.bind.annotation"); for (Annotation annotation : as) { System.out.println(annotation.annotationType().getName()); } } public static List&lt;Annotation&gt; getAnnotationsOfPackage(Class&lt;?&gt; classToCheck, String annotationsPackage) { List&lt;Annotation&gt; annotationsList = getAllAnnotationsOfPackage(classToCheck, annotationsPackage); Method[] ms = classToCheck.getDeclaredMethods(); for (int i = 0; i &lt; ms.length; i++) { annotationsList.addAll(getAllAnnotationsOfPackage(ms[i], annotationsPackage)); } Field[] fs = classToCheck.getDeclaredFields(); for (int i = 0; i &lt; fs.length; i++) { annotationsList.addAll(getAllAnnotationsOfPackage(fs[i], annotationsPackage)); } return annotationsList; } public static List&lt;Annotation&gt; getAllAnnotationsOfPackage(AnnotatedElement annotatedElement, String annotationsPackage) { Annotation[] as = annotatedElement.getAnnotations(); List&lt;Annotation&gt; asList = new LinkedList&lt;Annotation&gt;(); for (int i = 0; i &lt; as.length; i++) { if (as[i].annotationType().getPackage().getName().startsWith(annotationsPackage)) { asList.add(as[i]); } } return asList; } } </code></pre> <p>The <code>main()</code> method is <strong>getting all annotations from <code>"javax.xml.bind.annotation"</code></strong> of the <code>Root</code> class and <strong>printing their names</strong>. Here's the output:</p> <pre class="lang-none prettyprint-override"><code>javax.xml.bind.annotation.XmlRootElement javax.xml.bind.annotation.XmlAccessorType javax.xml.bind.annotation.XmlElement </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.
 

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