Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Aug 5, 2024
1 parent 3244fde commit 19df3eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
19 changes: 15 additions & 4 deletions rxlib/src/main/java/org/rx/core/Reflects.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,29 @@ static class ConvertBean<TS, TT> {
final TripleFunc<TS, Class<TT>, TT> converter;
}

static class SecurityManagerEx extends SecurityManager {
public interface ClassTracer {
Class<?>[] getClassTrace();

Class<?> getClassTrace(int depth);
}

static class SecurityManagerEx extends SecurityManager implements ClassTracer {
static final SecurityManagerEx INSTANCE = new SecurityManagerEx();

Class<?> stackClass(int depth) {
return getClassContext()[depth];
public Class<?>[] getClassTrace() {
return getClassContext();
}

public Class<?> getClassTrace(int depth) {
return getClassContext()[1 + depth];
}
}
//endregion

public static final Linq<String> COLLECTION_WRITE_METHOD_NAMES = Linq.from("add", "remove", "addAll", "removeAll", "removeIf", "retainAll", "clear"),
List_WRITE_METHOD_NAMES = COLLECTION_WRITE_METHOD_NAMES.union(Arrays.toList("replaceAll", "set"));
public static final Set<Method> OBJECT_METHODS = Collections.unmodifiableSet(new HashSet<>(Arrays.toList(Object.class.getMethods())));
public static final ClassTracer CLASS_TRACER = new SecurityManagerEx();
static final String M_0 = "close", CHANGE_TYPE_METHOD = "valueOf";
static final String GET_PROPERTY = "get", GET_BOOL_PROPERTY = "is", SET_PROPERTY = "set";
static final String TYPED_JSON_KEY = "$rxType";
Expand Down Expand Up @@ -123,7 +134,7 @@ public static Class<?> getCallerClass() {

public static Class<?> stackClass(int depth) {
//Throwable.class.getDeclaredMethod("getStackTraceElement", int.class) & Reflection.getCallerClass(2 + depth) java 11 not exist
return SecurityManagerEx.INSTANCE.stackClass(2 + depth);
return SecurityManagerEx.INSTANCE.getClassTrace()[2 + depth];
}

public static InputStream getResource(String namePattern) {
Expand Down
4 changes: 2 additions & 2 deletions rxlib/src/main/java/org/rx/core/ThreadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private Task(Func<T> fn, FlagsEnum<RunFlag> flags, Object id) {
}
if (conf.trace.slowMethodElapsedMicros > 0) {
//Reflects.getStackTrace(t)
caller = Reflects.getCallerClass();
caller = Reflects.CLASS_TRACER.getClassTrace(0);
} else {
caller = null;
}
Expand All @@ -205,7 +205,7 @@ public T call() {
throw e;
} finally {
Thread t = Thread.currentThread();
TraceHandler.INSTANCE.saveMethodTrace(t, ifNull(caller, ThreadPool.class), fn.getClass().getName(), id == null ? null : new Object[]{id},
TraceHandler.INSTANCE.saveMethodTrace(t, ifNull(caller, ThreadPool.class), fn.getClass().getSimpleName(), id == null ? null : new Object[]{id},
r, ex, System.nanoTime() - s);
}
return r;
Expand Down
3 changes: 2 additions & 1 deletion rxlib/src/test/java/org/rx/core/TestCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,8 @@ public void reflect() {
System.out.println(resource);
assert resource != null;
}
assert Reflects.stackClass(0) == this.getClass();
System.out.println(toJsonString(Reflects.CLASS_TRACER.getClassTrace()));
assert Reflects.CLASS_TRACER.getClassTrace(0) == this.getClass();
// for (StackTraceElement traceElement : Reflects.stackTrace(8)) {
// System.out.println(traceElement);
// }
Expand Down

0 comments on commit 19df3eb

Please sign in to comment.