Menu

IllegalAccessException using reflection

Tom
2005-03-01
2012-11-28
  • Tom

    Tom - 2005-03-01

    Hello,

    I am using reflection to do the following two steps in a bundle:
    - create an SSLServerSocket via SSLServerSocketFactory.create...
    - on the returned object, invoke a public method "setNeedClientAuthentication".

    Both of the SSLServerSocketFactory and Server Socket are exposed by the JRE (not exported by a bundle).

    Creating the socket works fine, then the following code:
    Method auth = socket.getClass().getMethod("setNeedClientAuth",
                                    new Class[] {boolean.class});
    auth.invoke(socket, new Object[] {requireClientAuth});

    results in this Exception:
    IllegalAccessException:
    Class org.knopflerfish.bundle.http.SocketListener can not access a member of class com.sun.net.ssl.internal.ssl.SSLServerSocketImpl with modifiers "public"

    Using "doPrivileged" did not help.

    Using explicit casting instead of reflection did help. But I want to avoid non-ee.foundation imports.

    Thanks
    Tom

     
    • Per

      Per - 2005-03-01

      Do it this way instead:

      Class sslSocket = Class.forName("javax.net.ssl.SSLServerSocket");
      Method auth = sslSocket.getMethod("setNeedClientAuth", new Class[] {Boolean.TYPE});
      auth.invoke(socket, new Object[] {requireClientAuth});

      /Per

       
      • Tom

        Tom - 2005-03-01

        Thanks, you are right.

        To summarize:
        - when using the abstract class "SSLServerSocket" to find the method "setNeedClientAuth(boolean)" to invoke it works.
        - when using the derived class "com.sun.net.ssl.internal.SSLServerSocketImpl" to find the method to invoke, there is an IllegalAccessException. (even though the method exists in this class and is public)

        Could anyone explain the logic behind this?
        Thanks

         
    • Per

      Per - 2005-03-04

      I suspect it's because SSLServerSocketImpl isn't a public class even though "setNeedClientAuth" is a public method in that class (it has to since it's declared public in SSLServerSocket).
      I.e. you cannot do:
      import com.sun.net.ssl.internal.ssl.SSLServerSocketImpl;

       

Log in to post a comment.

MongoDB Logo MongoDB