Skip to content

Commit

Permalink
[#147] Fixed issues with ElementWrapper.filterFlattenedEnclosedElemen…
Browse files Browse the repository at this point in the history
…tTree()
  • Loading branch information
tobiasstamann committed Apr 5, 2024
1 parent 629d54e commit b65428e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ public List<ElementWrapper<Element>> getFlattenedEnclosedElementTree(boolean inc
* @return a list of all elements up to the max depth of the enclosed element tree plus element itself if includeSelf flag is set
*/
public List<ElementWrapper<Element>> getFlattenedEnclosedElementTree(boolean includeSelf, int maxDepth) {
return ElementUtils.AccessEnclosingElements.getFlattenedEnclosingElementsTree(this.unwrap(), includeSelf, maxDepth)
.stream().map(ExecutableElementWrapper::wrap).collect(Collectors.toList());
return ElementUtils.AccessEnclosedElements.flattenEnclosedElementTree(this.unwrap(), includeSelf, maxDepth)
.stream().map(ElementWrapper::wrap).collect(Collectors.toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.toolisticon.aptk.tools.wrapper;

import io.toolisticon.aptk.common.ToolingProvider;

import javax.lang.model.element.PackageElement;
import java.util.Optional;

/**
* Wrapper for PackageElement.
Expand Down Expand Up @@ -45,4 +48,16 @@ public static PackageElementWrapper wrap(PackageElement element) {
return new PackageElementWrapper(element);
}

/**
* Gets the PackageElementWrapper by using a fully qualified package name.
* @param fqn the fully qualified name of the package
* @return an Optional containing the PackageElementWrapper or an empty Optional if the package element can't be found
*/
public static Optional<PackageElementWrapper> getByFqn(String fqn) {

PackageElement packageElement = ToolingProvider.getTooling().getElements().getPackageElement(fqn);
return packageElement != null ? Optional.of(PackageElementWrapper.wrap(packageElement)) : Optional.empty();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.toolisticon.cute.UnitTest;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;

Expand All @@ -25,6 +26,7 @@
import java.lang.annotation.Target;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -190,10 +192,11 @@ 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));
MatcherAssert.assertThat("Should find annotation", unit.getAnnotations(), Matchers.hasSize(1));
}).executeTest();

}

@Test
public void test_getAnnotation_byFqnString() {

Expand Down Expand Up @@ -275,7 +278,6 @@ public void test_getFirstEnclosingElementWithKind() {
MatcherAssert.assertThat(unit.<PackageElementWrapper>getFirstEnclosingElementWithKind(ElementKind.PACKAGE).get().getQualifiedName(), Matchers.is(ElementWrapperTest.class.getPackage().getName()));



}).executeTest();

}
Expand Down Expand Up @@ -943,4 +945,26 @@ public void test_toVariableElementWrapper() {
}


static class FlattenedNestedTestClass {

}
@Test
@Ignore
public void test_filterFlattenedEnclosedElementTree() {
CompileTestBuilder.unitTest().defineTest((processingEnvironment, element) -> {
try {
ToolingProvider.setTooling(processingEnvironment);
PackageElementWrapper packageElementWrapper = PackageElementWrapper.getByFqn(ElementWrapperTest.class.getPackage().getName()).get();
Set<String> types = packageElementWrapper.filterFlattenedEnclosedElementTree().applyFilter(AptkCoreMatchers.IS_TYPE_ELEMENT).getResult().stream().map(e -> e.getQualifiedName().toString()).collect(Collectors.toSet());
MatcherAssert.assertThat("Must find this class", types.contains(ElementWrapperTest.class.getCanonicalName()));
MatcherAssert.assertThat("Must find nested class", types.contains(FlattenedNestedTestClass.class.getCanonicalName()));
}finally {
ToolingProvider.clearTooling();
}
})
.compilationShouldSucceed()
.executeTest();
}


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.toolisticon.aptk.tools.wrapper;

import io.toolisticon.aptk.common.ToolingProvider;
import io.toolisticon.cute.CompileTestBuilder;
import io.toolisticon.cute.PassIn;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;

Expand All @@ -26,6 +28,46 @@ public void test_getQualifiedName() {

}

@Test
public void test_getByFqn() {

CompileTestBuilder.unitTest().<Element>defineTest((processingEnvironment, element) -> {

try {

ToolingProvider.setTooling(processingEnvironment);

String fqn = PackageElementWrapperTest.class.getPackage().getName();
MatcherAssert.assertThat(PackageElementWrapper.getByFqn(fqn).get().getQualifiedName(), Matchers.is(fqn));

} finally {
ToolingProvider.clearTooling();
}

}).executeTest();

}

@Test
public void test_getByFqn_nonExistingPackage() {

CompileTestBuilder.unitTest().<Element>defineTest((processingEnvironment, element) -> {

try {

ToolingProvider.setTooling(processingEnvironment);

String fqn = "xyz.xyz.xyz";
MatcherAssert.assertThat("Package must not be found", !PackageElementWrapper.getByFqn(fqn).isPresent());

} finally {
ToolingProvider.clearTooling();
}

}).executeTest();

}

@Test
public void test_isUnnamed() {
PackageElement packageElement = Mockito.mock(PackageElement.class);
Expand Down

0 comments on commit b65428e

Please sign in to comment.