[hxJava] reflection without java.lang.reflection

32 views
Skip to first unread message

Adrian Veith

unread,
Mar 5, 2015, 8:16:41 AM3/5/15
to haxe...@googlegroups.com
Hi,

I currently run into the problem that java.lang.reflection is not supported by every Java target (in my case currently codenameone for iOS, but also J2ME). As far as I understand from the generated sources, all descendants of HxObject already have reflection capabilities without dependencies to java.lang.reflection. 
Now is it possible to generate Java code without a dependency to java.lang.reflection ?

If not, I was directed to javassist which does compile time reflection by patching the .class file. This seems to increase the performance of reflection on the Java side as well. Would it be enough to patch Runtime.hx and Types.hx to use javassist.tools.reflect instead of java.lang.reflect ? 
Which class files in this case need to be patched with javassist ? - I am asking here, because I am not clear if we need the dependency to java.lang.reflection for the HxObjects or for native Java objects. I need at least Reflection for Haxe remoting to work.

thanks for any help.

cheers Adrian.

Cauê Waneck

unread,
Mar 5, 2015, 8:35:03 AM3/5/15
to haxe...@googlegroups.com
Hi!

As you well observed, most of the types generated by Haxe are generated with a reflection helper, so they don't need to go through the very slow Java reflection. There are some cases where even on Haxe generated classes, we need reflection. For example, when accessing a static class field (e.g. `var m:Dynamic = Math; trace(m.abs(-10));` ) or when resolving classes (e.g. `var type = Type.resolveClass("pack.to.Class");`).

All reflection code should be contained at std/java/internal/Runtime.hx, std/java/_std/Type.hx, std/java/_std/Reflect.hx . If may patch a version that uses javassist or a straight-out throw "Not supported", and see how this works for you. For serialization (remoting) to work, you will need at least `Type.resolveClass` / `Type.resolveEnum` / `Type.createEmpty` (which needs to access a static field in the class - even on classes that derive from HxObject) - and others.

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Adrian Veith

unread,
Mar 5, 2015, 11:58:44 AM3/5/15
to haxe...@googlegroups.com
Hi Caue, 
thanks again. I have stripped down Type.hx that there is no dependency to java.lang.reflect anymore (use javassist instead in most cases). Now I have to test if it works.

cheers, Adrian.
Reply all
Reply to author
Forward
0 new messages