Let generic type of TypeComponent extend AnnotatedElement

dev
Exlll 2 years ago
parent 0f3656a21b
commit 48aef6a48c

@ -7,6 +7,7 @@ import java.lang.reflect.AnnotatedElement;
import java.util.*;
import static de.exlll.configlib.Validator.requireConfigurationOrRecord;
import static de.exlll.configlib.Validator.requireNonNull;
final class CommentNodeExtractor {
private final FieldFilter fieldFilter;
@ -14,8 +15,8 @@ final class CommentNodeExtractor {
private final boolean outputNull;
CommentNodeExtractor(ConfigurationProperties properties) {
this.fieldFilter = Validator.requireNonNull(properties.getFieldFilter(), "field filter");
this.nameFormatter = Validator.requireNonNull(properties.getNameFormatter(), "name formatter");
this.fieldFilter = requireNonNull(properties.getFieldFilter(), "field filter");
this.nameFormatter = requireNonNull(properties.getNameFormatter(), "name formatter");
this.outputNull = properties.outputNulls();
}
@ -48,33 +49,22 @@ final class CommentNodeExtractor {
if ((componentValue == null) && !outputNull)
continue;
// pattern switch not yet supported in Java 17
final AnnotatedElement element = (component instanceof ConfigurationField cf)
? cf.component()
: (component instanceof ConfigurationRecordComponent crc)
? crc.component()
: null;
// The element cannot be null because we require a configuration or record
// at the beginning of this method.
assert element != null;
final var componentName = component.componentName();
final var commentNode = createNodeIfCommentPresent(
element,
component.component(),
componentName,
elementNameStack
);
commentNode.ifPresent(result::add);
if ((componentValue == null) ||
(!Reflect.isConfiguration(component.componentType()) &&
!component.componentType().isRecord()))
continue;
stateStack.addLast(new State(state.iterator, state.componentHolder));
elementNameStack.addLast(nameFormatter.format(componentName));
state = stateFromObject(componentValue);
final var componentType = component.componentType();
if ((componentValue != null) &&
(Reflect.isConfiguration(componentType) ||
componentType.isRecord())) {
stateStack.addLast(state);
elementNameStack.addLast(nameFormatter.format(componentName));
state = stateFromObject(componentValue);
}
}
}

@ -1,5 +1,7 @@
package de.exlll.configlib;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.RecordComponent;
@ -11,7 +13,7 @@ import static de.exlll.configlib.Validator.requireNonNull;
*
* @param <T> the type of the component
*/
sealed interface TypeComponent<T> {
sealed interface TypeComponent<T extends AnnotatedElement> {
/**
* Returns the component itself.
*
@ -50,6 +52,19 @@ sealed interface TypeComponent<T> {
*/
Class<?> declaringType();
/**
* Returns the annotation of the given type or null if the component is not annotated
* with such an annotation.
*
* @param annotationType the type of annotation
* @param <A> the type of annotation
* @return the annotation or null
* @throws NullPointerException if {@code annotationType} is null
*/
default <A extends Annotation> A annotation(Class<A> annotationType) {
return component().getAnnotation(annotationType);
}
record ConfigurationField(Field component) implements TypeComponent<Field> {
public ConfigurationField(Field component) {
this.component = requireNonNull(component, "component");
@ -103,4 +118,3 @@ sealed interface TypeComponent<T> {
}
}
}

@ -8,8 +8,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.RecordComponent;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.*;
class TypeComponentTest {
@ -18,6 +17,7 @@ class TypeComponentTest {
private static final ConfigurationField COMPONENT = new ConfigurationField(FIELD);
static final class C {
@Comment("")
int field = 20;
}
@ -40,6 +40,11 @@ class TypeComponentTest {
void declaringType() {
assertThat(COMPONENT.declaringType(), equalTo(C.class));
}
@Test
void annotation() {
assertThat(COMPONENT.annotation(Comment.class), notNullValue());
}
}
static final class ConfigurationRecordComponentTest {
@ -47,7 +52,7 @@ class TypeComponentTest {
private static final ConfigurationRecordComponent COMPONENT =
new ConfigurationRecordComponent(RECORD_COMPONENT);
record R(float comp) {}
record R(@Comment("") float comp) {}
@Test
void componentName() {
@ -68,5 +73,10 @@ class TypeComponentTest {
void declaringType() {
assertThat(COMPONENT.declaringType(), equalTo(R.class));
}
@Test
void annotation() {
assertThat(COMPONENT.annotation(Comment.class), notNullValue());
}
}
}
}

Loading…
Cancel
Save