forked from bmatthias/config-builder
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract any enum type parameters correctly
Signed-off-by: Daniel Kraschewski <daniel.kraschewski@tngtech.com>
- Loading branch information
Daniel Kraschewski
committed
Nov 28, 2020
1 parent
772c171
commit 68fb6ad
Showing
6 changed files
with
103 additions
and
20 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/tngtech/configbuilder/util/EnumTypeExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.tngtech.configbuilder.util; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Arrays.stream; | ||
|
||
public class EnumTypeExtractor { | ||
|
||
Stream<Class<? extends Enum<?>>> getEnumTypesRelevantFor(Type type) { | ||
if (type instanceof Class && ((Class<?>) type).isEnum()) { | ||
return Stream.of((Class<? extends Enum<?>>) type); | ||
} | ||
if (type instanceof ParameterizedType) { | ||
ParameterizedType parameterizedType = (ParameterizedType) type; | ||
return stream(parameterizedType.getActualTypeArguments()) | ||
.flatMap(this::getEnumTypesRelevantFor) | ||
.distinct(); | ||
} | ||
return Stream.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/java/com/tngtech/configbuilder/util/EnumTypeExtractorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.tngtech.configbuilder.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class EnumTypeExtractorTest { | ||
|
||
enum TestEnum1 {} | ||
|
||
enum TestEnum2 {} | ||
|
||
public static class TestClass { | ||
|
||
String simpleNonEnumField; | ||
Map<Integer, String> nonEnumParameterizedField; | ||
|
||
TestEnum1 simpleEnumField; | ||
Map<TestEnum1, TestEnum2> fieldWithEnumTypeParameters; | ||
Map<TestEnum1, List<TestEnum2>> fieldWithNestedEnumTypeParameters; | ||
Map<TestEnum1, Map<TestEnum1, TestEnum1>> fieldWithRepeatedEnumTypeParameter; | ||
} | ||
|
||
private final EnumTypeExtractor enumTypeExtractor = new EnumTypeExtractor(); | ||
|
||
@Test | ||
public void testExtractionOnSimpleNonEnumType() throws Exception { | ||
Field simpleNonEnumField = TestClass.class.getDeclaredField("simpleNonEnumField"); | ||
assertThat(enumTypeExtractor.getEnumTypesRelevantFor(simpleNonEnumField.getGenericType())) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testExtractionOnNonEnumParameterizedType() throws Exception { | ||
Field nonEnumParameterizedField = TestClass.class.getDeclaredField("nonEnumParameterizedField"); | ||
assertThat(enumTypeExtractor.getEnumTypesRelevantFor(nonEnumParameterizedField.getGenericType())) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testExtractionOfSimpleEnumType() throws Exception { | ||
Field simpleEnumField = TestClass.class.getDeclaredField("simpleEnumField"); | ||
assertThat(enumTypeExtractor.getEnumTypesRelevantFor(simpleEnumField.getGenericType())) | ||
.containsExactly(TestEnum1.class); | ||
} | ||
|
||
@Test | ||
public void testExtractionOfEnumTypeParameters() throws Exception { | ||
Field fieldWithEnumTypeParameters = TestClass.class.getDeclaredField("fieldWithEnumTypeParameters"); | ||
assertThat(enumTypeExtractor.getEnumTypesRelevantFor(fieldWithEnumTypeParameters.getGenericType())) | ||
.containsExactlyInAnyOrder(TestEnum1.class, TestEnum2.class); | ||
} | ||
|
||
@Test | ||
public void testExtractionOfNestedEnumTypeParameters() throws Exception { | ||
Field fieldWithNestedEnumTypeParameters = TestClass.class.getDeclaredField("fieldWithNestedEnumTypeParameters"); | ||
assertThat(enumTypeExtractor.getEnumTypesRelevantFor(fieldWithNestedEnumTypeParameters.getGenericType())) | ||
.containsExactlyInAnyOrder(TestEnum1.class, TestEnum2.class); | ||
} | ||
|
||
@Test | ||
public void testExtractedEnumTypesAreDeduplicated() throws Exception { | ||
Field fieldWithRepeatedEnumTypeParameter = TestClass.class.getDeclaredField("fieldWithRepeatedEnumTypeParameter"); | ||
assertThat(enumTypeExtractor.getEnumTypesRelevantFor(fieldWithRepeatedEnumTypeParameter.getGenericType())) | ||
.containsExactly(TestEnum1.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters