Skip to content

Commit

Permalink
[#148] support records and modules via reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Apr 9, 2024
1 parent f7e45bc commit 86dfa0a
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 157 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
public class Java16Tests {

@PassIn
record MyRecord(String name, String surname){}
record MyRecord(String name, String surname) {
}

@Test
public void test_records_isRecord() {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest( (processingEnvironment, element) -> {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest((processingEnvironment, element) -> {
try {
ToolingProvider.setTooling(processingEnvironment);

Expand All @@ -38,7 +39,7 @@ public void test_records_isRecord() {

@Test
public void test_records_isTypeElement() {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest( (processingEnvironment, element) -> {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest((processingEnvironment, element) -> {
try {
ToolingProvider.setTooling(processingEnvironment);

Expand All @@ -61,7 +62,7 @@ public void test_records_isTypeElement() {

@Test
public void test_recordComponent_isRecordComponent() {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest( (processingEnvironment, element) -> {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest((processingEnvironment, element) -> {
try {
ToolingProvider.setTooling(processingEnvironment);

Expand All @@ -82,7 +83,7 @@ public void test_recordComponent_isRecordComponent() {

@Test
public void test_recordComponent_isTypeElement() {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest( (processingEnvironment, element) -> {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest((processingEnvironment, element) -> {
try {
ToolingProvider.setTooling(processingEnvironment);

Expand All @@ -107,13 +108,13 @@ public void test_recordComponent_isTypeElement() {


public void test_recordComponent_simpleName() {
Cute.unitTest().when().passInElement().fromClass(MyRecord.class).intoUnitTest( (processingEnvironment, element) -> {
Cute.unitTest().when().passInElement().<TypeElement>fromClass(MyRecord.class).intoUnitTest((processingEnvironment, element) -> {
try {
ToolingProvider.setTooling(processingEnvironment);
TypeElement typeElement = (TypeElement) element;
TypeElementWrapper typeElement = TypeElementWrapper.wrap(element);

RecordComponentElementWrapper elementWrapper = RecordComponentElementWrapper.wrap(typeElement.getRecordComponents().get(0));
MatcherAssert.assertThat( elementWrapper.getSimpleName(), Matchers.is("name"));
RecordComponentElementWrapper elementWrapper = typeElement.getRecordComponents().get(0);
MatcherAssert.assertThat(elementWrapper.getSimpleName(), Matchers.is("name"));

} finally {
ToolingProvider.clearTooling();
Expand All @@ -125,14 +126,14 @@ public void test_recordComponent_simpleName() {

@Test
public void test_recordComponent_filtering_byTypeElement() {
Cute.unitTest().when( (processingEnvironment) -> {
Cute.unitTest().when((processingEnvironment) -> {
try {
ToolingProvider.setTooling(processingEnvironment);
TypeElementWrapper typeElement = TypeElementWrapper.getByClass(Java16Tests.class).get();

Set<String> enclosedTypeElements = typeElement.filterFlattenedEnclosedElementTree().applyFilter(AptkCoreMatchers.IS_TYPE_ELEMENT).getResult().stream().map(e -> e.getQualifiedName().toString()).collect(Collectors.toSet());
Set<String> enclosedTypeElements = typeElement.filterFlattenedEnclosedElementTree().applyFilter(AptkCoreMatchers.IS_TYPE_ELEMENT).getResult().stream().map(e -> e.getQualifiedName().toString()).collect(Collectors.toSet());

MatcherAssert.assertThat( enclosedTypeElements, Matchers.contains(MyRecord.class.getCanonicalName()));
MatcherAssert.assertThat(enclosedTypeElements, Matchers.contains(MyRecord.class.getCanonicalName()));

} finally {
ToolingProvider.clearTooling();
Expand All @@ -144,15 +145,15 @@ public void test_recordComponent_filtering_byTypeElement() {

@Test
public void test_recordComponent_filtering_byRecord() {
Cute.unitTest().when( (processingEnvironment) -> {
Cute.unitTest().when((processingEnvironment) -> {
try {
ToolingProvider.setTooling(processingEnvironment);
TypeElementWrapper typeElement = TypeElementWrapper.getByClass(Java16Tests.class).get();

Set<String> enclosedTypeElements = typeElement.filterFlattenedEnclosedElementTree().applyFilter(AptkCoreMatchers.IS_RECORD).getResult().stream().map(e -> e.getQualifiedName().toString()).collect(Collectors.toSet());
Set<String> enclosedTypeElements = typeElement.filterFlattenedEnclosedElementTree().applyFilter(AptkCoreMatchers.IS_RECORD).getResult().stream().map(e -> e.getQualifiedName().toString()).collect(Collectors.toSet());

MatcherAssert.assertThat(enclosedTypeElements, Matchers.hasSize(1));
MatcherAssert.assertThat( enclosedTypeElements, Matchers.contains(MyRecord.class.getCanonicalName()));
MatcherAssert.assertThat(enclosedTypeElements, Matchers.contains(MyRecord.class.getCanonicalName()));

} finally {
ToolingProvider.clearTooling();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;
import org.mockito.Mockito;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.Name;
Expand All @@ -18,31 +19,39 @@
public class ModuleElementWrapperTest {




@Test
public void testCreationOfWrapperAndUnwrap() {
ModuleElement moduleElement = Mockito.mock(ModuleElement.class);
ModuleElementWrapper unit = ModuleElementWrapper.wrap(moduleElement);
Mockito.when(moduleElement.getKind()).thenReturn(ElementKind.MODULE);

ModuleElementWrapper unit = ModuleElementWrapper.wrap((Element)moduleElement);
MatcherAssert.assertThat(unit, Matchers.notNullValue());
MatcherAssert.assertThat(unit.unwrap(), Matchers.is(moduleElement));
}

@Test
public void test_getQualifiedName() {
ModuleElement moduleElement = Mockito.mock(ModuleElement.class);
Mockito.when(moduleElement.getKind()).thenReturn(ElementKind.MODULE);

Name name = Mockito.mock(Name.class);
Mockito.when(name.toString()).thenReturn("JUPP");
Mockito.when(moduleElement.getQualifiedName()).thenReturn(name);
ModuleElementWrapper unit = ModuleElementWrapper.wrap(moduleElement);
ModuleElementWrapper unit = ModuleElementWrapper.wrap((Element)moduleElement);
MatcherAssert.assertThat(unit.getQualifiedName(), Matchers.is("JUPP"));
}

@Test
public void test_hasQualifiedName() {
ModuleElement moduleElement = Mockito.mock(ModuleElement.class);
Mockito.when(moduleElement.getKind()).thenReturn(ElementKind.MODULE);

Name name = Mockito.mock(Name.class);
Mockito.when(name.toString()).thenReturn("YES");
Mockito.when(moduleElement.getQualifiedName()).thenReturn(name);
ModuleElementWrapper unit = ModuleElementWrapper.wrap(moduleElement);
ModuleElementWrapper unit = ModuleElementWrapper.wrap((Element)moduleElement);
MatcherAssert.assertThat(unit.hasQualifiedName("YES"), Matchers.is(true));
MatcherAssert.assertThat(unit.hasQualifiedName("NO"), Matchers.is(false));
MatcherAssert.assertThat(unit.hasQualifiedName(null), Matchers.is(false));
Expand All @@ -51,7 +60,9 @@ public void test_hasQualifiedName() {
@Test
public void proxyTests_isOpen() {
ModuleElement moduleElement = Mockito.spy(ModuleElement.class);
ModuleElementWrapper unit = ModuleElementWrapper.wrap(moduleElement);
Mockito.when(moduleElement.getKind()).thenReturn(ElementKind.MODULE);

ModuleElementWrapper unit = ModuleElementWrapper.wrap((Element)moduleElement);

unit.isOpen();
Mockito.verify(moduleElement, Mockito.times(1)).isOpen();
Expand All @@ -61,28 +72,33 @@ public void proxyTests_isOpen() {
@Test
public void proxyTests_isUnnamed() {
ModuleElement moduleElement = Mockito.spy(ModuleElement.class);
ModuleElementWrapper unit = ModuleElementWrapper.wrap(moduleElement);
Mockito.when(moduleElement.getKind()).thenReturn(ElementKind.MODULE);

ModuleElementWrapper unit = ModuleElementWrapper.wrap((Element)moduleElement);

unit.isUnnamed();
Mockito.verify(moduleElement, Mockito.times(1)).isUnnamed();

}

/*-
// TODO: MUST IMPLEMENT DIRECTIVES VIA REFLECTION
@Test
public void proxyTests_getDirectives() {
ModuleElement moduleElement = Mockito.spy(ModuleElement.class);
ModuleElementWrapper unit = ModuleElementWrapper.wrap(moduleElement);
ModuleElementWrapper unit = ModuleElementWrapper.wrap((Element)moduleElement);
unit.getDirectives();
Mockito.verify(moduleElement, Mockito.times(1)).getDirectives();
}
*/

@Test
public void test_isModuleElement() {
ModuleElement moduleElement = Mockito.mock(ModuleElement.class);

Mockito.when(moduleElement.getKind()).thenReturn(ElementKind.MODULE);

MatcherAssert.assertThat(ElementWrapper.wrap(moduleElement).isModule(), Matchers.is(true));


Expand All @@ -91,21 +107,5 @@ public void test_isModuleElement() {
MatcherAssert.assertThat(ElementWrapper.wrap(typeElement).isModuleElement(), Matchers.is(false));

}
/*-
public static boolean isModuleElement(Element element) {
return element != null & element.getKind().equals(ElementKind.MODULE);
}
public static ModuleElementWrapperTest toModuleElement(Element element) {
return ModuleElementWrapperTest.wrap((ModuleElement) element);
}
public static ModuleElementWrapperTest wrap(ModuleElement moduleElement) {
return new ModuleElementWrapperTest(moduleElement);
}
*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.lang.model.element.TypeElement;
import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -746,4 +747,12 @@ public static ExecutableElementWrapper toExecutableElementWrapper(ElementWrapper
return ExecutableElementWrapper.wrap(ElementUtils.CastElement.castToExecutableElement(wrapper.unwrap()));
}

protected <TARGET_TYPE> TARGET_TYPE invokeParameterlessMethodOfElement(String methodName, TARGET_TYPE defaultReturnValue) {
try {
return (TARGET_TYPE) this.element.getClass().getMethod(methodName).invoke(element);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
return defaultReturnValue;
}
}

}
Loading

0 comments on commit 86dfa0a

Please sign in to comment.