diff --git a/src/main/java/sg/com/smartinventory/utility/Utility.java b/src/main/java/sg/com/smartinventory/utility/Utility.java new file mode 100644 index 0000000..bf168a5 --- /dev/null +++ b/src/main/java/sg/com/smartinventory/utility/Utility.java @@ -0,0 +1,105 @@ +package sg.com.smartinventory.utility; + +public class Utility { + /** + * Get the method name of the caller. + *

+ * Java 9 introduced the Stack-Walking API to traverse the JVM stack frames in a + * lazy and efficient manner. + * First, we get a StackWalker instance using the getInstance() factory method. + * Then we use the walk() method to traverse the stack frames from the top to + * the bottom: + *

+ *

+ *

+ * Therefore, if we get the first element from the stream, we’ll know the + * currently executing method details. More specifically, we can use + * StackFrame.getMethodName() to find the method name. + *

+ * Compared to other approaches (more on them later), the Stack-Walking API has + * a few advantages: + *

+ *