有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文件管理器总是抛出FilerException

Edit:我刚刚发现抛出的不是IOExcpetion,而是FilerException。因此,我在描述和标题中改变了这一点

我正在使用注释处理为我的java项目生成一些文件。现在,当注释处理试图生成我的文件时,我总是得到一个FilerException

这是我创建文件的方式(GenClass和GenAnnotation是抽象生成的类的自定义类。它们在大约半年内没有更改,所以我确定错误不在哪里。我编写文件的方式在去年也没有更改。):

public static boolean generateJavaSourceFile(final ProcessingEnvironment processingEnv,
        final GenClass element, final String fileName, final Class<?> generatorClass) {
    boolean succeed = false;
    Writer fw = null;
    Filer f = processingEnv.getFiler();

    // Mark the class as generated
    GenAnnotation generatedAnnotation = getAnnotation(generatorClass);
    element.pushImport(generatedAnnotation);
    element.addAnnotation(generatedAnnotation);

    try {
        JavaFileObject jfo = f.createSourceFile(fileName, (Element[]) null);
        // create new java source file
        fw = jfo.openWriter();
        // write the GenClass object into file
        fw.write(element.toString());

        succeed = true;
    } catch (FilerException e) {
        LOGGER.severe("Couldn't generate file (" + fileName + ")!");
        processingEnv.getMessager().printMessage(Kind.ERROR,
                                                 "Could not create source file " + fileName
                                                         + " because it already exists");
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        LOGGER.severe("Couldn't generate file (" + fileName + ")!");
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        if (fw != null) {
            try {
                fw.close(); // flush and close the stream
            } catch (IOException e) {
                LOGGER.severe("Couldn't close file [" + fileName + "]!");
            }
        }
    }
    LOGGER.fine(fileName + " written");
    return succeed;

以下是异常的信息:

Source file already created: /path/to/the/file/to/create

我确实在我的处理器上做了一些更改,但是错误只发生在特定类型的文件(我们用来过滤数据)上,我没有在生成过滤器的处理器上更改任何内容。我添加了一个新的处理器,该处理器使用不同的注释,并且正确生成了这些文件

有人知道这个错误的原因吗


共 (1) 个答案

  1. # 1 楼答案

    我在另一个处理器中出错(与生成过滤器的处理器无关),这导致了这个错误。既然我纠正了这个错误,这个行为也就停止了。我真的不确定为什么这FilerException一直在发生,但现在已经过去了