|
|
@@ -6,6 +6,7 @@
|
|
|
*/
|
|
|
package net.ranides.assira.trace;
|
|
|
|
|
|
+import lombok.experimental.UtilityClass;
|
|
|
import net.ranides.assira.reflection.*;
|
|
|
import net.ranides.assira.reflection.util.ServiceScanner;
|
|
|
|
|
|
@@ -14,13 +15,33 @@ import java.util.Optional;
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
/**
|
|
|
+ * TraceUtils is used for stack trace inspection.
|
|
|
+ * Most importantly, it allows to check stackframe of the caller.
|
|
|
+ * It can be used to search for specific stackframe too.
|
|
|
+ *
|
|
|
+ * This class tries to optimize operations as much as possible.
|
|
|
+ *
|
|
|
+ * Loading only frame of interest if prefferred over obtaining full list of elements.
|
|
|
+ * Altough even returned lists try to load data lazily.
|
|
|
+ *
|
|
|
+ * Please note, that assira library packaged for different JVM version
|
|
|
+ * could use different implementations.
|
|
|
*
|
|
|
* @author Ranides Atterwim {@literal <ranides@gmail.com>}
|
|
|
*/
|
|
|
-public final class TraceUtils {
|
|
|
+@UtilityClass
|
|
|
+public class TraceUtils {
|
|
|
|
|
|
+ /**
|
|
|
+ * Index of callee stack frame.
|
|
|
+ * In other words: index of stack frame of current method.
|
|
|
+ */
|
|
|
public static final int CALLEE = 1;
|
|
|
|
|
|
+ /**
|
|
|
+ * Index of caller stack frame.
|
|
|
+ * In other words: index of stack frame of method which invoked current method.
|
|
|
+ */
|
|
|
public static final int CALLER = 2;
|
|
|
|
|
|
interface IStackWalker {
|
|
|
@@ -52,73 +73,161 @@ public final class TraceUtils {
|
|
|
.first()
|
|
|
.orElse(null);
|
|
|
|
|
|
- private TraceUtils() {
|
|
|
- /* utility class */
|
|
|
- }
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Returns list with all stack frames.
|
|
|
+ *
|
|
|
+ * @return list
|
|
|
+ */
|
|
|
public static List<StackTraceElement> getFrames() {
|
|
|
return WALKER.getFrames();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns list with all classes from stack frames.
|
|
|
+ *
|
|
|
+ * @return list
|
|
|
+ */
|
|
|
public static List<Class<?>> getTypes() {
|
|
|
return WALKER.getTypes();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns list with all class names from stack frames.
|
|
|
+ *
|
|
|
+ * @return list
|
|
|
+ */
|
|
|
public static List<String> getNames() {
|
|
|
return WALKER.getNames();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns first frame matching predicate
|
|
|
+ *
|
|
|
+ * @param filter filter
|
|
|
+ * @return optional
|
|
|
+ */
|
|
|
public static Optional<StackTraceElement> getFrame(Predicate<StackTraceElement> filter) {
|
|
|
return WALKER.getFrame(filter);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns first class matching predicate
|
|
|
+ *
|
|
|
+ * @param filter filter
|
|
|
+ * @return optional
|
|
|
+ */
|
|
|
public static Optional<Class<?>> getType(Predicate<Class<?>> filter) {
|
|
|
return WALKER.getType(filter);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns first class name matching predicate
|
|
|
+ *
|
|
|
+ * @param filter filter
|
|
|
+ * @return optional
|
|
|
+ */
|
|
|
public static Optional<String> getName(Predicate<String> filter) {
|
|
|
return WALKER.getName(filter);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns frame at specified index.
|
|
|
+ * Most important indexes are CALLEE and CALLER.
|
|
|
+ *
|
|
|
+ * Behaviour is undefined for invalid indexes.
|
|
|
+ *
|
|
|
+ * @param index index
|
|
|
+ * @return frame
|
|
|
+ */
|
|
|
public static StackTraceElement getFrame(int index) {
|
|
|
return WALKER.getFrame(index);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns class at specified index.
|
|
|
+ * Most important indexes are CALLEE and CALLER.
|
|
|
+ *
|
|
|
+ * Behaviour is undefined for invalid indexes.
|
|
|
+ *
|
|
|
+ * @param index index
|
|
|
+ * @return class
|
|
|
+ */
|
|
|
public static Class<?> getType(int index) {
|
|
|
return WALKER.getType(index);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns class name at specified index.
|
|
|
+ * Most important indexes are CALLEE and CALLER.
|
|
|
+ *
|
|
|
+ * Behaviour is undefined for invalid indexes.
|
|
|
+ *
|
|
|
+ * @param index index
|
|
|
+ * @return class name
|
|
|
+ */
|
|
|
public static String getName(int index) {
|
|
|
return WALKER.getName(index);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns frame of callee method.
|
|
|
+ *
|
|
|
+ * @return frame
|
|
|
+ */
|
|
|
public static StackTraceElement getCalleeFrame() {
|
|
|
return getFrame(CALLEE);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns class of callee method.
|
|
|
+ *
|
|
|
+ * @return class
|
|
|
+ */
|
|
|
public static Class<?> getCalleeType() {
|
|
|
return getType(CALLEE);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns class name of callee method.
|
|
|
+ *
|
|
|
+ * @return class name
|
|
|
+ */
|
|
|
public static String getCalleeName() {
|
|
|
return getName(CALLEE);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns frame of caller method.
|
|
|
+ *
|
|
|
+ * @return frame
|
|
|
+ */
|
|
|
public static StackTraceElement getCallerFrame() {
|
|
|
return getFrame(CALLER);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns class of caller method.
|
|
|
+ *
|
|
|
+ * @return class
|
|
|
+ */
|
|
|
public static Class<?> getCallerType() {
|
|
|
return getType(CALLER);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns class name of caller method.
|
|
|
+ *
|
|
|
+ * @return class name
|
|
|
+ */
|
|
|
public static String getCallerName() {
|
|
|
return getName(CALLER);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Finds first public StackFrame invoked by JUnit framework.
|
|
|
- * @return empty if code is not invoked from unit test
|
|
|
+ * Returns none if code is not invoked from unit test
|
|
|
+ *
|
|
|
+ * @return optional
|
|
|
*/
|
|
|
public static Optional<StackTraceElement> getTestFrame() {
|
|
|
List<StackTraceElement> frames = TraceUtils.getFrames();
|