From 554fad9e3aa56c75c1604066bb919396a38fd70c Mon Sep 17 00:00:00 2001 From: Tobias Stamann Date: Fri, 27 Oct 2023 14:11:43 +0200 Subject: [PATCH] Added some functions to increase usability --- .../aptk/tools/TypeMirrorWrapper.java | 10 ++++++++++ .../wrapper/AnnotationMirrorWrapper.java | 19 +++++++++++++++++++ .../aptk/tools/wrapper/ElementWrapper.java | 8 ++++++++ .../tools/wrapper/ElementWrapperTest.java | 8 ++++++++ 4 files changed, 45 insertions(+) diff --git a/tools/src/main/java/io/toolisticon/aptk/tools/TypeMirrorWrapper.java b/tools/src/main/java/io/toolisticon/aptk/tools/TypeMirrorWrapper.java index 6c7035db..77572d68 100644 --- a/tools/src/main/java/io/toolisticon/aptk/tools/TypeMirrorWrapper.java +++ b/tools/src/main/java/io/toolisticon/aptk/tools/TypeMirrorWrapper.java @@ -898,6 +898,16 @@ public static Optional getTypeElement(TypeMirror typeMirror) return Optional.empty(); } + /** + * Gets the TypeElements wrapped TypeMirror. + * + * @param typeElement the TypeElement to get the TypeMirror for + * @return The wrapped TypeMirror of the TypeElement + */ + public static TypeMirrorWrapper wrap(TypeElement typeElement) { + return TypeElementWrapper.wrap(typeElement).asType(); + } + /** * Wraps a TypeMirror instance to provide some convenience methods * diff --git a/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/AnnotationMirrorWrapper.java b/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/AnnotationMirrorWrapper.java index 678fc6f7..7d99978a 100644 --- a/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/AnnotationMirrorWrapper.java +++ b/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/AnnotationMirrorWrapper.java @@ -7,6 +7,7 @@ import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; +import javax.lang.model.type.DeclaredType; import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.Optional; @@ -38,6 +39,24 @@ public AnnotationMirror unwrap() { return annotationMirror; } + /** + * Get DeclaredType of wrapped annotation. + * @return the annotations declared type + */ + DeclaredType getAnnotationType() { + return this.annotationMirror.getAnnotationType(); + } + + /** + * Get wrapped DeclaredType of wrapped annotation. + * @return the annotations declared type + */ + TypeMirrorWrapper getWrappedAnnotationType() { + return TypeMirrorWrapper.wrap(getAnnotationType()); + } + + + /** * Returns the "value" attribute * diff --git a/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/ElementWrapper.java b/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/ElementWrapper.java index 884ea4f2..b51450dd 100644 --- a/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/ElementWrapper.java +++ b/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/ElementWrapper.java @@ -173,6 +173,14 @@ public FluentElementFilter filterFlattenedEnclosedElementTree(boolean i return FluentElementFilter.createFluentElementFilter(getFlattenedEnclosedElementTree(includeSelf, maxDepth).stream().map(ElementWrapper::unwrap).collect(Collectors.toList())); } + /** + * Gets all wrapped annotations of element. + * @return a list containing all annotations of the element + */ + public List getAnnotations() { + return this.element.getAnnotationMirrors().stream().map(AnnotationMirrorWrapper::wrap).collect(Collectors.toList()); + } + /** * Gets the annotation * diff --git a/tools/src/test/java/io/toolisticon/aptk/tools/wrapper/ElementWrapperTest.java b/tools/src/test/java/io/toolisticon/aptk/tools/wrapper/ElementWrapperTest.java index 3bcc09c7..5e8b80dc 100644 --- a/tools/src/test/java/io/toolisticon/aptk/tools/wrapper/ElementWrapperTest.java +++ b/tools/src/test/java/io/toolisticon/aptk/tools/wrapper/ElementWrapperTest.java @@ -185,7 +185,15 @@ public void test_validate() { } + @Test + public void test_getAnnotations() { + + CompileTestBuilder.unitTest().defineTestWithPassedInElement(TestClass.class, (processingEnvironment, element) -> { + ElementWrapper unit = ElementWrapper.wrap(element); + MatcherAssert.assertThat("Should find annotation", unit.getAnnotations(),Matchers.hasSize(1)); + }).executeTest(); + } @Test public void test_getAnnotation_byFqnString() {