[LANG-751] Include the actual type in the Validate.isInstance and isAssignableFrom exception messages.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1166685 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
960b9a6cf7
commit
603d818735
|
@ -69,9 +69,8 @@ public class Validate {
|
||||||
private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE =
|
private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE =
|
||||||
"The validated collection index is invalid: %d";
|
"The validated collection index is invalid: %d";
|
||||||
private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false";
|
private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false";
|
||||||
private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE =
|
private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE = "Cannot assign a %s to a %s";
|
||||||
"The validated class can not be converted to the %s class";
|
private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "Expected type: %s, actual: %s";
|
||||||
private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "The validated object is not an instance of %s";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor. This class should not normally be instantiated.
|
* Constructor. This class should not normally be instantiated.
|
||||||
|
@ -975,14 +974,13 @@ public class Validate {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Validate that the argument is an instance of the specified class; otherwise
|
* Validates that the argument is an instance of the specified class, if not throws an exception.
|
||||||
* throwing an exception. This method is useful when validating according to an arbitrary
|
*
|
||||||
* class</p>
|
* <p>This method is useful when validating according to an arbitrary class</p>
|
||||||
*
|
*
|
||||||
* <pre>Validate.isInstanceOf(OkClass.class, object);</pre>
|
* <pre>Validate.isInstanceOf(OkClass.class, object);</pre>
|
||||||
*
|
*
|
||||||
* <p>The message of the exception is "The validated object is not an instance of"
|
* <p>The message of the exception is "Expected type: {type}, actual: {obj_type}"</p>
|
||||||
* followed by the name of the class</p>
|
|
||||||
*
|
*
|
||||||
* @param type the class the object must be validated against, not null
|
* @param type the class the object must be validated against, not null
|
||||||
* @param obj the object to check, null throws an exception
|
* @param obj the object to check, null throws an exception
|
||||||
|
@ -993,7 +991,8 @@ public class Validate {
|
||||||
*/
|
*/
|
||||||
public static void isInstanceOf(Class<?> type, Object obj) {
|
public static void isInstanceOf(Class<?> type, Object obj) {
|
||||||
if (type.isInstance(obj) == false) {
|
if (type.isInstance(obj) == false) {
|
||||||
throw new IllegalArgumentException(String.format(DEFAULT_IS_INSTANCE_OF_EX_MESSAGE, type.getName()));
|
throw new IllegalArgumentException(String.format(DEFAULT_IS_INSTANCE_OF_EX_MESSAGE, type.getName(),
|
||||||
|
obj == null ? "null" : obj.getClass().getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1024,32 +1023,32 @@ public class Validate {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Validate that the argument can be converted to the specified class; otherwise
|
* Validates that the argument can be converted to the specified class, if not, throws an exception.
|
||||||
* throwing an exception with the specified message. This method is useful when
|
*
|
||||||
* validating if there will be no casting errors.</p>
|
* <p>This method is useful when validating that there will be no casting errors.</p>
|
||||||
*
|
*
|
||||||
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
|
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
|
||||||
*
|
*
|
||||||
* <p>The message of the exception is "The validated object can not be converted to the"
|
* <p>The message format of the exception is "Cannot assign {type} to {superType}"</p>
|
||||||
* followed by the name of the class and "class"</p>
|
|
||||||
*
|
*
|
||||||
* @param superType the class the class must be validated against, not null
|
* @param superType the class the class must be validated against, not null
|
||||||
* @param type the class to check, not null
|
* @param type the class to check, not null
|
||||||
* @throws IllegalArgumentException if argument can not be converted to the specified class
|
* @throws IllegalArgumentException if type argument is not assignable to the specified superType
|
||||||
* @see #isAssignableFrom(Class, Class, String, Object...)
|
* @see #isAssignableFrom(Class, Class, String, Object...)
|
||||||
*
|
*
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public static void isAssignableFrom(Class<?> superType, Class<?> type) {
|
public static void isAssignableFrom(Class<?> superType, Class<?> type) {
|
||||||
if (superType.isAssignableFrom(type) == false) {
|
if (superType.isAssignableFrom(type) == false) {
|
||||||
throw new IllegalArgumentException(String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, superType.getName()));
|
throw new IllegalArgumentException(String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, type == null ? "null" : type.getName(),
|
||||||
|
superType.getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Validate that the argument can be converted to the specified class; otherwise
|
* Validates that the argument can be converted to the specified class, if not throws an exception.
|
||||||
* throwing an exception. This method is useful when validating if there will be no
|
*
|
||||||
* casting errors.</p>
|
* <p>This method is useful when validating if there will be no casting errors.</p>
|
||||||
*
|
*
|
||||||
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
|
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
|
||||||
*
|
*
|
||||||
|
|
|
@ -21,7 +21,8 @@
|
||||||
</properties>
|
</properties>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<release version="3.0.2" date="unreleased" description="September release">
|
<release version="3.0.2" date="unreleased" description="September release">
|
||||||
|
<action type="update" issue="LANG-751">Include the actual type in the Validate.isInstance and isAssignableFrom exception messages</action>
|
||||||
<action type="fix" issue="LANG-746">NumberUtils does not handle upper-case hex: 0X and -0X</action>
|
<action type="fix" issue="LANG-746">NumberUtils does not handle upper-case hex: 0X and -0X</action>
|
||||||
<action type="update" issue="LANG-736">CharUtils static final array CHAR_STRING is not needed to compute CHAR_STRING_ARRAY</action>
|
<action type="update" issue="LANG-736">CharUtils static final array CHAR_STRING is not needed to compute CHAR_STRING_ARRAY</action>
|
||||||
<action type="fix" issue="LANG-744">StringUtils throws java.security.AccessControlException on Google App Engine</action>
|
<action type="fix" issue="LANG-744">StringUtils throws java.security.AccessControlException on Google App Engine</action>
|
||||||
|
|
|
@ -849,11 +849,14 @@ public class ValidateTest extends TestCase {
|
||||||
public void testIsInstanceOf() {
|
public void testIsInstanceOf() {
|
||||||
Validate.isInstanceOf(String.class, "hi");
|
Validate.isInstanceOf(String.class, "hi");
|
||||||
Validate.isInstanceOf(Integer.class, 1);
|
Validate.isInstanceOf(Integer.class, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsInstanceOfExceptionMessage() {
|
||||||
try {
|
try {
|
||||||
Validate.isInstanceOf(List.class, "hi");
|
Validate.isInstanceOf(List.class, "hi");
|
||||||
fail("Expecting IllegalArgumentException");
|
fail("Expecting IllegalArgumentException");
|
||||||
} catch(IllegalArgumentException e) {
|
} catch(IllegalArgumentException e) {
|
||||||
assertEquals("The validated object is not an instance of java.util.List", e.getMessage());
|
assertEquals("Expected type: java.util.List, actual: java.lang.String", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -871,11 +874,14 @@ public class ValidateTest extends TestCase {
|
||||||
public void testIsAssignable() {
|
public void testIsAssignable() {
|
||||||
Validate.isAssignableFrom(CharSequence.class, String.class);
|
Validate.isAssignableFrom(CharSequence.class, String.class);
|
||||||
Validate.isAssignableFrom(AbstractList.class, ArrayList.class);
|
Validate.isAssignableFrom(AbstractList.class, ArrayList.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsAssignableExceptionMessage() {
|
||||||
try {
|
try {
|
||||||
Validate.isAssignableFrom(List.class, String.class);
|
Validate.isAssignableFrom(List.class, String.class);
|
||||||
fail("Expecting IllegalArgumentException");
|
fail("Expecting IllegalArgumentException");
|
||||||
} catch(IllegalArgumentException e) {
|
} catch(IllegalArgumentException e) {
|
||||||
assertEquals("The validated class can not be converted to the java.util.List class", e.getMessage());
|
assertEquals("Cannot assign a java.lang.String to a java.util.List", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue