0

Edit: I just found out that not a IOExcpetion but a FilerException is thrown. Therefore I changed that in the description and the title.

I'm working with Annotation Processing to generate some files for my java project. Now I always get an FilerException when the Annotation Processing tries to generate my files.

This is the way I create the files (GenClass and GenAnnotation are custom classes that abstract the generated classes. They weren't changed in about half a year so I'm sure the error isn't somewhere there. The way I write the files also didn't change in the last year.):

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;

This is the message of the exception:

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

I did change something on my processors, however the error only occurs for a certain type of files (Filters we use to filter data) and I didn't change anything on the processor that generates the filters. I added a new processor that works with a different annotation and those file are generated correctly.

Does anyone know what the cause of this error could be?

mvieghofer
  • 2,846
  • 4
  • 22
  • 51
  • Did you use different filename for using generateJavaSourceFile? Exception message said that "File of specified name is already create, and system cannot create multiple files of same name". I guess you use a file name again. – Fumu 7 Sep 05 '14 at 08:53
  • Yes the filename is the fully qualified name of the class to be written. – mvieghofer Sep 05 '14 at 08:54
  • What is recorded by LOGGER.severe()? How do you get this exception message "Source file already created: /path/to/the/file/to/create"? Is the name of file which is created by the execution of your code different to tha filename in exception message? If they are different, what does a filename in the exception message means? – Fumu 7 Sep 05 '14 at 10:01
  • LOGGER.severe records the error message that is build above. So "Couldn't generate file (com.example.foo)!" and yes the name of the file is the same as the filename in the exception. – mvieghofer Sep 05 '14 at 10:06
  • I want to know that does a file with filename in exception message exist in file system or not? If not found in the place you expected, please search file by name at all of disks. According to my experience, system message does not tell a lie in general. – Fumu 7 Sep 05 '14 at 10:46
  • ok I understand. In the location the filer should write the file, no such file exists. I start by cleaning all generated files so each file in the apt_generated folder (that's the name of the directory my files are generated to) is generated by the processor. I updated the question because I found out, that I get a `FilerExcpetion` instead of an `IOException`. – mvieghofer Sep 05 '14 at 10:53

1 Answers1

0

I had an error in another processor (that had nothing to do with the processor that generates the Filters) and that caused this error. Now that I fixed that error also this behavior stopped. I'm not really sure why this FilerException happened all the time, however it is gone now.

mvieghofer
  • 2,846
  • 4
  • 22
  • 51