Prevent NPE from StringUtil.simpleName(..)

This commit is contained in:
Trustin Lee 2013-12-16 13:54:23 +09:00
parent e136227ee6
commit a79dfe74b7

View File

@ -36,6 +36,7 @@ public final class StringUtil {
try {
newLine = new Formatter().format("%n").toString();
} catch (Exception e) {
// Should not reach here, but just in case.
newLine = "\n";
}
@ -89,14 +90,22 @@ public final class StringUtil {
* The shortcut to {@link #simpleClassName(Class) simpleClassName(o.getClass())}.
*/
public static String simpleClassName(Object o) {
if (o == null) {
return "null_object";
} else {
return simpleClassName(o.getClass());
}
}
/**
* Generates a simplified name from a {@link Class}. Similar to {@link Class#getSimpleName()}, but it works fine
* with anonymous classes.
*/
public static String simpleClassName(Class<?> clazz) {
if (clazz == null) {
return "null_class";
}
Package pkg = clazz.getPackage();
if (pkg != null) {
return clazz.getName().substring(pkg.getName().length() + 1);