From 7ce9c6b1c64ef13152ab8588e9418379622d35ec Mon Sep 17 00:00:00 2001 From: Exlll Date: Sat, 6 Aug 2022 10:28:48 +0200 Subject: [PATCH] Add componentGenericType to TypeComponent --- .../de/exlll/configlib/TypeComponent.java | 18 +++++++++++ .../de/exlll/configlib/TypeComponentTest.java | 30 +++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/configlib-core/src/main/java/de/exlll/configlib/TypeComponent.java b/configlib-core/src/main/java/de/exlll/configlib/TypeComponent.java index a7a4860..e9cc786 100644 --- a/configlib-core/src/main/java/de/exlll/configlib/TypeComponent.java +++ b/configlib-core/src/main/java/de/exlll/configlib/TypeComponent.java @@ -4,6 +4,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Field; import java.lang.reflect.RecordComponent; +import java.lang.reflect.Type; import static de.exlll.configlib.Validator.requireNonNull; @@ -35,6 +36,13 @@ sealed interface TypeComponent { */ Class componentType(); + /** + * Returns the generic type of the component. + * + * @return generic type of the component + */ + Type componentGenericType(); + /** * Returns the value the component is holding. * @@ -80,6 +88,11 @@ sealed interface TypeComponent { return component.getType(); } + @Override + public Type componentGenericType() { + return component.getGenericType(); + } + @Override public Object componentValue(Object componentHolder) { return Reflect.getValue(component, componentHolder); @@ -107,6 +120,11 @@ sealed interface TypeComponent { return component.getType(); } + @Override + public Type componentGenericType() { + return component.getGenericType(); + } + @Override public Object componentValue(Object componentHolder) { return Reflect.getValue(component, componentHolder); diff --git a/configlib-core/src/test/java/de/exlll/configlib/TypeComponentTest.java b/configlib-core/src/test/java/de/exlll/configlib/TypeComponentTest.java index 1c48a19..d8a06d1 100644 --- a/configlib-core/src/test/java/de/exlll/configlib/TypeComponentTest.java +++ b/configlib-core/src/test/java/de/exlll/configlib/TypeComponentTest.java @@ -5,7 +5,11 @@ import de.exlll.configlib.TypeComponent.ConfigurationRecordComponent; import org.junit.jupiter.api.Test; import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.RecordComponent; +import java.lang.reflect.Type; +import java.util.List; +import java.util.Set; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -18,7 +22,7 @@ class TypeComponentTest { static final class C { @Comment("") - int field = 20; + List field = List.of("20"); } @Test @@ -28,12 +32,19 @@ class TypeComponentTest { @Test void componentType() { - assertThat(COMPONENT.componentType(), equalTo(int.class)); + assertThat(COMPONENT.componentType(), equalTo(List.class)); + } + + @Test + void componentGenericType() { + ParameterizedType type = (ParameterizedType) COMPONENT.componentGenericType(); + Type argument = type.getActualTypeArguments()[0]; + assertThat(argument, equalTo(String.class)); } @Test void componentValue() { - assertThat(COMPONENT.componentValue(new C()), is(20)); + assertThat(COMPONENT.componentValue(new C()), is(List.of("20"))); } @Test @@ -52,7 +63,7 @@ class TypeComponentTest { private static final ConfigurationRecordComponent COMPONENT = new ConfigurationRecordComponent(RECORD_COMPONENT); - record R(@Comment("") float comp) {} + record R(@Comment("") Set comp) {} @Test void componentName() { @@ -61,12 +72,19 @@ class TypeComponentTest { @Test void componentType() { - assertThat(COMPONENT.componentType(), equalTo(float.class)); + assertThat(COMPONENT.componentType(), equalTo(Set.class)); + } + + @Test + void componentGenericType() { + ParameterizedType type = (ParameterizedType) COMPONENT.componentGenericType(); + Type argument = type.getActualTypeArguments()[0]; + assertThat(argument, equalTo(Integer.class)); } @Test void componentValue() { - assertThat(COMPONENT.componentValue(new R(10f)), is(10f)); + assertThat(COMPONENT.componentValue(new R(Set.of(1))), is(Set.of(1))); } @Test