Class APContext

    • Method Detail

      • 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
      • 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:
        e - the element to use as a position hint
        msg - the message, or an empty string if none
        args - String#format arguments
      • 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
      • 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
      • 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