Class AnnotationUtil

java.lang.Object
com.github.tadukoo.util.annotation.process.AnnotationUtil

public class AnnotationUtil
extends java.lang.Object
A collection of common functions to be used by Annotation Processors.
Version:
Pre-Alpha
Author:
Logan Ferree (Tadukoo)
  • Field Summary

    Fields
    Modifier and Type Field Description
    static java.lang.String ANNOTATION_PROCESSOR_FILE
    The file path for the Annotation Processor META-INF file
    private javax.lang.model.util.Elements elementUtil
    The Element Util for util methods related to Elements
    private javax.annotation.processing.Filer filer
    The Filer for creating and editing files
    private javax.annotation.processing.Messager messager
    The Messager for sending messages to the compiler
    private javax.lang.model.util.Types typeUtil
    The Type Util for util methods related to Types
  • Constructor Summary

    Constructors
    Constructor Description
    AnnotationUtil​(javax.annotation.processing.ProcessingEnvironment processingEnv)
    Grabs the elementUtil, typeUtil, messager, and filer off the given ProcessingEnvironment and stores them for later use in the various util methods in this class.
  • Method Summary

    Modifier and Type Method Description
    javax.tools.FileObject createFile​(java.lang.String filename)
    Creates a new file (as a FileObject) using the given filename/path.
    void displayCompileError​(javax.lang.model.element.Element element, java.lang.String error)
    Displays a compile error for the given Element with the given error message.
    void displayCompileWarning​(javax.lang.model.element.Element element, java.lang.String warning)
    Displays a compile warning for the given Element with the given warning message.
    java.lang.String getCanonicalClassName​(javax.lang.model.element.Element element)
    Grabs the Canonical Class Name of the given Element.
    javax.tools.FileObject getExistingFile​(java.lang.String filename)
    Grabs the file (as a FileObject) specified by the filename/path.
    java.io.Reader getFileReader​(java.lang.String filename)
    Grabs the existing file specified using getExistingFile(java.lang.String) and then opens a Reader for it and returns it.
    java.io.Writer getFileWriter​(java.lang.String filename)
    Creates a new file using createFile(java.lang.String) and then opens a Writer for it and returns it.
    javax.lang.model.type.TypeMirror getType​(java.lang.Class<?> clazz)
    Grabs a TypeMirror representing the given Class.
    javax.lang.model.type.TypeMirror getType​(javax.lang.model.element.Element element)
    Grabs the TypeMirror of the given Element, if it can be converted to a TypeMirror (may return null).
    javax.lang.model.type.TypeMirror getType​(javax.lang.model.element.TypeElement typeElement)
    Grabs the TypeMirror of the given TypeElement.
    javax.lang.model.element.TypeElement getTypeElement​(java.lang.CharSequence canonicalClassName)
    Grabs the TypeElement represented by the given canonical class name.
    javax.lang.model.element.TypeElement getTypeElement​(java.lang.Class<?> clazz)
    Grabs a TypeElement representing the given Class.
    javax.lang.model.element.TypeElement getTypeElement​(javax.lang.model.element.Element element)
    Casts the given Element as a TypeElement if it is an instance of one.
    boolean isSubType​(javax.lang.model.element.Element element, java.lang.Class<?> clazz)
    Checks if the given Element is a subtype of the given Class.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ANNOTATION_PROCESSOR_FILE

      public static final java.lang.String ANNOTATION_PROCESSOR_FILE
      The file path for the Annotation Processor META-INF file
      See Also:
      Constant Field Values
    • elementUtil

      private final javax.lang.model.util.Elements elementUtil
      The Element Util for util methods related to Elements
    • typeUtil

      private final javax.lang.model.util.Types typeUtil
      The Type Util for util methods related to Types
    • messager

      private final javax.annotation.processing.Messager messager
      The Messager for sending messages to the compiler
    • filer

      private final javax.annotation.processing.Filer filer
      The Filer for creating and editing files
  • Constructor Details

    • AnnotationUtil

      public AnnotationUtil​(javax.annotation.processing.ProcessingEnvironment processingEnv)
      Grabs the elementUtil, typeUtil, messager, and filer off the given ProcessingEnvironment and stores them for later use in the various util methods in this class.
      Parameters:
      processingEnv - The ProcessingEnvironment to grab stuff from
  • Method Details

    • getCanonicalClassName

      public java.lang.String getCanonicalClassName​(javax.lang.model.element.Element element)
      Grabs the Canonical Class Name of the given Element.
      Parameters:
      element - The element to get the canonical class name of
      Returns:
      The canonical class name of the given Element
    • getTypeElement

      public javax.lang.model.element.TypeElement getTypeElement​(java.lang.CharSequence canonicalClassName)
      Grabs the TypeElement represented by the given canonical class name.
      Parameters:
      canonicalClassName - The canonical class name of the class to get a TypeElement of
      Returns:
      The TypeElement representing the class specified
    • getTypeElement

      public javax.lang.model.element.TypeElement getTypeElement​(javax.lang.model.element.Element element)
      Casts the given Element as a TypeElement if it is an instance of one.
      Parameters:
      element - The Element to get as a TypeElement
      Returns:
      The Element cast as a TypeElement, or null if it's not an instanceof TypeElement
    • getTypeElement

      public javax.lang.model.element.TypeElement getTypeElement​(java.lang.Class<?> clazz)
      Grabs a TypeElement representing the given Class.
      Parameters:
      clazz - The Class to get as a TypeElement
      Returns:
      A TypeElement representing the given Class
    • getType

      public javax.lang.model.type.TypeMirror getType​(javax.lang.model.element.TypeElement typeElement)
      Grabs the TypeMirror of the given TypeElement. If the given TypeElement is null, it will return null.
      Parameters:
      typeElement - The TypeElement to get the TypeMirror of
      Returns:
      The TypeMirror for the given TypeElement, or null if the TypeElement is null
    • getType

      public javax.lang.model.type.TypeMirror getType​(javax.lang.model.element.Element element)
      Grabs the TypeMirror of the given Element, if it can be converted to a TypeMirror (may return null).
      Parameters:
      element - The Element to grab the TypeMirror of
      Returns:
      The TypeMirror of the given Element, or null
    • getType

      public javax.lang.model.type.TypeMirror getType​(java.lang.Class<?> clazz)
      Grabs a TypeMirror representing the given Class.
      Parameters:
      clazz - The Class to get a TypeMirror for
      Returns:
      A TypeMirror representing the given Class
    • isSubType

      public boolean isSubType​(javax.lang.model.element.Element element, java.lang.Class<?> clazz)
      Checks if the given Element is a subtype of the given Class.
      Parameters:
      element - The Element that may be a subtype of the Class
      clazz - The Class that may be a supertype of the Element
      Returns:
      If the Element is a subtype of the Class or not
    • displayCompileWarning

      public void displayCompileWarning​(javax.lang.model.element.Element element, java.lang.String warning)
      Displays a compile warning for the given Element with the given warning message.
      Parameters:
      element - The Element to show the warning on
      warning - The warning message to display
    • displayCompileError

      public void displayCompileError​(javax.lang.model.element.Element element, java.lang.String error)
      Displays a compile error for the given Element with the given error message.
      Parameters:
      element - The Element to show the error on
      error - The error message to display
    • createFile

      public javax.tools.FileObject createFile​(java.lang.String filename) throws java.io.IOException
      Creates a new file (as a FileObject) using the given filename/path.
      Parameters:
      filename - The file name/path to use
      Returns:
      A FileObject for the new file
      Throws:
      java.io.IOException - If something goes wrong in creating the file
    • getExistingFile

      public javax.tools.FileObject getExistingFile​(java.lang.String filename) throws java.io.IOException
      Grabs the file (as a FileObject) specified by the filename/path.
      Parameters:
      filename - The file name/path to use
      Returns:
      A FileObject for the existing file
      Throws:
      java.io.IOException - If something goes wrong in grabbing the file
    • getFileReader

      public java.io.Reader getFileReader​(java.lang.String filename) throws java.io.IOException
      Grabs the existing file specified using getExistingFile(java.lang.String) and then opens a Reader for it and returns it.
      Parameters:
      filename - The file name/path to use
      Returns:
      A Reader for the existing file
      Throws:
      java.io.IOException - If something goes wrong in either grabbing the file or opening it to read
    • getFileWriter

      public java.io.Writer getFileWriter​(java.lang.String filename) throws java.io.IOException
      Creates a new file using createFile(java.lang.String) and then opens a Writer for it and returns it.

      Note: This will overwrite the file if it already exists.
      Parameters:
      filename - The file name/path to use
      Returns:
      A Writer for the newly created file
      Throws:
      java.io.IOException - If something goes wrong in creating the file or opening it to write