Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Hotfix for filoghost#70
Browse files Browse the repository at this point in the history
  • Loading branch information
markhughes committed Oct 18, 2017
1 parent c4d1a34 commit 677ec60
Showing 1 changed file with 28 additions and 13 deletions.
Expand Up @@ -40,23 +40,37 @@ public static Object getPrivateField(Class<?> clazz, Object handle, String field
*/
public static StackTraceElement getStackTraceElement(int index) {
try {
if (getStackTraceElementMethod == null) {
getStackTraceElementMethod = Throwable.class.getDeclaredMethod("getStackTraceElement", int.class);
getStackTraceElementMethod.setAccessible(true);
}
if (getStackTraceDepthMethod == null) {
getStackTraceDepthMethod = Throwable.class.getDeclaredMethod("getStackTraceDepth");
getStackTraceDepthMethod.setAccessible(true);
}
boolean noGetStackTraceElement = false;

Throwable dummyThrowable = new Throwable();
int depth = (Integer) getStackTraceDepthMethod.invoke(dummyThrowable);
try {
if (getStackTraceElementMethod == null) {
getStackTraceElementMethod = Throwable.class.getDeclaredMethod("getStackTraceElement", int.class);
getStackTraceElementMethod.setAccessible(true);
}
if (getStackTraceDepthMethod == null) {
getStackTraceDepthMethod = Throwable.class.getDeclaredMethod("getStackTraceDepth");
getStackTraceDepthMethod.setAccessible(true);
}
} catch (NoSuchMethodException e) {
noGetStackTraceElement = true;
}

if (index < depth) {
return (StackTraceElement) getStackTraceElementMethod.invoke(new Throwable(), index);
if (noGetStackTraceElement) {
// Hotfix for: https://github.com/filoghost/HolographicDisplays/issues/70
// TODO: use StackWalker
Throwable dummyThrowable = new Throwable();
return dummyThrowable.getStackTrace()[index];
} else {
return null;
Throwable dummyThrowable = new Throwable();
int depth = (Integer) getStackTraceDepthMethod.invoke(dummyThrowable);

if (index < depth) {
return (StackTraceElement) getStackTraceElementMethod.invoke(new Throwable(), index);
} else {
return null;
}
}

} catch (Throwable t) {
if (!stackTraceErrorPrinted) {
HolographicDisplays.getInstance().getLogger().log(Level.WARNING, "Unable to get a stacktrace element, please inform the developer. You will only see this error once to avoid spam.", t);
Expand All @@ -65,4 +79,5 @@ public static StackTraceElement getStackTraceElement(int index) {
return null;
}
}

}

0 comments on commit 677ec60

Please sign in to comment.