-
-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
another possible and portable way to instantiate a class #23
Comments
Comment #1 originally posted by henri-tremblay on 2013-10-06T10:55:40.000Z: Hi, that sounds interesting. Would be able to provide a patch containing this instantiator? |
Comment #2 originally posted by henri-tremblay on 2013-10-06T11:47:18.000Z: 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); You can see that the invocatio of the super() is explicitly coded into the file as
If you create the JVM code using asm tools, like cglib does then you can skip this. |
Comment #3 originally posted by henri-tremblay on 2013-10-06T11:52:35.000Z: Wouldn't the JVM's bytecode verifier reject the class? |
Comment #4 originally posted by henri-tremblay on 2013-10-06T11:55:53.000Z: One should give a try. My bet is that it will not. |
Comment #5 originally posted by henri-tremblay on 2013-10-06T12:15:25.000Z: 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. |
Indeed, just removing the constructor of the super class causes a VerifyError. I will now try to call the constructor of Object. |
In fact, it does work when |
A similar approach to this one is to synthesize the I have a working prototype of this, and despite not having tested it in any platform other than Hotspot, it seems to work great. |
(I know this is not portable, just wanted to share another approach that I haven't seen explored yet) |
MagicAccessorImpl is package scope. You generate the ObjectInstantiator? Can you share the prototype in a gist or github repo? |
Objenesis isn't supposed to have portable instantiators. It is supposed to work on any platform. Right now, the only none working platform I am aware of if Google App Engine. |
You can find the prototype here. The interesting part is in the MagicInstantiatorStrategy class. I am using ASM to generate the |
How cute. You can generate a class extending it but not extend it from source code :-) Thanks for the code. I will try it ASAP. Can you tell me why or on which platform we are using it? (so why the classical instantiators where not working) |
I wrote this when searching for an alternative to Sun's It was only when I saw you attempting this and failing that I ported it to Objenesis. It will probably only work on the same platforms as ReflectionFactory, it is simply another approach. |
I've forked your idea in a issue #35 |
Original issue 20 created by henri-tremblay on 2013-08-16T08:01:07.000Z:
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.
The text was updated successfully, but these errors were encountered: