3030
3131public class AnnotationHelper {
3232
33- protected final ProcessingEnvironment processingEnv ;
34-
35- public AnnotationHelper (ProcessingEnvironment processingEnv ) {
36- this .processingEnv = processingEnv ;
37- }
38-
39- /**
40- * Tests whether one type is a subtype of another. Any type is considered to
41- * be a subtype of itself.
42- *
43- * @param t1
44- * the first type
45- * @param t2
46- * the second type
47- * @return true if and only if the first type is a subtype of the second
48- * @throws IllegalArgumentException
49- * if given an executable or package type
50- * @see Types#isSubtype(TypeMirror, TypeMirror)
51- */
52- public boolean isSubtype (TypeMirror t1 , TypeMirror t2 ) {
53- return processingEnv .getTypeUtils ().isSubtype (t1 , t2 );
54- }
55-
56- public boolean isSubtype (TypeElement t1 , TypeElement t2 ) {
57- return isSubtype (t1 .asType (), t2 .asType ());
58- }
59-
60- public TypeElement typeElementFromQualifiedName (String qualifiedName ) {
61- return processingEnv .getElementUtils ().getTypeElement (qualifiedName );
62- }
63-
64- public AnnotationMirror findAnnotationMirror (Element annotatedElement , Class <? extends Annotation > annotationClass ) {
65- List <? extends AnnotationMirror > annotationMirrors = annotatedElement .getAnnotationMirrors ();
66-
67- for (AnnotationMirror annotationMirror : annotationMirrors ) {
68- TypeElement annotationElement = (TypeElement ) annotationMirror .getAnnotationType ().asElement ();
69- if (isAnnotation (annotationElement , annotationClass )) {
70- return annotationMirror ;
71- }
72- }
73- return null ;
74- }
75-
76- public boolean isAnnotation (TypeElement annotation , Class <? extends Annotation > annotationClass ) {
77- return annotation .getQualifiedName ().toString ().equals (annotationClass .getName ());
78- }
79-
80- public void printAnnotationError (Element annotatedElement , Class <? extends Annotation > annotationClass , String message ) {
81- printAnnotationMessage (Diagnostic .Kind .ERROR , annotatedElement , annotationClass , message );
82- }
83-
84- public void printAnnotationWarning (Element annotatedElement , Class <? extends Annotation > annotationClass , String message ) {
85- printAnnotationMessage (Diagnostic .Kind .WARNING , annotatedElement , annotationClass , message );
86- }
87-
88- public void printAnnotationMessage (Diagnostic .Kind diagnosticKind , Element annotatedElement , Class <? extends Annotation > annotationClass , String message ) {
89- AnnotationMirror annotationMirror = findAnnotationMirror (annotatedElement , annotationClass );
90- if (annotationMirror != null ) {
91- processingEnv .getMessager ().printMessage (diagnosticKind , message , annotatedElement , annotationMirror );
92- } else {
93- printError (annotatedElement , message );
94- }
95- }
96-
97- public void printError (Element element , String message ) {
98- processingEnv .getMessager ().printMessage (Diagnostic .Kind .ERROR , message , element );
99- }
100-
101- public boolean isPrivate (Element element ) {
102- return element .getModifiers ().contains (Modifier .PRIVATE );
103- }
104-
105- public boolean isAbstract (Element element ) {
106- return element .getModifiers ().contains (Modifier .ABSTRACT );
107- }
108-
109- public boolean isInterface (TypeElement element ) {
110- return element .getKind ().isInterface ();
111- }
112-
113- public boolean isFinal (Element element ) {
114- return element .getModifiers ().contains (Modifier .FINAL );
115- }
116-
117- public boolean isSynchronized (Element element ) {
118- return element .getModifiers ().contains (Modifier .SYNCHRONIZED );
119- }
120-
121- public Elements getElementUtils () {
122- return processingEnv .getElementUtils ();
123- }
33+ protected final ProcessingEnvironment processingEnv ;
34+
35+ public AnnotationHelper (ProcessingEnvironment processingEnv ) {
36+ this .processingEnv = processingEnv ;
37+ }
38+
39+ /**
40+ * Tests whether one type is a subtype of another. Any type is considered to
41+ * be a subtype of itself.
42+ *
43+ * @param t1
44+ * the first type
45+ * @param t2
46+ * the second type
47+ * @return true if and only if the first type is a subtype of the second
48+ * @throws IllegalArgumentException
49+ * if given an executable or package type
50+ * @see Types#isSubtype(TypeMirror, TypeMirror)
51+ */
52+ public boolean isSubtype (TypeMirror t1 , TypeMirror t2 ) {
53+ return processingEnv .getTypeUtils ().isSubtype (t1 , t2 );
54+ }
55+
56+ public boolean isSubtype (TypeElement t1 , TypeElement t2 ) {
57+ return isSubtype (t1 .asType (), t2 .asType ());
58+ }
59+
60+ public TypeElement typeElementFromQualifiedName (String qualifiedName ) {
61+ return processingEnv .getElementUtils ().getTypeElement (qualifiedName );
62+ }
63+
64+ public AnnotationMirror findAnnotationMirror (Element annotatedElement , Class <? extends Annotation > annotationClass ) {
65+ List <? extends AnnotationMirror > annotationMirrors = annotatedElement .getAnnotationMirrors ();
66+
67+ for (AnnotationMirror annotationMirror : annotationMirrors ) {
68+ TypeElement annotationElement = (TypeElement ) annotationMirror .getAnnotationType ().asElement ();
69+ if (isAnnotation (annotationElement , annotationClass )) {
70+ return annotationMirror ;
71+ }
72+ }
73+ return null ;
74+ }
75+
76+ public boolean isAnnotation (TypeElement annotation , Class <? extends Annotation > annotationClass ) {
77+ return annotation .getQualifiedName ().toString ().equals (annotationClass .getName ());
78+ }
79+
80+ public void printAnnotationError (Element annotatedElement , Class <? extends Annotation > annotationClass , String message ) {
81+ printAnnotationMessage (Diagnostic .Kind .ERROR , annotatedElement , annotationClass , message );
82+ }
83+
84+ public void printAnnotationWarning (Element annotatedElement , Class <? extends Annotation > annotationClass , String message ) {
85+ printAnnotationMessage (Diagnostic .Kind .WARNING , annotatedElement , annotationClass , message );
86+ }
87+
88+ public void printAnnotationMessage (Diagnostic .Kind diagnosticKind , Element annotatedElement , Class <? extends Annotation > annotationClass , String message ) {
89+ AnnotationMirror annotationMirror = findAnnotationMirror (annotatedElement , annotationClass );
90+ if (annotationMirror != null ) {
91+ processingEnv .getMessager ().printMessage (diagnosticKind , message , annotatedElement , annotationMirror );
92+ } else {
93+ printError (annotatedElement , message );
94+ }
95+ }
96+
97+ public void printError (Element element , String message ) {
98+ processingEnv .getMessager ().printMessage (Diagnostic .Kind .ERROR , message , element );
99+ }
100+
101+ public boolean isPrivate (Element element ) {
102+ return element .getModifiers ().contains (Modifier .PRIVATE );
103+ }
104+
105+ public boolean isAbstract (Element element ) {
106+ return element .getModifiers ().contains (Modifier .ABSTRACT );
107+ }
108+
109+ public boolean isInterface (TypeElement element ) {
110+ return element .getKind ().isInterface ();
111+ }
112+
113+ public boolean isFinal (Element element ) {
114+ return element .getModifiers ().contains (Modifier .FINAL );
115+ }
116+
117+ public boolean isSynchronized (Element element ) {
118+ return element .getModifiers ().contains (Modifier .SYNCHRONIZED );
119+ }
120+
121+ public Elements getElementUtils () {
122+ return processingEnv .getElementUtils ();
123+ }
124+
125+ public Types getTypeUtils () {
126+ return processingEnv .getTypeUtils ();
127+ }
124128
125129}
0 commit comments