Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a kotlin file in java(generated) dir with specific package using AnnotationProcessor

Tags:

android

kotlin

i'm using annotation processing to generate some code and i need to create kotlin file in specific package this is my code

@AutoService(Processor.class)
@IncrementalAnnotationProcessor(IncrementalAnnotationProcessorType.DYNAMIC)
@SuppressWarnings("NullAway")
public final class MyProcessor extends AbstractProcessor {
 
   @Override
   public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
     ....
   }

   private void writeKotlinFile(String pack, String fileName, String fileContent, Element it) {
     .....
       
    }
}
like image 615
Atras VidA Avatar asked Oct 16 '22 02:10

Atras VidA


1 Answers

I think you have to use OutputStream like this in your writeKolinFilee function:

private void writeKotlinFile(String pack, String fileName, String fileContent, Element it) {
    try {
        FileObject filerSourceFile = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT,
            pack, fileName + ".kt", it);
        OutputStream outputStream = filerSourceFile.openOutputStream();
        outputStream.write(fileContent.getBytes());
        outputStream.flush();
        outputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

like image 159
Mort Avatar answered Oct 21 '22 01:10

Mort