Ao invés de usar uma classe para representar meu PersonDTO, gostaria de usar um record, porém não estou conseguindo fazer isso. Como posso fazer essa alteração?
package br.com.alura.refl;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
public class DTOConverter {
public <I, O> O transform(I input, Class<O> target) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<?> source = input.getClass();
O instanceOfTarget = target.getDeclaredConstructor().newInstance();
Field[] sourceFields = source.getDeclaredFields();
Field[] targetFields = target.getDeclaredFields();
Arrays.stream(sourceFields)
.forEach(sourceField ->
Arrays.stream(targetFields)
.forEach(targetField -> {
validate(sourceField, targetField);
try {
targetField.set(instanceOfTarget, sourceField.get(input));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
)
);
return instanceOfTarget;
}
private void validate(Field sourceField, Field targetField) {
if (sourceField.getName().equals(targetField.getName()) && sourceField.getType().equals(targetField.getType())) {
sourceField.setAccessible(true);
targetField.setAccessible(true);
}
}
}
package br.com.alura.model;
public record PersonDTO(String name, String cpf) {
}