0

I am trying to use processingEnv.getFiler() to create a source file. But I don't see any source file getting created. Below is the code that I am using:

public void javaPoetEg() {
  Filer filer = super.processingEnv.getFiler();
  MethodSpec main = MethodSpec.methodBuilder("testFiler")
    .addModifiers(Modifier.PUBLIC)
    .returns(void.class)
    .addParameter(String[].class, "args")
    .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!")
    .build();

  TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
    .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
    .addMethod(main)
    .build();


  JavaFile javaFile = JavaFile.builder("com.ankit.annotation", helloWorld)
    .build();

  try{
    javaFile.writeTo(filer);
  } catch (IOException e) {
    e.printStackTrace();
  }
}

And then calling the function javaPoetEg in the overridden function process() of Annotation processor. What am I doing wrong here?

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61

2 Answers2

0

I suggest you take a look at how Filer is supposed to work. Javadoc
The location of the source files might depends on parameters you specify via command line, or, you have to check the default generated directory.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
0

This part: javaFile.writeTo writes to a File, or a PrintStream. So you can do something like this:

File file = new File("/target/directory");
javaFile.writeTo(file); // this will make a new File with all the data you added

Or:

javaFile.writeTo(System.out) // this will print the file representation in the console

Hope this helps. Cheers.

n3k0
  • 577
  • 14
  • 40