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

Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions

    XMLWordPrintable

Details

    • b87
    • x86
    • linux

    Description

      Name: rmT116609 Date: 05/13/2002


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


      FULL OPERATING SYSTEM VERSION :
      Red Hat Linux release 6.2 (Zoot)
      Linux ace 2.2.17-14 #1 Mon Feb 5 17:53:36 EST 2001 i686 unknown
      glibc-2.1.3-22

      The problem is also reproducible on Windows 2000 using JDK1.4.0.


      A DESCRIPTION OF THE PROBLEM :
      The problem occurs when the Introspector class to inspect a non-public concrete class that inherits and overrides property methods from a public base class. The Introspector finds the property methods in the non-public class, and reports those in its descriptors. This leads to unnecessary IllegalAccessExceptions when tools (in a separate package) attempt to invoke the reported Methods.

      The correct behavior would be to continue up the hierarchy if property methods (even public ones) are discovered in a non-public class. If a public superclass declares the same public methods, those methods should be reported in BeanInfo
      descriptors. (Otherwise, the methods from the non-public class can be reported, as any IllegalAccessExceptions external tools see will be the legitimate and unavoidable consequence of the methods not being accessable outside the
      package.)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Define and compile the classes provided in the Source Code section (in appropiate pkg1 and pkg2 directories);
      2. Execute "java pkg2.IntrospectApp"


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      You'll see an IllegalAccessException, even though the getIntProperty() method you are trying to invoke should be accessible to you. It is clear why this happens, but it is useless and unnecessary, and would be avoided with modest
      changes to java.beans.Introspector.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.IllegalAccessException: Class pkg2.IntrospectApp can not access a
      member of class pkg1.NonPublicConcreteClass with modifiers "public"
      at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
      at java.lang.reflect.Method.invoke(Method.java:317)
      at pkg2.IntrospectApp.main(IntrospectApp.java:18)


      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package pkg1;

      public abstract class PublicAbstractClass
      {
          public static PublicAbstractClass createBean()
          { return new NonPublicConcreteClass(); }

          public abstract int getIntProperty();
      }

      ---

      package pkg1;

      class NonPublicConcreteClass extends PublicAbstractClass
      {
          public int getIntProperty()
          { return 42; }
      }

      ---

      package pkg2;

      import java.beans.*;
      import pkg1.*;

      class IntrospectApp
      {
          public static void main( String[] argv )
          {
      try
      {
      Object bean = PublicAbstractClass.createBean();
      BeanInfo bi = Introspector.getBeanInfo( bean.getClass() );
      PropertyDescriptor[] pds = bi.getPropertyDescriptors();
      for (int i = 0, len = pds.length; i < len; ++i)
      {
      PropertyDescriptor pd = pds[i];
      Object propVal =
      pd.getReadMethod().invoke( bean, new Object[]{} );
      System.out.println( pd.getName() + " --> " + propVal );
      }
      }
      catch ( Exception e )
      { e.printStackTrace(); }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      You can call setAccessible(true) on the methods of the non-public class you wish to invoke (at cost of increasing the trust level your application requires);

      You can expose your class, change its accessibility from non-public to public (at cost of allowing external dependencies on something you'd rather be able to mess with internally)
      (Review ID: 146463)
      ======================================================================

      Attachments

        Activity

          People

            malenkov Sergey Malenkov (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: