klcodanr closed pull request #7: create MissingElementsException only if there is at least one missing element URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/7
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java index 3326ac2..874877c 100644 --- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java +++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java @@ -595,11 +595,14 @@ RuntimeException injectElement(final InjectableElement element, final Object ada final Map<ValuePreparer, Object> preparedValues = new HashMap<>(VALUE_PREPARERS_COUNT); - MissingElementsException missingElements = new MissingElementsException("Could not create all mandatory methods for interface of model " + modelClass); + List<MissingElementException> missingElements = null; for (InjectableMethod method : injectableMethods) { RuntimeException t = injectElement(method, adaptable, registry, callback, preparedValues); if (t != null) { - missingElements.addMissingElementExceptions(new MissingElementException(method.getAnnotatedElement(), t)); + if (missingElements == null) { + missingElements = new ArrayList<>(); + } + missingElements.add(new MissingElementException(method.getAnnotatedElement(), t)); } } @@ -613,8 +616,12 @@ RuntimeException injectElement(final InjectableElement element, final Object ada registerCallbackRegistry(handler, registry); } } - if (!missingElements.isEmpty()) { - return new Result<>(missingElements); + if (missingElements != null) { + MissingElementsException missingElementsException = new MissingElementsException("Could not create all mandatory methods for interface of model " + modelClass); + for (MissingElementException me : missingElements) { + missingElementsException.addMissingElementExceptions(me); + } + return new Result<>(missingElementsException); } return new Result<InvocationHandler>(handler); } @@ -677,12 +684,15 @@ private static ServletRequest unwrapRequest(ServletRequest request) { InjectCallback callback = new SetFieldCallback(object); InjectableField[] injectableFields = modelClass.getInjectableFields(); - MissingElementsException missingElements = new MissingElementsException("Could not inject all required fields into " + modelClass.getType()); + List<MissingElementException> missingElements = null; for (InjectableField field : injectableFields) { RuntimeException t = injectElement(field, adaptable, registry, callback, preparedValues); if (t != null) { - missingElements.addMissingElementExceptions(new MissingElementException(field.getAnnotatedElement(), t)); + if (missingElements == null) { + missingElements = new ArrayList<>(); + } + missingElements.add(new MissingElementException(field.getAnnotatedElement(), t)); } } @@ -697,8 +707,12 @@ private static ServletRequest unwrapRequest(ServletRequest request) { } } - if (!missingElements.isEmpty()) { - return new Result<>(missingElements); + if (missingElements != null) { + MissingElementsException missingElementsException = new MissingElementsException("Could not inject all required fields into " + modelClass.getType()); + for (MissingElementException me : missingElements) { + missingElementsException.addMissingElementExceptions(me); + } + return new Result<>(missingElementsException); } try { object = invokePostConstruct(object); @@ -750,15 +764,22 @@ private static ServletRequest unwrapRequest(ServletRequest request) { List<Object> paramValues = new ArrayList<>(Arrays.asList(new Object[parameters.length])); InjectCallback callback = new SetConstructorParameterCallback(paramValues); - MissingElementsException missingElements = new MissingElementsException("Required constructor parameters were not able to be injected on model " + modelClass.getType()); + List<MissingElementException> missingElements = null; for (int i = 0; i < parameters.length; i++) { RuntimeException t = injectElement(parameters[i], adaptable, registry, callback, preparedValues); if (t != null) { - missingElements.addMissingElementExceptions(new MissingElementException(parameters[i].getAnnotatedElement(), t)); + if (missingElements == null) { + missingElements = new ArrayList<>(); + } + missingElements.add(new MissingElementException(parameters[i].getAnnotatedElement(), t)); } } - if (!missingElements.isEmpty()) { - return new Result<>(missingElements); + if (missingElements != null) { + MissingElementsException missingElementsException = new MissingElementsException("Required constructor parameters were not able to be injected on model " + modelClass.getType()); + for (MissingElementException me : missingElements) { + missingElementsException.addMissingElementExceptions(me); + } + return new Result<>(missingElementsException); } return new Result<>(constructor.getConstructor().newInstance(paramValues.toArray(new Object[paramValues.size()]))); } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services