Prevent NPE from StringUtil.simpleName(..)

This commit is contained in:
Trustin Lee 2013-12-16 13:54:23 +09:00
parent 1a9eb05ba0
commit cc295107b3

View File

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