Add componentGenericType to TypeComponent

dev
Exlll 3 years ago
parent 48aef6a48c
commit 7ce9c6b1c6

@ -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<T extends AnnotatedElement> {
*/
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<T extends AnnotatedElement> {
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<T extends AnnotatedElement> {
return component.getType();
}
@Override
public Type componentGenericType() {
return component.getGenericType();
}
@Override
public Object componentValue(Object componentHolder) {
return Reflect.getValue(component, componentHolder);

@ -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<String> 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<Integer> 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

Loading…
Cancel
Save