From affed2b513297e1b46454e006f04dd664f321fae Mon Sep 17 00:00:00 2001 From: altro3 Date: Sun, 29 Oct 2023 15:53:26 +0700 Subject: [PATCH] Pattern matching for instanceof --- .../validation/customann/EmailSenderTest.java | 3 +- .../validation/tck/DeploymentClassLoader.java | 3 +- .../tck/TckDeployableContainer.java | 6 +- .../validation/ValidatingInterceptor.java | 6 +- .../ConstraintExceptionHandler.java | 6 +- .../DefaultConstraintValidatorContext.java | 2 +- .../validator/DefaultValidator.java | 4 +- .../DefaultValidatorConfiguration.java | 8 +-- .../validation/validator/ValidationPath.java | 1 + .../ConstraintValidatorContext.java | 1 + .../constraints/DigitsValidator.java | 2 +- .../InternalConstraintValidators.java | 60 +++++++++---------- 12 files changed, 51 insertions(+), 51 deletions(-) diff --git a/test-suite/src/test/java/io/micronaut/docs/validation/customann/EmailSenderTest.java b/test-suite/src/test/java/io/micronaut/docs/validation/customann/EmailSenderTest.java index 73bee250..83ddf131 100644 --- a/test-suite/src/test/java/io/micronaut/docs/validation/customann/EmailSenderTest.java +++ b/test-suite/src/test/java/io/micronaut/docs/validation/customann/EmailSenderTest.java @@ -7,7 +7,6 @@ import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; import java.util.Collections; -import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -19,6 +18,6 @@ class EmailSenderTest { void defaultMessageIsUsed(EmailSender emailSender) { Executable e = () -> emailSender.send(new Email("", "")); ConstraintViolationException thrown = assertThrows(ConstraintViolationException.class, e); - assertEquals(Collections.singletonList(EmailMessages.ANY_RECIPIENT_MESSAGE), thrown.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.toList())); + assertEquals(Collections.singletonList(EmailMessages.ANY_RECIPIENT_MESSAGE), thrown.getConstraintViolations().stream().map(ConstraintViolation::getMessage).toList()); } } diff --git a/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/DeploymentClassLoader.java b/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/DeploymentClassLoader.java index 46263873..348c233e 100644 --- a/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/DeploymentClassLoader.java +++ b/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/DeploymentClassLoader.java @@ -26,7 +26,6 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.Stream; @Internal @@ -45,7 +44,7 @@ private static URL[] findUrls(DeploymentDir deploymentDir) throws IOException { result.add(deploymentDir.target.toUri().toURL()); try (Stream stream = Files.walk(deploymentDir.lib)) { - List jars = stream.filter(p -> p.toString().endsWith(".jar")).collect(Collectors.toList()); + List jars = stream.filter(p -> p.toString().endsWith(".jar")).toList(); for (Path jar : jars) { result.add(jar.toUri().toURL()); } diff --git a/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/TckDeployableContainer.java b/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/TckDeployableContainer.java index db0ecab7..ef7045cd 100644 --- a/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/TckDeployableContainer.java +++ b/tests/jakarta-validation-tck/src/main/java/io/micronaut/validation/tck/TckDeployableContainer.java @@ -111,8 +111,8 @@ private static JavaArchive buildSupportLibrary() { @Override public ProtocolMetaData deploy(Archive archive) { - if (archive instanceof LibraryContainer) { - ((LibraryContainer) archive).addAsLibrary(buildSupportLibrary()); + if (archive instanceof LibraryContainer libraryContainer) { + libraryContainer.addAsLibrary(buildSupportLibrary()); } else { throw new IllegalStateException("Expected library container!"); } @@ -120,7 +120,7 @@ public ProtocolMetaData deploy(Archive archive) { if (testClass.get() == null) { throw new IllegalStateException("Test class not available"); } - Class testJavaClass = testClass.get().getJavaClass(); + Class testJavaClass = testClass.get().getJavaClass(); Objects.requireNonNull(testJavaClass); try { diff --git a/validation/src/main/java/io/micronaut/validation/ValidatingInterceptor.java b/validation/src/main/java/io/micronaut/validation/ValidatingInterceptor.java index 3051369d..c645280b 100644 --- a/validation/src/main/java/io/micronaut/validation/ValidatingInterceptor.java +++ b/validation/src/main/java/io/micronaut/validation/ValidatingInterceptor.java @@ -119,18 +119,18 @@ public Object intercept(MethodInvocationContext context) { throw new ConstraintViolationException(constraintViolations); } } - if (micronautValidator instanceof ReactiveValidator) { + if (micronautValidator instanceof ReactiveValidator reactiveValidator) { InterceptedMethod interceptedMethod = InterceptedMethod.of(context, conversionService); try { return switch (interceptedMethod.resultType()) { case PUBLISHER -> interceptedMethod.handleResult( - ((ReactiveValidator) micronautValidator).validatePublisher( + reactiveValidator.validatePublisher( context.getReturnType(), interceptedMethod.interceptResultAsPublisher(), getValidationGroups(context)) ); case COMPLETION_STAGE -> interceptedMethod.handleResult( - ((ReactiveValidator) micronautValidator).validateCompletionStage( + reactiveValidator.validateCompletionStage( (CompletionStage) interceptedMethod.interceptResultAsCompletionStage(), (Argument) context.getReturnType().getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT), getValidationGroups(context)) diff --git a/validation/src/main/java/io/micronaut/validation/exceptions/ConstraintExceptionHandler.java b/validation/src/main/java/io/micronaut/validation/exceptions/ConstraintExceptionHandler.java index c0790f8b..3a197bcd 100644 --- a/validation/src/main/java/io/micronaut/validation/exceptions/ConstraintExceptionHandler.java +++ b/validation/src/main/java/io/micronaut/validation/exceptions/ConstraintExceptionHandler.java @@ -16,6 +16,7 @@ package io.micronaut.validation.exceptions; import io.micronaut.context.annotation.Requires; +import io.micronaut.core.util.CollectionUtils; import io.micronaut.http.HttpRequest; import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpStatus; @@ -33,7 +34,6 @@ import jakarta.validation.Path; import java.util.Iterator; import java.util.Set; -import java.util.stream.Collectors; /** * Default {@link ExceptionHandler} for {@link ConstraintViolationException}. @@ -62,7 +62,7 @@ public HttpResponse handle(HttpRequest request, ConstraintViolationException Set> constraintViolations = exception.getConstraintViolations(); MutableHttpResponse response = HttpResponse.badRequest(); final ErrorContext.Builder contextBuilder = ErrorContext.builder(request).cause(exception); - if (constraintViolations == null || constraintViolations.isEmpty()) { + if (CollectionUtils.isEmpty(constraintViolations)) { return responseProcessor.processResponse(contextBuilder.errorMessage( exception.getMessage() == null ? HttpStatus.BAD_REQUEST.getReason() : exception.getMessage() ).build(), response); @@ -72,7 +72,7 @@ public HttpResponse handle(HttpRequest request, ConstraintViolationException .stream() .map(this::buildMessage) .sorted() - .collect(Collectors.toList()) + .toList() ).build(), response); } } diff --git a/validation/src/main/java/io/micronaut/validation/validator/DefaultConstraintValidatorContext.java b/validation/src/main/java/io/micronaut/validation/validator/DefaultConstraintValidatorContext.java index f1ab6cb8..4c1714ff 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/DefaultConstraintValidatorContext.java +++ b/validation/src/main/java/io/micronaut/validation/validator/DefaultConstraintValidatorContext.java @@ -347,7 +347,7 @@ public ClockProvider getClockProvider() { @Override public ConstraintViolationBuilder buildConstraintViolationWithTemplate(String messageTemplate) { - return new DefaultConstraintViolationBuilder(messageTemplate, this, defaultValidator.messageInterpolator); + return new DefaultConstraintViolationBuilder<>(messageTemplate, this, defaultValidator.messageInterpolator); } @Override diff --git a/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java b/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java index 7fd62250..6ee2d15b 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java +++ b/validation/src/main/java/io/micronaut/validation/validator/DefaultValidator.java @@ -1353,8 +1353,8 @@ private void validateConstrains(DefaultConstraintValidatorContext cont } catch (Exception e) { throw new ValidationException("Cannot initialize validator: " + beanIntrospection.getBeanType().getName()); } - if (constraintValidator instanceof ConstraintValidator) { - validator = (ConstraintValidator) constraintValidator; + if (constraintValidator instanceof ConstraintValidator cv) { + validator = cv; } else { validator = new ConstraintValidator<>() { diff --git a/validation/src/main/java/io/micronaut/validation/validator/DefaultValidatorConfiguration.java b/validation/src/main/java/io/micronaut/validation/validator/DefaultValidatorConfiguration.java index e741dc93..ecc3d7be 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/DefaultValidatorConfiguration.java +++ b/validation/src/main/java/io/micronaut/validation/validator/DefaultValidatorConfiguration.java @@ -454,11 +454,11 @@ private static void determineValueExtractorDefinitions(List value } public static Class getClassFromType(Type type) { - if (type instanceof Class) { - return (Class) type; + if (type instanceof Class classType) { + return classType; } - if (type instanceof ParameterizedType) { - return getClassFromType(((ParameterizedType) type).getRawType()); + if (type instanceof ParameterizedType parameterizedType) { + return getClassFromType(parameterizedType.getRawType()); } if (type instanceof GenericArrayType) { return Object[].class; diff --git a/validation/src/main/java/io/micronaut/validation/validator/ValidationPath.java b/validation/src/main/java/io/micronaut/validation/validator/ValidationPath.java index 5c897646..d7c94c69 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/ValidationPath.java +++ b/validation/src/main/java/io/micronaut/validation/validator/ValidationPath.java @@ -510,6 +510,7 @@ public record DefaultContainerContext(@Nullable Class containerClass, /** * Not in a container context. */ + @SuppressWarnings("StaticVariableName") static DefaultContainerContext NONE = new DefaultContainerContext(null, null, null, false, null); /** diff --git a/validation/src/main/java/io/micronaut/validation/validator/constraints/ConstraintValidatorContext.java b/validation/src/main/java/io/micronaut/validation/validator/constraints/ConstraintValidatorContext.java index f324b655..a5760b93 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/constraints/ConstraintValidatorContext.java +++ b/validation/src/main/java/io/micronaut/validation/validator/constraints/ConstraintValidatorContext.java @@ -39,6 +39,7 @@ public interface ConstraintValidatorContext extends jakarta.validation.Constrain * * @since 2.0 */ + @Override @NonNull ClockProvider getClockProvider(); /** diff --git a/validation/src/main/java/io/micronaut/validation/validator/constraints/DigitsValidator.java b/validation/src/main/java/io/micronaut/validation/validator/constraints/DigitsValidator.java index 979182f5..416e0409 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/constraints/DigitsValidator.java +++ b/validation/src/main/java/io/micronaut/validation/validator/constraints/DigitsValidator.java @@ -56,7 +56,7 @@ default boolean isValid(@Nullable T value, @NonNull AnnotationValue anno } int intLen = bigDecimal.precision() - bigDecimal.scale(); - int fracLen = bigDecimal.scale() < 0 ? 0 : bigDecimal.scale(); + int fracLen = Math.max(bigDecimal.scale(), 0); return intMax >= intLen && fracMax >= fracLen; } diff --git a/validation/src/main/java/io/micronaut/validation/validator/constraints/InternalConstraintValidators.java b/validation/src/main/java/io/micronaut/validation/validator/constraints/InternalConstraintValidators.java index 72233669..e0f6927d 100644 --- a/validation/src/main/java/io/micronaut/validation/validator/constraints/InternalConstraintValidators.java +++ b/validation/src/main/java/io/micronaut/validation/validator/constraints/InternalConstraintValidators.java @@ -91,8 +91,8 @@ final class InternalConstraintValidators { final DecimalMinValidator decimalMinValidatorNumber = InternalConstraintValidators::compareNumber; final DigitsValidator digitsValidatorNumber = value -> { - if (value instanceof BigDecimal) { - return (BigDecimal) value; + if (value instanceof BigDecimal decimalValue) { + return decimalValue; } return new BigDecimal(value.toString()); }; @@ -109,10 +109,10 @@ final class InternalConstraintValidators { new ValidationException("@Max annotation specified without value") ); - if (value instanceof BigInteger) { - return ((BigInteger) value).compareTo(BigInteger.valueOf(max)) <= 0; - } else if (value instanceof BigDecimal) { - return ((BigDecimal) value).compareTo(BigDecimal.valueOf(max)) <= 0; + if (value instanceof BigInteger intValue) { + return intValue.compareTo(BigInteger.valueOf(max)) <= 0; + } else if (value instanceof BigDecimal decimalValue) { + return decimalValue.compareTo(BigDecimal.valueOf(max)) <= 0; } return value.longValue() <= max; }; @@ -126,10 +126,10 @@ final class InternalConstraintValidators { new ValidationException("@Min annotation specified without value") ); - if (value instanceof BigInteger) { - return ((BigInteger) value).compareTo(BigInteger.valueOf(max)) >= 0; - } else if (value instanceof BigDecimal) { - return ((BigDecimal) value).compareTo(BigDecimal.valueOf(max)) >= 0; + if (value instanceof BigInteger intValue) { + return intValue.compareTo(BigInteger.valueOf(max)) >= 0; + } else if (value instanceof BigDecimal decimalValue) { + return decimalValue.compareTo(BigDecimal.valueOf(max)) >= 0; } return value.longValue() >= max; }; @@ -140,11 +140,11 @@ final class InternalConstraintValidators { if (value == null) { return true; } - if (value instanceof BigDecimal) { - return ((BigDecimal) value).signum() < 0; + if (value instanceof BigDecimal decimalValue) { + return decimalValue.signum() < 0; } - if (value instanceof BigInteger) { - return ((BigInteger) value).signum() < 0; + if (value instanceof BigInteger intValue) { + return intValue.signum() < 0; } if (value instanceof Double || value instanceof Float || @@ -161,11 +161,11 @@ final class InternalConstraintValidators { if (value == null) { return true; } - if (value instanceof BigDecimal) { - return ((BigDecimal) value).signum() <= 0; + if (value instanceof BigDecimal decimalValue) { + return decimalValue.signum() <= 0; } - if (value instanceof BigInteger) { - return ((BigInteger) value).signum() <= 0; + if (value instanceof BigInteger intValue) { + return intValue.signum() <= 0; } if (value instanceof Double || value instanceof Float || @@ -182,11 +182,11 @@ final class InternalConstraintValidators { if (value == null) { return true; } - if (value instanceof BigDecimal) { - return ((BigDecimal) value).signum() > 0; + if (value instanceof BigDecimal decimalValue) { + return decimalValue.signum() > 0; } - if (value instanceof BigInteger) { - return ((BigInteger) value).signum() > 0; + if (value instanceof BigInteger intValue) { + return intValue.signum() > 0; } if (value instanceof Double || value instanceof Float || @@ -203,11 +203,11 @@ final class InternalConstraintValidators { if (value == null) { return true; } - if (value instanceof BigDecimal) { - return ((BigDecimal) value).signum() >= 0; + if (value instanceof BigDecimal decimalValue) { + return decimalValue.signum() >= 0; } - if (value instanceof BigInteger) { - return ((BigInteger) value).signum() >= 0; + if (value instanceof BigInteger intValue) { + return intValue.signum() >= 0; } if (value instanceof Double || value instanceof Float || @@ -410,10 +410,10 @@ final class InternalConstraintValidators { */ private static int compareNumber(@NonNull Number value, @NonNull BigDecimal bigDecimal) { int result; - if (value instanceof BigDecimal) { - result = ((BigDecimal) value).compareTo(bigDecimal); - } else if (value instanceof BigInteger) { - result = new BigDecimal((BigInteger) value).compareTo(bigDecimal); + if (value instanceof BigDecimal decimalValue) { + result = decimalValue.compareTo(bigDecimal); + } else if (value instanceof BigInteger intValue) { + result = new BigDecimal(intValue).compareTo(bigDecimal); } else { result = BigDecimal.valueOf(value.doubleValue()).compareTo(bigDecimal); }