public class StackTrace {
public static boolean displayStackTraceInformation(Throwable ex) {
ex.printStackTrace();
StackTraceElement[] stackElements = ex.getStackTrace();
System.out.println("The " + stackElements.length + " element"
+ ((stackElements.length == 1) ? "" : "s") + " of the stack trace:\n");
for (int lcv = 0; lcv < stackElements.length; lcv++) {
System.out.println("File name: " + stackElements[lcv].getFileName());
System.out.println("Line number: " + stackElements[lcv].getLineNumber());
String className = stackElements[lcv].getClassName();
String packageName = extractPackageName(className);
String simpleClassName = extractSimpleClassName(className);
System.out.println("Package name: " + ("".equals(packageName) ? "[default package]" : packageName));
System.out.println("Full class name: " + className);
System.out.println("Simple class name: " + simpleClassName);
System.out.println("Unmunged class name: " + unmungeSimpleClassName(simpleClassName));
System.out.println("Direct class name: " + extractDirectClassName(simpleClassName));
System.out.println("Method name: " + stackElements[lcv].getMethodName());
System.out.println("Native method?: " + stackElements[lcv].isNativeMethod());
System.out.println("toString(): " + stackElements[lcv].toString());
System.out.println("");
}
return true;
}
public static String extractPackageName(String fullClassName) {
if ((null == fullClassName) || ("".equals(fullClassName)))
return "";
int lastDot = fullClassName.lastIndexOf('.');
if (0 >= lastDot)
return "";
return fullClassName.substring(0, lastDot);
}
public static String extractSimpleClassName(String fullClassName) {
if ((null == fullClassName) || ("".equals(fullClassName)))
return "";
int lastDot = fullClassName.lastIndexOf('.');
if (0 > lastDot)
return fullClassName;
return fullClassName.substring(++lastDot);
}
public static String extractDirectClassName(String simpleClassName) {
if ((null == simpleClassName) || ("".equals(simpleClassName)))
return "";
int lastSign = simpleClassName.lastIndexOf('$');
if (0 > lastSign)
return simpleClassName;
return simpleClassName.substring(++lastSign);
}
public static String unmungeSimpleClassName(String simpleClassName) {
if ((null == simpleClassName) || ("".equals(simpleClassName)))
return "";
return simpleClassName.replace('$', '.');
}
}
|