Refactor TypeComponent

dev
Exlll 3 years ago
parent 7ce9c6b1c6
commit 3c25327f7f

@ -44,12 +44,12 @@ final class CommentNodeExtractor {
while (state.iterator.hasNext()) {
final var component = state.iterator.next();
final var componentValue = component.componentValue(state.componentHolder);
final var componentValue = component.value(state.componentHolder);
if ((componentValue == null) && !outputNull)
continue;
final var componentName = component.componentName();
final var componentName = component.name();
final var commentNode = createNodeIfCommentPresent(
component.component(),
componentName,
@ -57,7 +57,7 @@ final class CommentNodeExtractor {
);
commentNode.ifPresent(result::add);
final var componentType = component.componentType();
final var componentType = component.type();
if ((componentValue != null) &&
(Reflect.isConfiguration(componentType) ||
componentType.isRecord())) {

@ -15,7 +15,7 @@ final class ConfigurationSerializer<T> extends TypeSerializer<T, ConfigurationFi
final T result = Reflect.callNoParamConstructor(type);
for (final var component : components()) {
final var formattedName = formatter.format(component.componentName());
final var formattedName = formatter.format(component.name());
if (!element.containsKey(formattedName))
continue;

@ -20,10 +20,10 @@ final class RecordSerializer<R> extends
for (int i = 0, size = components.size(); i < size; i++) {
final var component = components.get(i);
final var formattedName = formatter.format(component.componentName());
final var formattedName = formatter.format(component.name());
if (!element.containsKey(formattedName)) {
constructorArguments[i] = Reflect.getDefaultValue(component.componentType());
constructorArguments[i] = Reflect.getDefaultValue(component.type());
continue;
}
@ -34,7 +34,7 @@ final class RecordSerializer<R> extends
requireNonPrimitiveComponentType(recordComponent);
constructorArguments[i] = null;
} else if (serializedValue == null) {
constructorArguments[i] = Reflect.getDefaultValue(component.componentType());
constructorArguments[i] = Reflect.getDefaultValue(component.type());
} else {
constructorArguments[i] = deserialize(component, serializedValue);
}

@ -27,21 +27,21 @@ sealed interface TypeComponent<T extends AnnotatedElement> {
*
* @return name of the component
*/
String componentName();
String name();
/**
* Returns the type of the component.
*
* @return type of the component
*/
Class<?> componentType();
Class<?> type();
/**
* Returns the generic type of the component.
*
* @return generic type of the component
*/
Type componentGenericType();
Type genericType();
/**
* Returns the value the component is holding.
@ -51,7 +51,7 @@ sealed interface TypeComponent<T extends AnnotatedElement> {
* @throws IllegalArgumentException if {@code componentHolder} is not an instance of the type to
* which this component belongs
*/
Object componentValue(Object componentHolder);
Object value(Object componentHolder);
/**
* Returns the type that declares this component.
@ -79,22 +79,22 @@ sealed interface TypeComponent<T extends AnnotatedElement> {
}
@Override
public String componentName() {
public String name() {
return component.getName();
}
@Override
public Class<?> componentType() {
public Class<?> type() {
return component.getType();
}
@Override
public Type componentGenericType() {
public Type genericType() {
return component.getGenericType();
}
@Override
public Object componentValue(Object componentHolder) {
public Object value(Object componentHolder) {
return Reflect.getValue(component, componentHolder);
}
@ -111,22 +111,22 @@ sealed interface TypeComponent<T extends AnnotatedElement> {
}
@Override
public String componentName() {
public String name() {
return component.getName();
}
@Override
public Class<?> componentType() {
public Class<?> type() {
return component.getType();
}
@Override
public Type componentGenericType() {
public Type genericType() {
return component.getGenericType();
}
@Override
public Object componentValue(Object componentHolder) {
public Object value(Object componentHolder) {
return Reflect.getValue(component, componentHolder);
}

@ -35,13 +35,13 @@ sealed abstract class TypeSerializer<T, TC extends TypeComponent<?>>
final Map<String, Object> result = new LinkedHashMap<>();
for (final TC component : components()) {
final Object componentValue = component.componentValue(element);
final Object componentValue = component.value(element);
if ((componentValue == null) && !properties.outputNulls())
continue;
final Object serializedValue = serialize(component, componentValue);
final String formattedName = formatter.format(component.componentName());
final String formattedName = formatter.format(component.name());
result.put(formattedName, serializedValue);
}
@ -53,7 +53,7 @@ sealed abstract class TypeSerializer<T, TC extends TypeComponent<?>>
// are selected based on the component type.
@SuppressWarnings("unchecked")
final var serializer = (Serializer<Object, Object>)
serializers.get(component.componentName());
serializers.get(component.name());
return (value != null) ? serializer.serialize(value) : null;
}
@ -62,7 +62,7 @@ sealed abstract class TypeSerializer<T, TC extends TypeComponent<?>>
// is deserialized is not a subtype of the type the deserializer expects.
@SuppressWarnings("unchecked")
final var serializer = (Serializer<Object, Object>)
serializers.get(component.componentName());
serializers.get(component.name());
final Object deserialized;
try {

@ -27,24 +27,24 @@ class TypeComponentTest {
@Test
void componentName() {
assertThat(COMPONENT.componentName(), is("field"));
assertThat(COMPONENT.name(), is("field"));
}
@Test
void componentType() {
assertThat(COMPONENT.componentType(), equalTo(List.class));
assertThat(COMPONENT.type(), equalTo(List.class));
}
@Test
void componentGenericType() {
ParameterizedType type = (ParameterizedType) COMPONENT.componentGenericType();
ParameterizedType type = (ParameterizedType) COMPONENT.genericType();
Type argument = type.getActualTypeArguments()[0];
assertThat(argument, equalTo(String.class));
}
@Test
void componentValue() {
assertThat(COMPONENT.componentValue(new C()), is(List.of("20")));
assertThat(COMPONENT.value(new C()), is(List.of("20")));
}
@Test
@ -67,24 +67,24 @@ class TypeComponentTest {
@Test
void componentName() {
assertThat(COMPONENT.componentName(), is("comp"));
assertThat(COMPONENT.name(), is("comp"));
}
@Test
void componentType() {
assertThat(COMPONENT.componentType(), equalTo(Set.class));
assertThat(COMPONENT.type(), equalTo(Set.class));
}
@Test
void componentGenericType() {
ParameterizedType type = (ParameterizedType) COMPONENT.componentGenericType();
ParameterizedType type = (ParameterizedType) COMPONENT.genericType();
Type argument = type.getActualTypeArguments()[0];
assertThat(argument, equalTo(Integer.class));
}
@Test
void componentValue() {
assertThat(COMPONENT.componentValue(new R(Set.of(1))), is(Set.of(1)));
assertThat(COMPONENT.value(new R(Set.of(1))), is(Set.of(1)));
}
@Test

Loading…
Cancel
Save