与注解处理器的有关的命令有5个,分别如下:
(1)-XprintProcessorInfo 输出有关请求处理程序处理哪些注释的信息
(2)-XprintRounds 输出有关注释处理循环的信息
(3)-processor 路径为包路径,因为指定的路径要通过loadClass()方法来加载
Names of the annotation processors to run. This bypasses the default discovery process.
(4)-processpath
Specifies where to find annotation processors. If this option is not used, then the class path is searched for processors.
(5)-proc: 当指定-proc:none值时不对注解进行处理。
Controls whether annotation processing and compilation are done. -proc:none
means that compilation takes place without annotation processing. -proc:only
means that only annotation processing is done, without any subsequent compilation.
(6)-Xprint 如果有这个命令,则Javac中有个专门的注解处理类PrintingProcessor。注意:当指定这个-Xprint时其它注解类将不生效。
(7)-Akey=value
eg:
@SupportedOptions({"com.sun.tools.javac.sym.Jar", "com.sun.tools.javac.sym.Dest"})
@SupportedAnnotationTypes({"com.test11.CSAnn"})
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class CreateSymbolsProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) {
if (renv.processingOver()) // 没有这个判断会调用两次createSymbols
createSymbols();
return true;
}
void createSymbols() {
String jarName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Jar");
String destName = processingEnv.getOptions().get("com.sun.tools.javac.sym.Dest");
System.out.println(jarName+"/"+destName);
}
}
支持的注解为com.test11.CSAnn,代码如下:
package com.test11;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface CSAnn {
}
在方法上使用这个注解:
public class Test1 {
@CSAnn
private void process() {
}
}
在运行时指定:
"-Acom.sun.tools.javac.sym.Jar=AJarValue"
"-Acom.sun.tools.javac.sym.Dest=ADestValue"
"-processor","com.test11.CreateSymbolsProcessor"
最后打印出AJarValue/ADestValue