I want to customize hashcode of StackTraceElement
class . My problem is how to use this new customized class instead of default StackTraceElement class of JVM.
Asked
Active
Viewed 1,129 times
0

Saurabh Agarwal
- 323
- 2
- 6
- 16
-
1But [StackTraceElement](http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html) is final Class. – vels4j Jan 21 '13 at 12:27
-
4What customisations do you want to perform? – adarshr Jan 21 '13 at 12:28
-
Yes it's final I had noticed it Thanks. But what all customization I want is modifying the hashcode generation process. – Saurabh Agarwal Jan 21 '13 at 12:36
-
1Careful now! If you tell us more, we might actually help you with your problem ;) – Cephalopod Jan 21 '13 at 12:44
2 Answers
2
I want to customize the StackTraceElement class by extending it
StackTraceElement is final
and hence can not be extended.
If you (for whatever reason) want to customize the way a stack trace is printed, you can implement a utility method which takes the Throwable
and then uses the various methods from StackTraceElement
to create your own layout, something like
public static void printCustomizedTrace(Throwable t) {
for(StackTraceElement e : t.getStackTrace()) {
System.err.println(" => " + e.getFileName() + ":" + e.getLineNumber());
}
}
You could also use delegation and create a list of CustomStackTraceElements
, and implement additional logic (like different hashmap()
) in the CustomStackTraceElement
class:
public static List<CustomStackTraceElement> getCustomizedStackTrace(Throwable t) {
List<CustomStackTraceElement> result = new ArrayList<>();
for(StackTraceElement e : t.getStackTrace()) {
result.add(new CustomStackTraceElement(e));
}
return result;
}

Andreas Fester
- 36,091
- 7
- 95
- 123