This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: I can make your gcj-compiled binaries much smaller.


Hi,

On Tue, 2003-01-14 at 23:03, Adam Megacz wrote:
> Andrew Haley <aph@redhat.com> writes:
> > I don't believe that's true, and I explained why.  When libgcj uses
> > reflection as a means of resolving references _internally_,
> 
> As I said, Class.forName("foo") will not work unless "foo" is public.
> If "foo" is public, then the app developer can explicitly reference it.

Actually this does always work even if the Class is private or package
local. And if you have enough Security Permissions (or there is no
SecurityManager at all) you can even set the Constructor of such a class
to accessible and you will be able to create an actual object.

Here is an example that creates an instance of the package local
java.net.PlainSocketImpl class.

import java.lang.reflect.Constructor;

public class Reflect
{
  public static void main(String[] args) throws Exception
  {
    Class c = Class.forName("java.net.PlainSocketImpl");
    System.out.println("Class: " + c);
    Constructor con = c.getDeclaredConstructor(null);
    System.out.println("Constructor: " + con);
    con.setAccessible(true); // Look a dirty reflection trick!
    Object o = con.newInstance(null);
    System.out.println("Object: " + o);
  }
}

Cheers,

Mark

P.S. Actually it doesn't work because of a bug in SocketImpl.toString().
Patch below.

--- java/net/SocketImpl.java	25 Sep 2002 05:05:06 -0000	1.11
+++ java/net/SocketImpl.java	14 Jan 2003 22:40:11 -0000
@@ -271,8 +271,9 @@
    */
   public String toString()
   {
-    return "[addr=" + address.toString() + ",port=" + Integer.toString(port)
-      + ",localport=" + Integer.toString(localport) + "]";
+    return "[addr=" + address
+	    + ",port=" + port
+	    + ",localport=" + localport + "]";
   }
 
   /**



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]