Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6531596

Wrong exception from Method.invoke() call

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • hs10
    • 6
    • hotspot
    • None
    • b14
    • generic
    • generic

        Consider the following interface and implementor classes:

        public interface Intf {
           // public void method1();
        }

        import java.lang.reflect.Method;
        import java.lang.reflect.InvocationTargetException;

        public class SubClass implements Intf {
            public static void main(String[] args) {
                SubClass sc = new SubClass();
                Class paramClasses[] = new Class[]{};
                
                for (int i = 0; i < 20; i++) {
                  try{
                    System.out.println("Try: " + i);
                    Method method1 = sc.getClass().getMethod("method1",paramClasses);
                    method1.invoke(sc);
                  } catch(NoSuchMethodException nme){
                    nme.printStackTrace();
                  } catch(AbstractMethodError ame){
                    ame.printStackTrace();
                  } catch (IllegalAccessException e) {
                    e.printStackTrace();
                  } catch (InvocationTargetException e) {
                    e.printStackTrace();
                  }
                }
            }
        }

        Steps to reproduce the issue:

        1) Compiled both the classes
        2) Uncomment method1() in Intf.java
        3) recompile only Intf.java

        We get incompatible class set. Now, run

        java Subclass

        You get AbstractMethodError for the first 15 iterations and then get InvocationTargetException that wraps AbstractMethodError. Actually, AbstractMethodError is expected always -- because "method1()" is not implemented in "SubClass" and therefore the linkage should fail always. Throwing InvocationTargetException seem to suggest that the method was called and resulted in some exception being thrown.

              acorn Karen Kinnear (Inactive)
              sundar Sundararajan Athijegannathan
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: