Class APContext

java.lang.Object
io.avaje.spi.internal.APContext

@Generated("avaje-prism-generator") public final class APContext extends Object
Utiliy Class that stores the ProcessingEnvironment and provides various helper methods
  • Method Details

    • init

      public static void init(ProcessingEnvironment processingEnv)
      Initialize the ThreadLocal containing the Processing Enviroment. this typically should be called during the init phase of processing. Be sure to run the clear method at the last round of processing
      Parameters:
      processingEnv - the current annotation processing enviroment
    • init

      public static void init(APContext.Ctx context, int jdkVersion, boolean preview)
      Initialize the ThreadLocal containing the ProcessingEnvironment. Be sure to run the clear method at the last round of processing
      Parameters:
      context - the current annotation processing enviroment
      jdkVersion - the JDK version number
      preview - whether preview features are enabled
    • clear

      public static void clear()
      Clears the ThreadLocal containing the ProcessingEnvironment.
    • jdkVersion

      public static int jdkVersion()
      Returns the source version that any generated source and class files should conform to
      Returns:
      the source version as an int
    • previewEnabled

      public static boolean previewEnabled()
      Returns whether --preview-enabled has been added to compiler flags.
      Returns:
      true if preview features are enabled
    • logError

      public static void logError(Element e, String msg, Object... args)
      Prints an error at the location of the element.
      Parameters:
      e - the element to use as a position hint
      msg - the message, or an empty string if none
      args - String#format arguments
    • logError

      public static void logError(String msg, Object... args)
      Prints an error.
      Parameters:
      msg - the message, or an empty string if none
      args - String#format arguments
    • logWarn

      public static void logWarn(Element e, String msg, Object... args)
      Prints an warning at the location of the element.
      Parameters:
      e - the element to use as a position hint
      msg - the message, or an empty string if none
      args - String#format arguments
    • logWarn

      public static void logWarn(String msg, Object... args)
      Prints a warning.
      Parameters:
      msg - the message, or an empty string if none
      args - String#format arguments
    • logNote

      public static void logNote(Element e, String msg, Object... args)
      Prints a note.
      Parameters:
      msg - the message, or an empty string if none
      args - String#format arguments
    • logNote

      public static void logNote(String msg, Object... args)
      Prints a note at the location of the element.
      Parameters:
      msg - the message, or an empty string if none
      args - String#format arguments
      e - the element to use as a position hint
    • elementsAnnotatedWith

      public static Set<? extends Element> elementsAnnotatedWith(RoundEnvironment round, String annotationFQN)
      Returns the elements annotated with the given annotation interface.
      Parameters:
      round - RoundEnviroment to extract the elements
      annotationFQN - the fqn of the annotation
      Returns:
      the elements annotated with the given annotation interface,or an empty set if there are none
    • createSourceFile

      public static JavaFileObject createSourceFile(CharSequence name, Element... originatingElements) throws IOException
      Create a file writer for the given class name.
      Parameters:
      name - canonical (fully qualified) name of the principal class or interface being declared in this file or a package name followed by ".package-info" for a package information file
      originatingElements - class, interface, package, or module elements causally associated with the creation of this file, may be elided or null
      Returns:
      a JavaFileObject to write the new source file
      Throws:
      IOException
    • typeElement

      public static TypeElement typeElement(String name)
      Returns a type element given its canonical name.
      Parameters:
      name - the canonical name
      Returns:
      the named type element, or null if no type element can be uniquely determined
    • asTypeElement

      public static TypeElement asTypeElement(TypeMirror t)
      Returns the element corresponding to a type.The type may be a DeclaredType or TypeVariable.Returns null if the type is not one with a corresponding element.
      Parameters:
      t - the type to map to an element
      Returns:
      the element corresponding to the given type
    • processingEnv

      public static ProcessingEnvironment processingEnv()
      Returns:
      the enviroment
    • filer

      public static Filer filer()
      Get current Filer from the ProcessingEnvironment
      Returns:
      the filer
    • elements

      public static Elements elements()
      Get current Elements from the ProcessingEnvironment
      Returns:
      the filer
    • messager

      public static Messager messager()
      Get current Messager from the ProcessingEnvironment
      Returns:
      the messager
    • types

      public static Types types()
      Get current Types from the ProcessingEnvironment
      Returns:
      the types
    • getOption

      public static Optional<String> getOption(String key)
      Returns processor-specific option passed to the annotation processing tool.
      Parameters:
      key - the key of the processor option
      Returns:
      an Optional for the processor option, or null if none is available.
    • isAssignable

      public static boolean isAssignable(String type, String superType)
      Determine whether the first type can be assigned to the second
      Parameters:
      type - string type to check
      superType - the type that should be assignable to.
      Returns:
      true if type can be assinged to supertype
    • isAssignable

      public static boolean isAssignable(TypeElement type, String superType)
      Determine whether the first type can be assigned to the second
      Parameters:
      type - type to check
      superType - the type that should be assignable to.
      Returns:
      true if type can be assinged to supertype
    • setProjectModuleElement

      public static void setProjectModuleElement(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
      Discover the ModuleElement for the project being processed and set in the context.
      Parameters:
      annotations - the annotation interfaces requested to be processed
      roundEnv - environment for information about the current and prior round
    • getProjectModuleElement

      public static ModuleElement getProjectModuleElement()
      Retrieve the project's ModuleElement. setProjectModuleElement must be called before this.
      Returns:
      the ModuleElement associated with the current project
    • moduleInfoReader

      public static Optional<ModuleInfoReader> moduleInfoReader()
      Retrieve the root module-info reader if it can be read
    • getModuleInfoReader

      public static BufferedReader getModuleInfoReader() throws IOException
      Gets a BufferedReader for the project's module-info.java source file.

      Calling ModuleElement's getDirectives() method has a chance of making compilation fail in certain situations. Therefore, manually parsing module-info.java seems to be the safest way to get module information.

      Returns:
      Throws:
      IOException - if unable to read the module-info
    • getBuildResource

      public static Path getBuildResource(String path) throws IOException
      Given the relative path, gets a Path from the Maven target/Gradle build folder.
      Parameters:
      path - the relative path of the file in the target/build folder
      Returns:
      the file object
      Throws:
      IOException - if unable to retrieve the file
    • isTestCompilation

      public static boolean isTestCompilation()
      Return true if the compiler is creating test classes.
      Returns:
      Whether the current apt compilation is for test-compile.