BAEL-5608 - Get current stack trace java (#12533)
* Initial commit for Object copy in Java * review comments commit for Object copy in Java * Initial commit for parseInt vs valueOf java * Review comments commit for parseInt vs valueOf java * Modify readme * review comments * build failure * build failure retry * build failure retry remove parseInt(java.lang.String,int,int,int) * build failure add comment * change examples * review comments * review comments 2 * review comments 3 * Initial commit for get current stacktrace * Remove old files * Name updates * Jenkins error * changes to file name * Review comments
This commit is contained in:
parent
614795051e
commit
a6ef4b67d1
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.exception.currentstacktrace;
|
||||
|
||||
public class DumpStackTraceDemo
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
methodA();
|
||||
}
|
||||
|
||||
public static void methodA() {
|
||||
try {
|
||||
int num1 = 5/0; // java.lang.ArithmeticException: divide by zero
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.exception.currentstacktrace;
|
||||
|
||||
public class StackTraceUsingThreadDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
methodA();
|
||||
}
|
||||
|
||||
public static StackTraceElement[] methodA() {
|
||||
return methodB();
|
||||
}
|
||||
|
||||
public static StackTraceElement[] methodB() {
|
||||
return Thread.currentThread().getStackTrace();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.exception.currentstacktrace;
|
||||
|
||||
public class StackTraceUsingThrowableDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
methodA();
|
||||
}
|
||||
|
||||
public static StackTraceElement[] methodA() {
|
||||
try {
|
||||
methodB();
|
||||
} catch (Throwable t) {
|
||||
return t.getStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void methodB() throws Throwable {
|
||||
throw new Throwable("A test exception");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.exception.currentstacktrace;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.exception.currentstacktrace.StackTraceUsingThreadDemo;
|
||||
import com.baeldung.exception.currentstacktrace.StackTraceUsingThrowableDemo;
|
||||
|
||||
public class CurrentStacktraceDemoUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenElementIsFecthedUsingThread_thenCorrectMethodAndClassIsReturned() {
|
||||
StackTraceElement[] stackTrace = new StackTraceUsingThreadDemo().methodA();
|
||||
|
||||
StackTraceElement elementZero = stackTrace[0];
|
||||
assertEquals("java.lang.Thread", elementZero.getClassName());
|
||||
assertEquals("getStackTrace", elementZero.getMethodName());
|
||||
|
||||
StackTraceElement elementOne = stackTrace[1];
|
||||
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThreadDemo", elementOne.getClassName());
|
||||
assertEquals("methodB", elementOne.getMethodName());
|
||||
|
||||
StackTraceElement elementTwo = stackTrace[2];
|
||||
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThreadDemo", elementTwo.getClassName());
|
||||
assertEquals("methodA", elementTwo.getMethodName());
|
||||
|
||||
StackTraceElement elementThree = stackTrace[3];
|
||||
assertEquals("com.baeldung.exception.currentstacktrace.CurrentStacktraceDemoUnitTest", elementThree.getClassName());
|
||||
assertEquals("whenElementIsFecthedUsingThread_thenCorrectMethodAndClassIsReturned", elementThree.getMethodName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenElementIsFecthedUsingThrowable_thenCorrectMethodAndClassIsReturned() {
|
||||
StackTraceElement[] stackTrace = new StackTraceUsingThrowableDemo().methodA();
|
||||
|
||||
StackTraceElement elementZero = stackTrace[0];
|
||||
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThrowableDemo", elementZero.getClassName());
|
||||
assertEquals("methodB", elementZero.getMethodName());
|
||||
|
||||
StackTraceElement elementOne = stackTrace[1];
|
||||
assertEquals("com.baeldung.exception.currentstacktrace.StackTraceUsingThrowableDemo", elementOne.getClassName());
|
||||
assertEquals("methodA", elementOne.getMethodName());
|
||||
|
||||
StackTraceElement elementThree = stackTrace[2];
|
||||
assertEquals("com.baeldung.exception.currentstacktrace.CurrentStacktraceDemoUnitTest", elementThree.getClassName());
|
||||
assertEquals("whenElementIsFecthedUsingThrowable_thenCorrectMethodAndClassIsReturned", elementThree.getMethodName());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue