Refactor TypeComponent

dev
Exlll 3 years ago
parent 7ce9c6b1c6
commit 3c25327f7f

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

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

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

@ -27,21 +27,21 @@ sealed interface TypeComponent<T extends AnnotatedElement> {
* *
* @return name of the component * @return name of the component
*/ */
String componentName(); String name();
/** /**
* Returns the type of the component. * Returns the type of the component.
* *
* @return type of the component * @return type of the component
*/ */
Class<?> componentType(); Class<?> type();
/** /**
* Returns the generic type of the component. * Returns the generic type of the component.
* *
* @return generic type of the component * @return generic type of the component
*/ */
Type componentGenericType(); Type genericType();
/** /**
* Returns the value the component is holding. * 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 * @throws IllegalArgumentException if {@code componentHolder} is not an instance of the type to
* which this component belongs * which this component belongs
*/ */
Object componentValue(Object componentHolder); Object value(Object componentHolder);
/** /**
* Returns the type that declares this component. * Returns the type that declares this component.
@ -79,22 +79,22 @@ sealed interface TypeComponent<T extends AnnotatedElement> {
} }
@Override @Override
public String componentName() { public String name() {
return component.getName(); return component.getName();
} }
@Override @Override
public Class<?> componentType() { public Class<?> type() {
return component.getType(); return component.getType();
} }
@Override @Override
public Type componentGenericType() { public Type genericType() {
return component.getGenericType(); return component.getGenericType();
} }
@Override @Override
public Object componentValue(Object componentHolder) { public Object value(Object componentHolder) {
return Reflect.getValue(component, componentHolder); return Reflect.getValue(component, componentHolder);
} }
@ -111,22 +111,22 @@ sealed interface TypeComponent<T extends AnnotatedElement> {
} }
@Override @Override
public String componentName() { public String name() {
return component.getName(); return component.getName();
} }
@Override @Override
public Class<?> componentType() { public Class<?> type() {
return component.getType(); return component.getType();
} }
@Override @Override
public Type componentGenericType() { public Type genericType() {
return component.getGenericType(); return component.getGenericType();
} }
@Override @Override
public Object componentValue(Object componentHolder) { public Object value(Object componentHolder) {
return Reflect.getValue(component, 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<>(); final Map<String, Object> result = new LinkedHashMap<>();
for (final TC component : components()) { for (final TC component : components()) {
final Object componentValue = component.componentValue(element); final Object componentValue = component.value(element);
if ((componentValue == null) && !properties.outputNulls()) if ((componentValue == null) && !properties.outputNulls())
continue; continue;
final Object serializedValue = serialize(component, componentValue); final Object serializedValue = serialize(component, componentValue);
final String formattedName = formatter.format(component.componentName()); final String formattedName = formatter.format(component.name());
result.put(formattedName, serializedValue); result.put(formattedName, serializedValue);
} }
@ -53,7 +53,7 @@ sealed abstract class TypeSerializer<T, TC extends TypeComponent<?>>
// are selected based on the component type. // are selected based on the component type.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final var serializer = (Serializer<Object, Object>) final var serializer = (Serializer<Object, Object>)
serializers.get(component.componentName()); serializers.get(component.name());
return (value != null) ? serializer.serialize(value) : null; 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. // is deserialized is not a subtype of the type the deserializer expects.
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final var serializer = (Serializer<Object, Object>) final var serializer = (Serializer<Object, Object>)
serializers.get(component.componentName()); serializers.get(component.name());
final Object deserialized; final Object deserialized;
try { try {

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

Loading…
Cancel
Save