jMDA tasks (de.jmda.mproc.task.Task) facilitate JSR 269 annotation processing significantly. Together with the jMDA processing utilities (de.jmda.mproc.ProcessingUtilities) they are like rabbit holes into the wonderland of annotation processing. jMDA provides different task types to address typical use cases for annotation processing.
So how many rounds will be started by javac? This depends on the following circumstances:
- If no supported annotations can be found by javac no processing round will be started. You don't have to worry much about this if you use jMDA type elements tasks (explained in more detail below).
- If supported annotations can be found by javac at least two rounds will be started. You can find out if the last round is running by calling ProcessingUtilities.isProcessingOver().
- If your code produces files using ProcessingUtilities.getFiler() during a processing round another processing round will be started for these files. This continues until no further files are produced.
package
de.jmda.sample.mproc.task;
import
static
de.jmda.mproc.ProcessingUtilities.getFiler;
//
… further import statements
omitted
public
class
JUTTypeElementsTaskProcessingRounds
{
private
class
TypeElementsTask extends
AbstractTypeElementsTaskTypes
{
private
int
round
= 0;
private
boolean
produce;
public
TypeElementsTask(Set<? extends
Class<?>> types, boolean
produce)
{
super(types);
this.produce
= produce;
}
@Override
public
boolean
execute() throws
TaskException
{
StringBuffer
sb = sb("round:
"
+ round
+ ",
produce: "
+ produce);
round++;
if
(produce)
{
try
{
JavaFileObject
javaFileObject = getFiler().createSourceFile("EmptyClass");
Writer
writer = javaFileObject.openWriter();
writer.append("class
EmptyClass {}");
writer.close();
sb.append(",
produced file");
}
catch
(IOException e)
{
throw
new
TaskException("failure
creating source file",
e);
}
produce
= false;
}
if
(isProcessingOver())
{
sb.append(",
last round - goodbye");
}
System.out.println(sb);
return
false;
}
}
@Test
public
void
testProduceFalse() throws
IOException
{
TypeElementsTask
task = new
TypeElementsTask(asSet(String.class),
false);
TaskRunner.run(task);
assertEquals("wrong
number of rounds",
2, task.round);
}
@Test
public
void
testProduceTrue() throws
IOException
{
TypeElementsTask
task = new
TypeElementsTask(asSet(String.class),
true);
TaskRunner.run(task);
assertEquals("wrong
number of rounds",
3, task.round);
}
}
|
No comments:
Post a Comment