Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Annotations and apt (fundamentals)
    text
    copied!<p>I'm really rolling up my sleeves and trying to understand Java annotations for the first time, and have read the Sun, Oracle and Wikipedia articles on the subject. They're easy to understand conceptually, but am finding it difficult putting all the pieces of the puzzle together.</p> <p>The following example is probably terrible engineering, but just humor me (it's an <em>example</em>!).</p> <p>Let's say I have the following class:</p> <pre> public Widget { // ... public void foo(int cmd) { switch(cmd) { case 1: function1(); break; case 2: function2(); break; case 3: default: function3(); break; } } } </pre> <p>Now, somewhere else in my project, I have another class, <strong>SpaceShuttle</strong>, that has a method called <strong>blastOff()</strong>:</p> <pre> public class SpaceShuttle { // ... public void blastOff() { // ... } } </pre> <p>Now then, I want to configure an annotation called <strong>Widgetize</strong> so that any methods annotated with <em>@Widgetize</em> will have Widget::foo(int) invoked prior to their own call.</p> <pre> @interface Widgetize { int cmd() default 2; } </pre> <p>So now let's revisit SpaceShuttle:</p> <pre> public class SpaceShuttle { // ... @Widgetize(3) public void blastOff() { // Since we pass a cmd of "3" to @Widgetize, // Widget::function3() should be invoked, per // Widget::foo()'s definition. } } </pre> <p>Alas, my questions!</p> <ol> <li><p>I assume that somewhere I need to define an annotation processor; a Java class that will specify what to do when @Widgetize(int) annotations are encountered, yes? Or does this happen in, say, XML config files that get fed into <em>apt</em> (like the way ant reads build.xml files)?</p></li> <li><p><strong>Edit:</strong> If I was correct about these annotation processors in question #1 above, then how do I "map"/"register"/make known these processors to the apt?</p></li> <li><p>In buildscripts, is apt typically ran <em>before</em> javac, so that annotation-based changes or code generation takes place prior to the compile? (This is a best practices-type question).</p></li> </ol> <p>Thanks and I apologize for my code samples, they turned out a lot bulkier than I intended them to (!)</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