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

java.lang.Method.invoke cannot invoke method from non-public inherited class

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1
    • core-libs



      Name: mc57594 Date: 05/21/97


      Create the two files listed below (Bug.java and pkg\Foo.java). Compile them using javac. Run "java Bug". You will get an illegal access exception. It does not matter if getFoo() is static or not.

      This is a very serious bug in that invoking accessor methods on Java Beans properties in a generic fashion is hampered by this.

      A sample run of the program prints :

      D:\java_bugs\three>java Bug
      Method = public int pkg.Bar.getFoo()
      Exception caught:java.lang.IllegalAccessException: pkg/Bar


      FILE1: Bug.java

      import java.lang.reflect.*;
      import pkg.*;

      public class Bug
      {
              public static void main(String[] args)
              {
                      try
                      {
                              Class cls = pkg.Foo.class;
                              Method method = cls.getMethod("getFoo", null);
                              System.out.println("Method = " + method );

                              Foo foo = new Foo();
                              method.invoke( foo, null );
                      }
                      catch (Exception e)
                      {
                              System.out.println("Exception caught:" + e.toString() );

                      }
              }
      }


      FILE2: pkg\Foo.java

      package pkg;

      class Bar
      {
              public int getFoo() { return 1; }
      }

      public class Foo extends Bar
      {
      }
      company - SAS Institute Inc. , email - ###@###.###
      ======================================================================

            apalanissunw Anand Palaniswamy (Inactive)
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: