Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
todvora committed Jun 6, 2024
1 parent 3ab4f76 commit dae3713
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 37 deletions.
35 changes: 17 additions & 18 deletions src/main/java/com/github/joschi/jadconfig/JadConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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()));
}
}

Expand All @@ -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<String, Exception> fieldProcessingProblems = processClassFieldsFailingLazily(bean, ReflectionUtils.getAllFields(bean.getClass()));
final Map<String, Exception> validationMethodsProblems = invokeValidatorMethodsFailingLazily(bean, ReflectionUtils.getAllMethods(bean.getClass()));
return new ProcessingOutcome(bean, fieldProcessingProblems, validationMethodsProblems);
}


Expand All @@ -146,7 +145,7 @@ private void processClassFields(Object configurationBean, Field[] fields) throws
}

private Map<String, Exception> processClassFieldsFailingLazily(Object configurationBean, Field[] fields) {
Map<String, Exception> fieldProcessingProblems = new HashMap<>();
final Map<String, Exception> fieldProcessingProblems = new HashMap<>();
for (Field field : fields) {
try {
processClassField(configurationBean, field);
Expand Down Expand Up @@ -281,7 +280,7 @@ private void validateParameter(Collection<Class<? extends Validator<?>>> 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();
Expand All @@ -294,7 +293,7 @@ private void invokeValidatorMethods(Object configurationBean, Method[] methods)
}

private Map<String, Exception> invokeValidatorMethodsFailingLazily(Object configurationBean, Method[] methods) {
Map<String, Exception> problems = new HashMap<>();
final Map<String, Exception> problems = new HashMap<>();

for (Method method : methods) {
if (method.isAnnotationPresent(ValidatorMethod.class)) {
Expand Down Expand Up @@ -364,7 +363,7 @@ public Map<String, String> dump() {
final Map<String, String> configurationDump = new HashMap<String, String>();

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ProcessingOutcome> outcomes;
private final List<ProcessingOutcome> outcomes;

public ProcessingResponse() {
this.outcomes = new LinkedList<>();
}

public void addOutcome(final Object configurationBean,
final Map<String, Exception> fieldProcessingProblems,
final Map<String, Exception> validationMethodsProblems) {
outcomes.add(new ProcessingOutcome(configurationBean, fieldProcessingProblems, validationMethodsProblems));
public ProcessingResponse(List<ProcessingOutcome> outcomes) {
this.outcomes = outcomes;
}

public List<ProcessingOutcome> getOutcomes() {
Expand Down

0 comments on commit dae3713

Please sign in to comment.