Browse Source

Apply "instanceof pattern matching" to remaining Validation classes

pull/30047/head
Sam Brannen 2 years ago
parent
commit
024d02225c
  1. 20
      spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java
  2. 11
      spring-context/src/main/java/org/springframework/validation/BindingResultUtils.java
  3. 4
      spring-context/src/main/java/org/springframework/validation/DataBinder.java
  4. 8
      spring-context/src/main/java/org/springframework/validation/ObjectError.java
  5. 4
      spring-context/src/main/java/org/springframework/validation/annotation/ValidationAnnotationUtils.java

20
spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* 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.
@ -169,8 +169,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi @@ -169,8 +169,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi
public List<FieldError> getFieldErrors() {
List<FieldError> result = new ArrayList<>();
for (ObjectError objectError : this.errors) {
if (objectError instanceof FieldError) {
result.add((FieldError) objectError);
if (objectError instanceof FieldError fieldError) {
result.add(fieldError);
}
}
return Collections.unmodifiableList(result);
@ -180,8 +180,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi @@ -180,8 +180,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi
@Nullable
public FieldError getFieldError() {
for (ObjectError objectError : this.errors) {
if (objectError instanceof FieldError) {
return (FieldError) objectError;
if (objectError instanceof FieldError fieldError) {
return fieldError;
}
}
return null;
@ -192,8 +192,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi @@ -192,8 +192,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi
List<FieldError> result = new ArrayList<>();
String fixedField = fixedField(field);
for (ObjectError objectError : this.errors) {
if (objectError instanceof FieldError && isMatchingFieldError(fixedField, (FieldError) objectError)) {
result.add((FieldError) objectError);
if (objectError instanceof FieldError fieldError && isMatchingFieldError(fixedField, fieldError)) {
result.add(fieldError);
}
}
return Collections.unmodifiableList(result);
@ -204,10 +204,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi @@ -204,10 +204,8 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi
public FieldError getFieldError(String field) {
String fixedField = fixedField(field);
for (ObjectError objectError : this.errors) {
if (objectError instanceof FieldError fieldError) {
if (isMatchingFieldError(fixedField, fieldError)) {
return fieldError;
}
if (objectError instanceof FieldError fieldError && isMatchingFieldError(fixedField, fieldError)) {
return fieldError;
}
}
return null;

11
spring-context/src/main/java/org/springframework/validation/BindingResultUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* 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.
@ -42,10 +42,15 @@ public abstract class BindingResultUtils { @@ -42,10 +42,15 @@ public abstract class BindingResultUtils {
Assert.notNull(model, "Model map must not be null");
Assert.notNull(name, "Name must not be null");
Object attr = model.get(BindingResult.MODEL_KEY_PREFIX + name);
if (attr != null && !(attr instanceof BindingResult)) {
if (attr == null) {
return null;
}
if (attr instanceof BindingResult bindingResult) {
return bindingResult;
}
else {
throw new IllegalStateException("BindingResult attribute is not of type BindingResult: " + attr);
}
return (BindingResult) attr;
}
/**

4
spring-context/src/main/java/org/springframework/validation/DataBinder.java

@ -849,8 +849,8 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { @@ -849,8 +849,8 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
PropertyValue pv = propertyValues.get(field);
boolean empty = (pv == null || pv.getValue() == null);
if (!empty) {
if (pv.getValue() instanceof String) {
empty = !StringUtils.hasText((String) pv.getValue());
if (pv.getValue() instanceof String text) {
empty = !StringUtils.hasText(text);
}
else if (pv.getValue() instanceof String[] values) {
empty = (values.length == 0 || !StringUtils.hasText(values[0]));

8
spring-context/src/main/java/org/springframework/validation/ObjectError.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* 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.
@ -105,8 +105,8 @@ public class ObjectError extends DefaultMessageSourceResolvable { @@ -105,8 +105,8 @@ public class ObjectError extends DefaultMessageSourceResolvable {
if (sourceType.isInstance(this.source)) {
return sourceType.cast(this.source);
}
else if (this.source instanceof Throwable) {
Throwable cause = ((Throwable) this.source).getCause();
else if (this.source instanceof Throwable throwable) {
Throwable cause = throwable.getCause();
if (sourceType.isInstance(cause)) {
return sourceType.cast(cause);
}
@ -126,7 +126,7 @@ public class ObjectError extends DefaultMessageSourceResolvable { @@ -126,7 +126,7 @@ public class ObjectError extends DefaultMessageSourceResolvable {
*/
public boolean contains(Class<?> sourceType) {
return (sourceType.isInstance(this.source) ||
(this.source instanceof Throwable && sourceType.isInstance(((Throwable) this.source).getCause())));
(this.source instanceof Throwable throwable && sourceType.isInstance(throwable.getCause())));
}

4
spring-context/src/main/java/org/springframework/validation/annotation/ValidationAnnotationUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* 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.
@ -64,7 +64,7 @@ public abstract class ValidationAnnotationUtils { @@ -64,7 +64,7 @@ public abstract class ValidationAnnotationUtils {
if (hints == null) {
return EMPTY_OBJECT_ARRAY;
}
return (hints instanceof Object[] ? (Object[]) hints : new Object[]{hints});
return (hints instanceof Object[] objectHints ? objectHints : new Object[] {hints});
}
}

Loading…
Cancel
Save