Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficient Java Annotations
    text
    copied!<p>I'm a huge fan of Java's annotations, but find it a pain in the neck to have to include Google's <a href="https://code.google.com/p/reflections/" rel="nofollow">Reflections</a> or <a href="http://scannotation.sourceforge.net/" rel="nofollow">Scannotations</a> every time I want to make my own.</p> <p>I haven't been able to find any documentation about Java being able to automatically scan for annotations &amp; use them appropriately, without the help of a container or alike.</p> <p><strong>Question:</strong> Have I missed something fundamental about Java, or were annotations always designed such that manual scanning &amp; checking is required? Is there some built-in way of handling annotations?</p> <p><em>To clarify further</em></p> <p>I'd like to be able to approach annotations in Java a little more programatically. For instance, say you wanted to build a <code>List</code> of <code>Car</code>s. To do this, you annotate the list with a class that can populate the list for you. For instance:</p> <pre><code>@CarMaker List&lt;Car&gt; cars = new List&lt;Car&gt;(); </code></pre> <p>In this example, the <code>CarMaker</code> annotation is approached by Java, who strikes a deal and asks them how many cars they want to provide. It's up to the <code>CarMaker</code> annotation/class to then provide them with a list of which cars to include. This could be all classes with <code>@CarType</code> annotations, and a <code>Car</code> interface.</p> <p><em>Another way of looking at it</em>, is that if you know you want to build something like this: <code>List&lt;Car&gt; cars</code>, you could annotate it with <code>@ListMaker&lt;Car&gt;</code>. The <code>ListMaker</code> is something built into Java. It looks for all classes annotated with <code>@CarType</code>, and populates the list accordingly.</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