0

我在扩展 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 配置为将其用作注释处理器。

4

1 回答 1

1

您将自定义属性加载到props变量中,但从未将它们传递给 Velocity。

因此,您应该简单地执行以下操作:

velocityEngine.init(props)
于 2018-07-13T15:01:21.967 回答