reflection API is throwing no such method exception while migrating to java 9

letsStudyJava :

I am migrating to java 9 from java 8 I was using reflection to fetch method 'getStackTraceElement(int)' from class 'java.lang.Throwable' And I was setting the accessibility to "true".

programs works fine for java 8 but when I migrated to java 9 , It started failing with error 'java.lang.NoSuchMethodException: java.lang.Throwable.getStackTraceElement(int) . '

I have the code which works fine in java 8 as following private static Method m;

'public static void main(String args[])
{
    try 
    {
        System.out.println("here1");
        //next line throws error
        m = Throwable.class.getDeclaredMethod("getStackTraceElement",int.class);
        m.setAccessible(true);
        System.out.println("here2");
    } catch (Exception e) {

        //logger.warn(e.getLocalizedMessage(), e);
        System.out.println("==>"+e.getLocalizedMessage);
        System.out.println("==>"+e.getName);
        System.out.println("==>"+e.getStackTraceElement);
        System.out.println("here3");
    }
}'
Demozo :

They have introduced a class in Java 9 specifically for this purpose called the StackWalker.

https://docs.oracle.com/javase/9/docs/api/java/lang/StackWalker.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=87960&siteId=1