diff --git a/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java b/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java index 2ef8f10ae0f..964d8e1bf36 100644 --- a/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java +++ b/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java @@ -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 public List getFieldErrors() { List 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 @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 List 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 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; diff --git a/spring-context/src/main/java/org/springframework/validation/BindingResultUtils.java b/spring-context/src/main/java/org/springframework/validation/BindingResultUtils.java index deef4e8d109..04694d823f8 100644 --- a/spring-context/src/main/java/org/springframework/validation/BindingResultUtils.java +++ b/spring-context/src/main/java/org/springframework/validation/BindingResultUtils.java @@ -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 { 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; } /** diff --git a/spring-context/src/main/java/org/springframework/validation/DataBinder.java b/spring-context/src/main/java/org/springframework/validation/DataBinder.java index 88ce9a88965..c2e5ecfa3b0 100644 --- a/spring-context/src/main/java/org/springframework/validation/DataBinder.java +++ b/spring-context/src/main/java/org/springframework/validation/DataBinder.java @@ -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])); diff --git a/spring-context/src/main/java/org/springframework/validation/ObjectError.java b/spring-context/src/main/java/org/springframework/validation/ObjectError.java index 0a02d714a76..0e19cc7b934 100644 --- a/spring-context/src/main/java/org/springframework/validation/ObjectError.java +++ b/spring-context/src/main/java/org/springframework/validation/ObjectError.java @@ -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 { 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 { */ 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()))); } diff --git a/spring-context/src/main/java/org/springframework/validation/annotation/ValidationAnnotationUtils.java b/spring-context/src/main/java/org/springframework/validation/annotation/ValidationAnnotationUtils.java index a5309bcb02c..1ca33e89acd 100644 --- a/spring-context/src/main/java/org/springframework/validation/annotation/ValidationAnnotationUtils.java +++ b/spring-context/src/main/java/org/springframework/validation/annotation/ValidationAnnotationUtils.java @@ -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 { 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}); } }