Issue 20 in objenesis: another possible and portable way to instantiate a class

3 views
Skip to first unread message

obje...@googlecode.com

unread,
Aug 16, 2013, 4:01:21 AM8/16/13
to objene...@googlegroups.com
Status: New
Owner: ----

New issue 20 by peter.ve...@gmail.com: another possible and portable way to
instantiate a class
http://code.google.com/p/objenesis/issues/detail?id=20

If the original class is not final you can create a class that extends it,
creating direct JVM byte-code, and loading the created class. Then creating
an instance of the extended class will invoke the constructor of the
extending class, but since the JVM code is generated calling the
superconstructor can be skipped (simply not generating the code into the
constructor that invokes super).

You may perhaps even create the extending class if the original class is
final accessing the original class object already loaded and altering
the "final" modifier using reflection.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

obje...@googlecode.com

unread,
Oct 6, 2013, 6:55:53 AM10/6/13
to objene...@googlegroups.com

Comment #1 on issue 20 by henri.tr...@gmail.com: another possible and
portable way to instantiate a class
http://code.google.com/p/objenesis/issues/detail?id=20

Hi, that sounds interesting. Would be able to provide a patch containing
this instantiator?

obje...@googlecode.com

unread,
Oct 6, 2013, 7:47:28 AM10/6/13
to objene...@googlegroups.com

Comment #2 on issue 20 by peter.ve...@gmail.com: another possible and
portable way to instantiate a class
http://code.google.com/p/objenesis/issues/detail?id=20

No unfoirtunately I am not, having no time to devote to that.

If you look at the disassembly of a class file, for example:

public javax0.casuar.Node(java.lang.String);
flags: ACC_PUBLIC
Code:
stack=4, locals=3, args_size=2
0: aload_0
1: invokespecial #75 // Method
java/lang/Object."<init>":()V
4: aload_0
5: aconst_null
6: putfield #59 // Field file:Ljava/io/File;
9: aload_0
10: aconst_null
11: putfield #36 // Field
subdirs:[Ljava/io/File;
14: aload_0
15: aconst_null
16: putfield #42 // Field
children:[Ljavax0/casuar/Node;
19: aload_1
20: ifnonnull 32
23: ldc #76 // String user.home
25: invokestatic #78 // Method
java/lang/System.getProperty:(Ljava/lang/String;)Ljava/lang/String;
28: astore_2
29: goto 34
32: aload_1
33: astore_2
34: aload_0
35: new #45 // class java/io/File
38: dup
39: aload_2
40: invokespecial #84 // Method
java/io/File."<init>":(Ljava/lang/String;)V
43: putfield #59 // Field file:Ljava/io/File;
46: new #85 // class
javax0/casuar/Node$SubdirCollector
49: dup
50: aload_0
51: dup
52: invokevirtual #87 // Method
java/lang/Object.getClass:()Ljava/lang/Class;
55: pop
56: aconst_null
57: invokespecial #91 // Method
javax0/casuar/Node$SubdirCollector."<init>":(Ljavax0/casuar/Node;Ljavax0/casuar/Node$SubdirCollector;)V
60: invokevirtual #94 // Method
javax0/casuar/Node$SubdirCollector.start:()V
63: return

You can see that the invocatio of the super() is explicitly coded into the
file as

0: aload_0
1: invokespecial #75 // Method
java/lang/Object."<init>":()V

If you create the JVM code using asm tools, like cglib does then you can
skip this.

obje...@googlecode.com

unread,
Oct 6, 2013, 7:52:50 AM10/6/13
to objene...@googlegroups.com

Comment #3 on issue 20 by nat.pr...@gmail.com: another possible and
portable way to instantiate a class
http://code.google.com/p/objenesis/issues/detail?id=20

Wouldn't the JVM's bytecode verifier reject the class?

obje...@googlecode.com

unread,
Oct 6, 2013, 7:56:00 AM10/6/13
to objene...@googlegroups.com

Comment #4 on issue 20 by peter.ve...@gmail.com: another possible and
portable way to instantiate a class
http://code.google.com/p/objenesis/issues/detail?id=20

One should give a try. My bet is that it will not.

obje...@googlecode.com

unread,
Oct 6, 2013, 8:15:31 AM10/6/13
to objene...@googlegroups.com

Comment #5 on issue 20 by nat.pr...@gmail.com: another possible and
portable way to instantiate a class
http://code.google.com/p/objenesis/issues/detail?id=20

Have you tried?

I would be surprised if the bytecode verifier did not reject the class when
the classloader defined the class from the invalid bytecode.
Reply all
Reply to author
Forward
0 new messages