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 fileprivate javax.lang.model.util.Elements
elementUtil
TheElement Util
for util methods related toElements
private javax.annotation.processing.Filer
filer
TheFiler
for creating and editing filesprivate javax.annotation.processing.Messager
messager
TheMessager
for sending messages to the compilerprivate javax.lang.model.util.Types
typeUtil
TheType Util
for util methods related toTypes
-
Constructor Summary
Constructors Constructor Description AnnotationUtil(javax.annotation.processing.ProcessingEnvironment processingEnv)
Grabs theelementUtil
,typeUtil
,messager
, andfiler
off the givenProcessingEnvironment
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 aFileObject
) using the given filename/path.void
displayCompileError(javax.lang.model.element.Element element, java.lang.String error)
Displays a compile error for the givenElement
with the given error message.void
displayCompileWarning(javax.lang.model.element.Element element, java.lang.String warning)
Displays a compile warning for the givenElement
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 aFileObject
) specified by the filename/path.java.io.Reader
getFileReader(java.lang.String filename)
Grabs the existing file specified usinggetExistingFile(java.lang.String)
and then opens aReader
for it and returns it.java.io.Writer
getFileWriter(java.lang.String filename)
Creates a new file usingcreateFile(java.lang.String)
and then opens aWriter
for it and returns it.javax.lang.model.type.TypeMirror
getType(java.lang.Class<?> clazz)
Grabs aTypeMirror
representing the givenClass
.javax.lang.model.type.TypeMirror
getType(javax.lang.model.element.Element element)
Grabs theTypeMirror
of the givenElement
, if it can be converted to a TypeMirror (may return null).javax.lang.model.type.TypeMirror
getType(javax.lang.model.element.TypeElement typeElement)
Grabs theTypeMirror
of the givenTypeElement
.javax.lang.model.element.TypeElement
getTypeElement(java.lang.CharSequence canonicalClassName)
Grabs theTypeElement
represented by the given canonical class name.javax.lang.model.element.TypeElement
getTypeElement(java.lang.Class<?> clazz)
Grabs aTypeElement
representing the givenClass
.javax.lang.model.element.TypeElement
getTypeElement(javax.lang.model.element.Element element)
Casts the givenElement
as aTypeElement
if it is an instance of one.boolean
isSubType(javax.lang.model.element.Element element, java.lang.Class<?> clazz)
Checks if the givenElement
is a subtype of the givenClass
.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_FILEThe file path for the Annotation Processor META-INF file- See Also:
- Constant Field Values
-
elementUtil
private final javax.lang.model.util.Elements elementUtilTheElement Util
for util methods related toElements
-
typeUtil
private final javax.lang.model.util.Types typeUtilTheType Util
for util methods related toTypes
-
messager
private final javax.annotation.processing.Messager messagerTheMessager
for sending messages to the compiler -
filer
private final javax.annotation.processing.Filer filerTheFiler
for creating and editing files
-
-
Constructor Details
-
AnnotationUtil
public AnnotationUtil(javax.annotation.processing.ProcessingEnvironment processingEnv)Grabs theelementUtil
,typeUtil
,messager
, andfiler
off the givenProcessingEnvironment
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 theTypeElement
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 givenElement
as aTypeElement
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 aTypeElement
representing the givenClass
.- 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 theTypeMirror
of the givenTypeElement
. 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 theTypeMirror
of the givenElement
, 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 aTypeMirror
representing the givenClass
.- 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 givenElement
is a subtype of the givenClass
.- Parameters:
element
- The Element that may be a subtype of the Classclazz
- 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 givenElement
with the given warning message.- Parameters:
element
- The Element to show the warning onwarning
- 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 givenElement
with the given error message.- Parameters:
element
- The Element to show the error onerror
- The error message to display
-
createFile
public javax.tools.FileObject createFile(java.lang.String filename) throws java.io.IOExceptionCreates a new file (as aFileObject
) 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.IOExceptionGrabs the file (as aFileObject
) 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.IOExceptionGrabs the existing file specified usinggetExistingFile(java.lang.String)
and then opens aReader
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.IOExceptionCreates a new file usingcreateFile(java.lang.String)
and then opens aWriter
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
-