Skip to content

Commit

Permalink
Add ~.validation.method package
Browse files Browse the repository at this point in the history
Extract classes from ~.validation.beanvalidation without a direct
dependency on beanvalidation.

See gh-30644
  • Loading branch information
rstoyanchev committed Jul 3, 2023
1 parent 0da2241 commit 592ab0f
Show file tree
Hide file tree
Showing 27 changed files with 220 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -50,7 +49,6 @@
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.function.SingletonSupplier;
import org.springframework.validation.BeanPropertyBindingResult;
Expand All @@ -59,6 +57,10 @@
import org.springframework.validation.Errors;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.method.MethodValidationResult;
import org.springframework.validation.method.MethodValidator;
import org.springframework.validation.method.ParameterErrors;
import org.springframework.validation.method.ParameterValidationResult;

/**
* {@link MethodValidator} that uses a Bean Validation
Expand All @@ -70,12 +72,12 @@
*/
public class MethodValidationAdapter implements MethodValidator {

private static final MethodValidationResult emptyValidationResult = MethodValidationResult.emptyResult();

private static final ObjectNameResolver defaultObjectNameResolver = new DefaultObjectNameResolver();

private static final Comparator<ParameterValidationResult> resultComparator = new ResultComparator();

private static final MethodValidationResult emptyResult = new EmptyMethodValidationResult();


private final Supplier<Validator> validator;

Expand Down Expand Up @@ -219,7 +221,7 @@ public final MethodValidationResult validateArguments(
invokeValidatorForArguments(target, method, arguments, groups);

if (violations.isEmpty()) {
return emptyResult;
return emptyValidationResult;
}

return adaptViolations(target, method, violations,
Expand Down Expand Up @@ -257,7 +259,7 @@ public final MethodValidationResult validateReturnValue(
invokeValidatorForReturnValue(target, method, returnValue, groups);

if (violations.isEmpty()) {
return emptyResult;
return emptyValidationResult;
}

return adaptViolations(target, method, violations,
Expand Down Expand Up @@ -320,7 +322,7 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
cascadedViolations.forEach((node, builder) -> validatonResultList.add(builder.build()));
validatonResultList.sort(resultComparator);

return new DefaultMethodValidationResult(target, method, validatonResultList);
return MethodValidationResult.create(target, method, validatonResultList);
}

private MethodParameter initMethodParameter(Method method, int index) {
Expand Down Expand Up @@ -534,91 +536,4 @@ private <E> int compareKeys(ParameterErrors errors1, ParameterErrors errors2) {
}
}


/**
* Default {@link MethodValidationResult} implementation with non-zero errors.
*/
private static class DefaultMethodValidationResult implements MethodValidationResult {

private final Object target;

private final Method method;

private final List<ParameterValidationResult> allValidationResults;

private final boolean forReturnValue;


DefaultMethodValidationResult(Object target, Method method, List<ParameterValidationResult> results) {
Assert.notEmpty(results, "'results' is required and must not be empty");
Assert.notNull(target, "'target' is required");
Assert.notNull(method, "Method is required");
this.target = target;
this.method = method;
this.allValidationResults = results;
this.forReturnValue = (results.get(0).getMethodParameter().getParameterIndex() == -1);
}


@Override
public Object getTarget() {
return this.target;
}

@Override
public Method getMethod() {
return this.method;
}

@Override
public boolean isForReturnValue() {
return this.forReturnValue;
}

@Override
public List<ParameterValidationResult> getAllValidationResults() {
return this.allValidationResults;
}


@Override
public String toString() {
return getAllErrors().size() + " validation errors " +
"for " + (isForReturnValue() ? "return value" : "arguments") + " of " +
this.method.toGenericString();
}
}


/**
* {@link MethodValidationResult} for when there are no errors.
*/
private static class EmptyMethodValidationResult implements MethodValidationResult {

@Override
public Object getTarget() {
throw new UnsupportedOperationException();
}

@Override
public Method getMethod() {
throw new UnsupportedOperationException();
}

@Override
public boolean isForReturnValue() {
throw new UnsupportedOperationException();
}

@Override
public List<ParameterValidationResult> getAllValidationResults() {
return Collections.emptyList();
}

@Override
public String toString() {
return "0 validation errors";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.method.MethodValidationException;
import org.springframework.validation.method.MethodValidationResult;

/**
* An AOP Alliance {@link MethodInterceptor} implementation that delegates to a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.springframework.util.Assert;
import org.springframework.util.function.SingletonSupplier;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.method.MethodValidationException;
import org.springframework.validation.method.MethodValidationResult;

/**
* A convenient {@link BeanPostProcessor} implementation that delegates to a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.validation.method;

import java.lang.reflect.Method;
import java.util.List;

import org.springframework.util.Assert;

/**
* Default {@link MethodValidationResult} implementation as a simple container.
*
* @author Rossen Stoyanchev
* @since 6.1
*/
final class DefaultMethodValidationResult implements MethodValidationResult {

private final Object target;

private final Method method;

private final List<ParameterValidationResult> allValidationResults;

private final boolean forReturnValue;


DefaultMethodValidationResult(Object target, Method method, List<ParameterValidationResult> results) {
Assert.notEmpty(results, "'results' is required and must not be empty");
Assert.notNull(target, "'target' is required");
Assert.notNull(method, "Method is required");
this.target = target;
this.method = method;
this.allValidationResults = results;
this.forReturnValue = (results.get(0).getMethodParameter().getParameterIndex() == -1);
}


@Override
public Object getTarget() {
return this.target;
}

@Override
public Method getMethod() {
return this.method;
}

@Override
public boolean isForReturnValue() {
return this.forReturnValue;
}

@Override
public List<ParameterValidationResult> getAllValidationResults() {
return this.allValidationResults;
}


@Override
public String toString() {
return getAllErrors().size() + " validation errors " +
"for " + (isForReturnValue() ? "return value" : "arguments") + " of " +
this.method.toGenericString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.validation.method;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

/**
* {@link MethodValidationResult} with an empty list of results.
*
* @author Rossen Stoyanchev
* @since 6.1
*/
final class EmptyMethodValidationResult implements MethodValidationResult {

@Override
public Object getTarget() {
throw new UnsupportedOperationException();
}

@Override
public Method getMethod() {
throw new UnsupportedOperationException();
}

@Override
public boolean isForReturnValue() {
throw new UnsupportedOperationException();
}

@Override
public List<ParameterValidationResult> getAllValidationResults() {
return Collections.emptyList();
}

@Override
public String toString() {
return "0 validation errors";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.validation.beanvalidation;
package org.springframework.validation.method;

import java.lang.reflect.Method;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.validation.beanvalidation;
package org.springframework.validation.method;

import java.lang.reflect.Method;
import java.util.List;
Expand Down Expand Up @@ -101,4 +101,25 @@ default List<ParameterErrors> getBeanResults() {
.toList();
}


/**
* Factory method to create a {@link MethodValidationResult} instance.
* @param target the target Object
* @param method the target method
* @param results method validation results, expected to be non-empty
* @return the created instance
*/
static MethodValidationResult create(Object target, Method method, List<ParameterValidationResult> results) {
return new DefaultMethodValidationResult(target, method, results);
}

/**
* Factory method to create a {@link MethodValidationResult} instance with
* 0 errors, suitable to use as a constant. Getters for a target object or
* method are not supported.
*/
static MethodValidationResult emptyResult() {
return new EmptyMethodValidationResult();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.validation.beanvalidation;
package org.springframework.validation.method;

import java.lang.reflect.Method;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.validation.beanvalidation;
package org.springframework.validation.method;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.validation.beanvalidation;
package org.springframework.validation.method;

import java.util.Collection;
import java.util.List;
Expand Down
Loading

0 comments on commit 592ab0f

Please sign in to comment.