From dae37137e9e70a1c7c92318809437403636065f2 Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Thu, 6 Jun 2024 12:16:29 +0200 Subject: [PATCH] Code cleanup --- .../github/joschi/jadconfig/JadConfig.java | 35 +++++++++---------- .../jadconfig/LazyValidationException.java | 10 ++---- .../response/ProcessingResponse.java | 14 ++------ 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/github/joschi/jadconfig/JadConfig.java b/src/main/java/com/github/joschi/jadconfig/JadConfig.java index 6bd0a19..54ca7ff 100644 --- a/src/main/java/com/github/joschi/jadconfig/JadConfig.java +++ b/src/main/java/com/github/joschi/jadconfig/JadConfig.java @@ -2,6 +2,7 @@ import com.github.joschi.jadconfig.converters.NoConverter; import com.github.joschi.jadconfig.converters.StringConverter; +import com.github.joschi.jadconfig.response.ProcessingOutcome; import com.github.joschi.jadconfig.response.ProcessingResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,8 +20,8 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; -import static com.github.joschi.jadconfig.ReflectionUtils.*; /** * The main class for JadConfig. It's responsible for parsing the configuration bean(s) that contain(s) the annotated @@ -99,8 +100,8 @@ public void process() throws RepositoryException, ValidationException { for (Object configurationBean : configurationBeans) { LOG.debug("Processing configuration bean {}", configurationBean); - processClassFields(configurationBean, getAllFields(configurationBean.getClass())); - invokeValidatorMethods(configurationBean, getAllMethods(configurationBean.getClass())); + processClassFields(configurationBean, ReflectionUtils.getAllFields(configurationBean.getClass())); + invokeValidatorMethods(configurationBean, ReflectionUtils.getAllMethods(configurationBean.getClass())); } } @@ -119,23 +120,21 @@ public void processFailingLazily() throws RepositoryException, LazyValidationExc } ProcessingResponse doProcessFailingLazily() throws RepositoryException { - ProcessingResponse response = new ProcessingResponse(); - for (Repository repository : repositories) { LOG.debug("Opening repository {}", repository); repository.open(); } - for (Object configurationBean : configurationBeans) { - LOG.debug("Processing configuration bean {}", configurationBean); + return configurationBeans.stream() + .peek(bean -> LOG.debug("Processing configuration bean {}", bean)) + .map(this::processBean) + .collect(Collectors.collectingAndThen(Collectors.toList(), ProcessingResponse::new)); + } - response.addOutcome( - configurationBean, - processClassFieldsFailingLazily(configurationBean, getAllFields(configurationBean.getClass())), - invokeValidatorMethodsFailingLazily(configurationBean, getAllMethods(configurationBean.getClass())) - ); - } - return response; + private ProcessingOutcome processBean(Object bean) { + final Map fieldProcessingProblems = processClassFieldsFailingLazily(bean, ReflectionUtils.getAllFields(bean.getClass())); + final Map validationMethodsProblems = invokeValidatorMethodsFailingLazily(bean, ReflectionUtils.getAllMethods(bean.getClass())); + return new ProcessingOutcome(bean, fieldProcessingProblems, validationMethodsProblems); } @@ -146,7 +145,7 @@ private void processClassFields(Object configurationBean, Field[] fields) throws } private Map processClassFieldsFailingLazily(Object configurationBean, Field[] fields) { - Map fieldProcessingProblems = new HashMap<>(); + final Map fieldProcessingProblems = new HashMap<>(); for (Field field : fields) { try { processClassField(configurationBean, field); @@ -281,7 +280,7 @@ private void validateParameter(Collection>> validat private void invokeValidatorMethods(Object configurationBean, Method[] methods) throws ValidationException { try { - invokeMethodsWithAnnotation(configurationBean, ValidatorMethod.class, methods); + ReflectionUtils.invokeMethodsWithAnnotation(configurationBean, ValidatorMethod.class, methods); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof ValidationException) { throw (ValidationException)e.getTargetException(); @@ -294,7 +293,7 @@ private void invokeValidatorMethods(Object configurationBean, Method[] methods) } private Map invokeValidatorMethodsFailingLazily(Object configurationBean, Method[] methods) { - Map problems = new HashMap<>(); + final Map problems = new HashMap<>(); for (Method method : methods) { if (method.isAnnotationPresent(ValidatorMethod.class)) { @@ -364,7 +363,7 @@ public Map dump() { final Map configurationDump = new HashMap(); for (Object configurationBean : configurationBeans) { - for (Field field : getAllFields(configurationBean.getClass())) { + for (Field field : ReflectionUtils.getAllFields(configurationBean.getClass())) { final Parameter parameter = field.getAnnotation(Parameter.class); if (parameter != null) { diff --git a/src/main/java/com/github/joschi/jadconfig/LazyValidationException.java b/src/main/java/com/github/joschi/jadconfig/LazyValidationException.java index 146655b..0acb777 100644 --- a/src/main/java/com/github/joschi/jadconfig/LazyValidationException.java +++ b/src/main/java/com/github/joschi/jadconfig/LazyValidationException.java @@ -21,18 +21,12 @@ private static String toMessage(ProcessingResponse result) { result.getOutcomes().stream() .filter(ProcessingOutcome::hasProblems) .flatMap(processingOutcome -> Stream.concat( - processingOutcome.getFieldProcessingProblems().values().stream().map(e -> toMessage(processingOutcome, e)), - processingOutcome.getValidationMethodsProblems().values().stream().map(e -> toMessage(processingOutcome, e)) + processingOutcome.getFieldProcessingProblems().values().stream().map(Throwable::getMessage), + processingOutcome.getValidationMethodsProblems().values().stream().map(Throwable::getMessage) )).forEach(stringBuilder::add); return String.join("\n", stringBuilder); } - private static String toMessage(ProcessingOutcome processingOutcome, Exception exception) { - // TODO: should we distinct between field processing problem and validation method? - // TODO: should we include class name of the bean or not? - return exception.getMessage(); - } - public ProcessingResponse getProcessingResponse() { return processingResponse; } diff --git a/src/main/java/com/github/joschi/jadconfig/response/ProcessingResponse.java b/src/main/java/com/github/joschi/jadconfig/response/ProcessingResponse.java index f80c47a..e6cbe2e 100644 --- a/src/main/java/com/github/joschi/jadconfig/response/ProcessingResponse.java +++ b/src/main/java/com/github/joschi/jadconfig/response/ProcessingResponse.java @@ -1,21 +1,13 @@ package com.github.joschi.jadconfig.response; -import java.util.LinkedList; import java.util.List; -import java.util.Map; public class ProcessingResponse { - private List outcomes; + private final List outcomes; - public ProcessingResponse() { - this.outcomes = new LinkedList<>(); - } - - public void addOutcome(final Object configurationBean, - final Map fieldProcessingProblems, - final Map validationMethodsProblems) { - outcomes.add(new ProcessingOutcome(configurationBean, fieldProcessingProblems, validationMethodsProblems)); + public ProcessingResponse(List outcomes) { + this.outcomes = outcomes; } public List getOutcomes() {