Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create your own annotations and apply them to your own classes.</p> <p>If you specify that an annotation is detectable at runtime, you can process it easily with reflection.</p> <p>For example, you could use something like this to print the name of each field in a class that has been marked with the <em>Funky</em> annotation:</p> <pre><code>for (Field someField : AnnotatedClass.getClass().getDeclaredFields()) { if (someField.isAnnotationPresent(Funky.class)) { System.out.println("This field is funky: " + someField.getName()); } } </code></pre> <p>The code to declare the <em>Funky</em> annotation would look something like this:</p> <pre><code>package org.foo.annotations; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Funky { } </code></pre> <p>Here's a class that uses the annotation:</p> <pre><code>package org.foo.examples; import org.foo.annotations.Funky; public class AnnotatedClass { @Funky private String funkyString; private String nonFunkyString; @Funky private Integer funkyInteger; private Integer nonFunkyInteger; } </code></pre> <p>Here's some <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/language/annotations.html" rel="nofollow">more reading on Annotations</a>.</p> <p>Here are the javadocs for the classes used above:</p> <ul> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/annotation/Retention.html" rel="nofollow">Retention annotation</a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/annotation/RetentionPolicy.html" rel="nofollow">RetentionPolicy enum</a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/annotation/Target.html" rel="nofollow">Target annotation</a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Field.html" rel="nofollow">Field class</a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/AccessibleObject.html#isAnnotationPresent%28java.lang.Class%29" rel="nofollow">isAnnotationPresent() method</a></li> <li><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getDeclaredFields%28%29" rel="nofollow">getDeclaredFields() method</a></li> </ul> <hr> <p>I'm trying to understand your car example, but I'm not sure I follow what you want.</p> <p>If you had a list of objects (Jaguar, Porche, Ferrari, Kia) that extend Car and are marked with various car-related annotations, you could create an object that filters the list based on annotations. </p> <p>The code might look like this:</p> <pre><code>@WorldsFinestMotorCar class Jaguar extends Car { // blah blah } @BoringCar class Porche extends Car { // blah blah } @BoringCar class Ferrari extends Car { // blah blah } @IncredibleCar class Kia extends Car { // blah blah } </code></pre> <p>You could implement an <em>AnnotationFilter</em> class that removes cars from the list that do not have a certain annotation. </p> <p>It might look something like this:</p> <pre><code>List&lt;Car&gt; carList = getListOfRandomCars(); AnnotationFilter&lt;Car&gt; annoFilter = new AnnotationFilter&lt;Car&gt;(BoringCar.class); List&lt;Car&gt; boringCars = annoFilter.filter(carList); </code></pre> <p>Is that what you want to do?</p> <p>If so, it can definitely be done.</p> <p>The implementation for AnnotationFilter might look something like this:</p> <pre><code>public class AnnotationFilter&lt;T&gt; { private Class filterAnno; public AnnotationFilter(Class a) { filterAnno = a; } public List&lt;T&gt; filter(List&lt;T&gt; inputList) { if (inputList == null || inputList.isEmpty()) { return inputList; } List&lt;T&gt; filteredList = new ArrayList&lt;T&gt;(); for (T someT : inputList) { if (someT.getClass().isAnnotationPresent(filterAnno)) { filteredList.add(someT); } } return filteredList; } } </code></pre> <p>If that's not what you're after, a specific example would be helpful.</p>
 

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