diff --git a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java index c149a4c3332..5d6b9c5e5d6 100644 --- a/spring-core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ObjectUtils.java @@ -59,23 +59,21 @@ public abstract class ObjectUtils { } /** - * Check whether the given exception is compatible with the exceptions - * declared in a throws clause. - * @param ex the exception to checked - * @param declaredExceptions the exceptions declared in the throws clause + * Check whether the given exception is compatible with the specified + * exception types, as declared in a throws clause. + * @param ex the exception to check + * @param declaredExceptions the exception types declared in the throws clause * @return whether the given exception is compatible */ - public static boolean isCompatibleWithThrowsClause(Throwable ex, Class[] declaredExceptions) { + public static boolean isCompatibleWithThrowsClause(Throwable ex, Class... declaredExceptions) { if (!isCheckedException(ex)) { return true; } if (declaredExceptions != null) { - int i = 0; - while (i < declaredExceptions.length) { - if (declaredExceptions[i].isAssignableFrom(ex.getClass())) { + for (Class declaredException : declaredExceptions) { + if (declaredException.isInstance(ex)) { return true; } - i++; } } return false; diff --git a/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java b/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java index fcfe1b38d40..800a0ac19aa 100644 --- a/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2013 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. @@ -16,9 +16,6 @@ package org.springframework.util; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - import java.io.IOException; import java.sql.SQLException; @@ -26,6 +23,9 @@ import junit.framework.TestCase; import org.springframework.core.task.TaskRejectedException; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + /** * @author Rod Johnson * @author Juergen Hoeller @@ -51,25 +51,25 @@ public final class ObjectUtilsTests extends TestCase { Class[] sqlAndIO = new Class[] {SQLException.class, IOException.class}; Class[] throwable = new Class[] {Throwable.class}; - assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), null)); + assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException())); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), empty)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), exception)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), sqlAndIO)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new RuntimeException(), throwable)); - assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), null)); + assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception())); assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), empty)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), exception)); assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), sqlAndIO)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new Exception(), throwable)); - assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), null)); + assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new SQLException())); assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), empty)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), exception)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), sqlAndIO)); assertTrue(ObjectUtils.isCompatibleWithThrowsClause(new SQLException(), throwable)); - assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), null)); + assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable())); assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), empty)); assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), exception)); assertFalse(ObjectUtils.isCompatibleWithThrowsClause(new Throwable(), sqlAndIO)); @@ -196,7 +196,7 @@ public final class ObjectUtilsTests extends TestCase { Object obj = new Object(); String expected = obj.getClass().getName() + "@" + ObjectUtils.getIdentityHexString(obj); String actual = ObjectUtils.identityToString(obj); - assertEquals(expected.toString(), actual); + assertEquals(expected, actual); } public void testIdentityToStringWithNullObject() { @@ -620,7 +620,7 @@ public final class ObjectUtilsTests extends TestCase { assertEquals("null", ObjectUtils.nullSafeToString((String[]) null)); } - enum Tropes { FOO, BAR, baz }; + enum Tropes { FOO, BAR, baz } public void testContainsConstant() { assertThat(ObjectUtils.containsConstant(Tropes.values(), "FOO"), is(true));