diff --git a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java index a9a7e41b0f..9080c252cb 100644 --- a/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java +++ b/spring-test/src/main/java/org/springframework/test/util/AopTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2021 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. @@ -54,8 +54,8 @@ public abstract class AopTestUtils { public static T getTargetObject(Object candidate) { Assert.notNull(candidate, "Candidate must not be null"); try { - if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) { - Object target = ((Advised) candidate).getTargetSource().getTarget(); + if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) { + Object target = advised.getTargetSource().getTarget(); if (target != null) { return (T) target; } @@ -86,8 +86,8 @@ public abstract class AopTestUtils { public static T getUltimateTargetObject(Object candidate) { Assert.notNull(candidate, "Candidate must not be null"); try { - if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) { - Object target = ((Advised) candidate).getTargetSource().getTarget(); + if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) { + Object target = advised.getTargetSource().getTarget(); if (target != null) { return (T) getUltimateTargetObject(target); } diff --git a/spring-test/src/main/java/org/springframework/test/util/ExceptionCollector.java b/spring-test/src/main/java/org/springframework/test/util/ExceptionCollector.java index f53190b7af..68b84e0eb9 100644 --- a/spring-test/src/main/java/org/springframework/test/util/ExceptionCollector.java +++ b/spring-test/src/main/java/org/springframework/test/util/ExceptionCollector.java @@ -87,11 +87,11 @@ public class ExceptionCollector { if (this.exceptions.size() == 1) { Throwable exception = this.exceptions.get(0); - if (exception instanceof Error) { - throw (Error) exception; + if (exception instanceof Error error) { + throw error; } - if (exception instanceof Exception) { - throw (Exception) exception; + if (exception instanceof Exception ex) { + throw ex; } AssertionError assertionError = new AssertionError(exception.getMessage()); assertionError.initCause(exception); diff --git a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java index e8c1b47522..6bf523226e 100644 --- a/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/JsonPathExpectationsHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -97,9 +97,7 @@ public class JsonPathExpectationsHelper { */ public void assertValue(String content, @Nullable Object expectedValue) { Object actualValue = evaluateJsonPath(content); - if ((actualValue instanceof List) && !(expectedValue instanceof List)) { - @SuppressWarnings("rawtypes") - List actualValueList = (List) actualValue; + if ((actualValue instanceof List actualValueList) && !(expectedValue instanceof List)) { if (actualValueList.isEmpty()) { AssertionErrors.fail("No matching value at JSON path \"" + this.expression + "\""); } @@ -209,8 +207,8 @@ public class JsonPathExpectationsHelper { return; } String reason = failureReason("no value", value); - if (pathIsIndefinite() && value instanceof List) { - AssertionErrors.assertTrue(reason, ((List) value).isEmpty()); + if (pathIsIndefinite() && value instanceof List list) { + AssertionErrors.assertTrue(reason, list.isEmpty()); } else { AssertionErrors.assertTrue(reason, (value == null)); @@ -252,9 +250,9 @@ public class JsonPathExpectationsHelper { */ public void hasJsonPath(String content) { Object value = evaluateJsonPath(content); - if (pathIsIndefinite() && value instanceof List) { + if (pathIsIndefinite() && value instanceof List list) { String message = "No values for JSON path \"" + this.expression + "\""; - AssertionErrors.assertTrue(message, !((List) value).isEmpty()); + AssertionErrors.assertTrue(message, !list.isEmpty()); } } @@ -276,8 +274,8 @@ public class JsonPathExpectationsHelper { catch (AssertionError ex) { return; } - if (pathIsIndefinite() && value instanceof List) { - AssertionErrors.assertTrue(failureReason("no values", value), ((List) value).isEmpty()); + if (pathIsIndefinite() && value instanceof List list) { + AssertionErrors.assertTrue(failureReason("no values", value), list.isEmpty()); } else { AssertionErrors.fail(failureReason("no value", value)); @@ -328,8 +326,8 @@ public class JsonPathExpectationsHelper { Object value = evaluateJsonPath(content); String reason = "No value at JSON path \"" + this.expression + "\""; AssertionErrors.assertTrue(reason, value != null); - if (pathIsIndefinite() && value instanceof List) { - AssertionErrors.assertTrue(reason, !((List) value).isEmpty()); + if (pathIsIndefinite() && value instanceof List list) { + AssertionErrors.assertTrue(reason, !list.isEmpty()); } return value; }