我在扩展 AbstractProcessor 的注释处理器中初始化速度引擎,如下所示:
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
{
String fqClassName = null;
String className = null;
String packageName = null;
Map<String, VariableElement> fields = new HashMap<String, VariableElement>();
Map<String, ExecutableElement> methods = new HashMap<String, ExecutableElement>();
...
if (fqClassName != null)
{
Properties props = new Properties();
URL url = this.getClass().getClassLoader().getResource("velocity.properties");
VelocityContext velocityContext = null;
Template template = null;
JavaFileObject javaFileObject;
Writer writer;
try
{
processingEnv.getMessager().printMessage(Kind.NOTE, "Before");
props.load(url.openStream());
processingEnv.getMessager().printMessage(Kind.NOTE, "Props: " + props);
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("resource.loader", "classpath");
velocityEngine.init();
...
} catch (Exception e1)
{
processingEnv.getMessager().printMessage(Kind.ERROR,
"Exception: " + e1.getMessage() + " : " + e1.getStackTrace());
}
}
return false;
}
调用方法时会发生异常init()
。
我将带有 Velocity jar 文件的处理器导出为 jar 文件,并将 eclipse 配置为将其用作注释处理器。