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.StringANNOTATION_PROCESSOR_FILEThe file path for the Annotation Processor META-INF fileprivate javax.lang.model.util.ElementselementUtilTheElement Utilfor util methods related toElementsprivate javax.annotation.processing.FilerfilerTheFilerfor creating and editing filesprivate javax.annotation.processing.MessagermessagerTheMessagerfor sending messages to the compilerprivate javax.lang.model.util.TypestypeUtilTheType Utilfor util methods related toTypes -
Constructor Summary
Constructors Constructor Description AnnotationUtil(javax.annotation.processing.ProcessingEnvironment processingEnv)Grabs theelementUtil,typeUtil,messager, andfileroff the givenProcessingEnvironmentand stores them for later use in the various util methods in this class. -
Method Summary
Modifier and Type Method Description javax.tools.FileObjectcreateFile(java.lang.String filename)Creates a new file (as aFileObject) using the given filename/path.voiddisplayCompileError(javax.lang.model.element.Element element, java.lang.String error)Displays a compile error for the givenElementwith the given error message.voiddisplayCompileWarning(javax.lang.model.element.Element element, java.lang.String warning)Displays a compile warning for the givenElementwith the given warning message.java.lang.StringgetCanonicalClassName(javax.lang.model.element.Element element)Grabs the Canonical Class Name of the given Element.javax.tools.FileObjectgetExistingFile(java.lang.String filename)Grabs the file (as aFileObject) specified by the filename/path.java.io.ReadergetFileReader(java.lang.String filename)Grabs the existing file specified usinggetExistingFile(java.lang.String)and then opens aReaderfor it and returns it.java.io.WritergetFileWriter(java.lang.String filename)Creates a new file usingcreateFile(java.lang.String)and then opens aWriterfor it and returns it.javax.lang.model.type.TypeMirrorgetType(java.lang.Class<?> clazz)Grabs aTypeMirrorrepresenting the givenClass.javax.lang.model.type.TypeMirrorgetType(javax.lang.model.element.Element element)Grabs theTypeMirrorof the givenElement, if it can be converted to a TypeMirror (may return null).javax.lang.model.type.TypeMirrorgetType(javax.lang.model.element.TypeElement typeElement)Grabs theTypeMirrorof the givenTypeElement.javax.lang.model.element.TypeElementgetTypeElement(java.lang.CharSequence canonicalClassName)Grabs theTypeElementrepresented by the given canonical class name.javax.lang.model.element.TypeElementgetTypeElement(java.lang.Class<?> clazz)Grabs aTypeElementrepresenting the givenClass.javax.lang.model.element.TypeElementgetTypeElement(javax.lang.model.element.Element element)Casts the givenElementas aTypeElementif it is an instance of one.booleanisSubType(javax.lang.model.element.Element element, java.lang.Class<?> clazz)Checks if the givenElementis 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 Utilfor util methods related toElements -
typeUtil
private final javax.lang.model.util.Types typeUtilTheType Utilfor util methods related toTypes -
messager
private final javax.annotation.processing.Messager messagerTheMessagerfor sending messages to the compiler -
filer
private final javax.annotation.processing.Filer filerTheFilerfor creating and editing files
-
-
Constructor Details
-
AnnotationUtil
public AnnotationUtil(javax.annotation.processing.ProcessingEnvironment processingEnv)Grabs theelementUtil,typeUtil,messager, andfileroff the givenProcessingEnvironmentand 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 theTypeElementrepresented 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 givenElementas aTypeElementif 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 aTypeElementrepresenting 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 theTypeMirrorof 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 theTypeMirrorof 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 aTypeMirrorrepresenting 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 givenElementis 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 givenElementwith 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 givenElementwith 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 aReaderfor 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 aWriterfor 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
-