Skip to content

Commit

Permalink
faster reflection protect
Browse files Browse the repository at this point in the history
  • Loading branch information
n9Mtq4 committed Feb 19, 2017
1 parent 443ea86 commit 6e25339
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/main/java/com/n9mtq4/reflection/ReflectionProtector.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public final class ReflectionProtector {
* the first one requires the default index to be 3
* the second one requires the default index to be 3
* */
private StackTraceElement[] getStackTrace() {
return new Throwable().getStackTrace();
// return Thread.currentThread().getStackTrace();
private static StackTraceElement[] getStackTrace() {
return new Throwable().getStackTrace(); // DEFAULT_INDEX = 3;
// return Thread.currentThread().getStackTrace(); // DEFAULT_INDEX = 4;
}

/**
Expand All @@ -40,7 +40,7 @@ private StackTraceElement[] getStackTrace() {
* @return boolean - if the calling method was invoked with reflection
* */
public static boolean usedReflection() {
return usedReflection(Thread.currentThread().getStackTrace());
return usedReflection(getStackTrace());
}

private static boolean usedReflection(StackTraceElement[] stackTraceElements) {
Expand All @@ -56,7 +56,7 @@ private static boolean usedReflection(StackTraceElement[] stackTraceElements) {
* @return boolean - if the stacktrace at the given index is a reflection one.
* */
public static boolean usedReflection(int index) {
return usedReflection(index, Thread.currentThread().getStackTrace());
return usedReflection(index, getStackTrace());
}

private static boolean usedReflection(int index, StackTraceElement[] stackTraceElements) {
Expand All @@ -73,13 +73,13 @@ private static boolean usedReflection(int index, StackTraceElement[] stackTraceE
* @return boolean - if the stacktrace within the two ranges is a reflection one
* */
public static boolean usedReflection(int min, int max) {
return usedReflection(min, max, Thread.currentThread().getStackTrace());
return usedReflection(min, max, getStackTrace());
}

private static boolean usedReflection(int min, int max, StackTraceElement[] stackTraceElements) {
for (int i = min; i < max; i++) {
if (stackTraceElements[i].getClassName().startsWith("java.lang.reflect") || stackTraceElements[i].getClassName().startsWith("sun.reflect")) return true;

if (stackTraceElements[i].getClassName().startsWith("java.lang.reflect") ||
stackTraceElements[i].getClassName().startsWith("sun.reflect")) return true;
}
return false;
}
Expand Down

0 comments on commit 6e25339

Please sign in to comment.