Class FilerProcessor


  • public class FilerProcessor
    extends java.lang.Object
    A processor class that provides safe and exception-handled operations for interacting with the Filer in an annotation processing environment. This class wraps calls to the underlying Filer instance obtained from the provided ProcessingEnvironment, ensuring robust resource handling and simplifying error management through functional interfaces.

    It supports executing operations on the Filer using a callback model, allowing custom logic to be applied while handling exceptions gracefully using provided handlers.

    Example Usage

     // Creating an instance of FilerProcessor
     FilerProcessor filerProcessor = new FilerProcessor(processingEnv);
    
     // Using processInFiler to create a source file
     filerProcessor.processInFiler(filer -> {
         JavaFileObject file = filer.createSourceFile("com.example.GeneratedClass");
         try (Writer writer = file.openWriter()) {
             writer.write("// Auto-generated class\npublic class GeneratedClass {}");
         }
         return null;
     });
    
     // Using processInFiler with a custom exception handler
     filerProcessor.processInFiler(
         filer -> {
             JavaFileObject file = filer.createSourceFile("com.example.AnotherGeneratedClass");
             try (Writer writer = file.openWriter()) {
                 writer.write("// Auto-generated class\npublic class AnotherGeneratedClass {}");
             }
             return null;
         },
         (filer, e) -> {
             System.err.println("Failed to generate file: " + e.getMessage());
             return null;
         }
     );
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    processInFiler(ThrowableFunction), processInFiler(ThrowableFunction, BiFunction)
    • Constructor Summary

      Constructors 
      Constructor Description
      FilerProcessor​(javax.annotation.processing.ProcessingEnvironment processingEnv)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      javax.tools.JavaFileManager getJavaFileManager()
      Get the JavaFileManager
      <T> T processInFiler​(io.microsphere.lang.function.ThrowableFunction<javax.annotation.processing.Filer,​T> filerCallback)  
      <T> T processInFiler​(io.microsphere.lang.function.ThrowableFunction<javax.annotation.processing.Filer,​T> filerCallback, java.util.function.BiFunction<javax.annotation.processing.Filer,​java.lang.Throwable,​T> exceptionHandler)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FilerProcessor

        public FilerProcessor​(javax.annotation.processing.ProcessingEnvironment processingEnv)
    • Method Detail

      • processInFiler

        public <T> T processInFiler​(io.microsphere.lang.function.ThrowableFunction<javax.annotation.processing.Filer,​T> filerCallback)
      • processInFiler

        public <T> T processInFiler​(io.microsphere.lang.function.ThrowableFunction<javax.annotation.processing.Filer,​T> filerCallback,
                                    java.util.function.BiFunction<javax.annotation.processing.Filer,​java.lang.Throwable,​T> exceptionHandler)
      • getJavaFileManager

        public javax.tools.JavaFileManager getJavaFileManager()
        Get the JavaFileManager
        Returns:
        the JavaFileManager