Skip to content

Commit

Permalink
Added some functions to increase usability
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Oct 27, 2023
1 parent b2fbf8c commit 554fad9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,16 @@ public static Optional<TypeElementWrapper> 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public FluentElementFilter<Element> 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<AnnotationMirrorWrapper> getAnnotations() {
return this.element.getAnnotationMirrors().stream().map(AnnotationMirrorWrapper::wrap).collect(Collectors.toList());
}

/**
* Gets the annotation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,15 @@ public void test_validate() {

}

@Test
public void test_getAnnotations() {

CompileTestBuilder.unitTest().<TypeElement>defineTestWithPassedInElement(TestClass.class, (processingEnvironment, element) -> {
ElementWrapper<TypeElement> unit = ElementWrapper.wrap(element);
MatcherAssert.assertThat("Should find annotation", unit.getAnnotations(),Matchers.hasSize(1));
}).executeTest();

}
@Test
public void test_getAnnotation_byFqnString() {

Expand Down

0 comments on commit 554fad9

Please sign in to comment.