Reflection for Java REPL

65 views
Skip to first unread message

reportm...@gmail.com

unread,
Aug 4, 2022, 3:10:21 PM8/4/22
to TeaVM
My latest Java/SnapKit/TeaVM project is a Java notebook application. I want to be able to type in Java statements and expressions and execute them in an interpreter. This obviously relies heavily on Java reflection, which is not automatically available with TeaVM. The good news is that I can 'whitelist' any JRE method with just a couple lines of code in a tedious static fashion.

But before I try to figure out how to automatically generate a huge source file that implements all my whitelist JRE methods, I thought I would check and see if anyone else had any ideas. I know Alexey had a great example of compiling Java source in the browser, so I have some hope there is another possible solution. I don't mind if this balloons my download. Most of my TeaVM apps have been ~ 1MB gzip, and it would be just fine if this app was more like 10MB.

Below is what I have so far. I know there are other JShell options out there in the browser, but I want mine to soon support interactive SnapCharts and UI in the browser, so running JShell as a client/server isn't helpful.

JavaShell.png


ScraM Team

unread,
Aug 4, 2022, 8:47:47 PM8/4/22
to TeaVM
First off, this is very cool.  You could build some sort of coding practice site out of this.

I tried Math.max() and quickly found one of the methods needing importing.  :-)

I don't have a shortcut idea other than what you mentioned, automatically generating a list of wrappers/proxies.

Maybe there's a way to tell TeaVM to _not_ cull methods during DCE, but that would probably require code changes.

Jeff Martin

unread,
Aug 5, 2022, 9:53:05 AM8/5/22
to ScraM Team, TeaVM
First off, this is very cool.  You could build some sort of coding practice site out of this.

Thanks! That is a great idea. It might be interesting to tie this in with Project Euler (https://projecteuler.net). Mostly I’m interested in doing something like Matlab or Jupyter, but with Java instead of Python. In that sense it could make for an interesting IDE in the browser, for very targeted development (switching over to the desktop version when things get too demanding).

I tried Math.max() and quickly found one of the methods needing importing.  :-)

Yep, so far I’ve only manually added support for System.out.println() and String.replace(), as seen in the sample.

I was hoping I could trigger reflection just by adding lines like this to a whitelist source file:

String.class.getMethod(“replace”, String.class, String.class);
PrintStream.class.getMethod(“print”, Object.class);
PrintStream.class.getMethod(“println”, Object.class);
Math.class.getMethod(“min”, double.class, double.class);
Math.class.getMethod(“max”, double.class, double.class);

But this doesn’t seem to make things like Class getDeclaredMethods() and getMethod(name, params) work. I have actually been refactoring stuff like this out of my code for years, because I though it was leading TeaVM to include a bunch of bloat.

My JavaParser library actually has a shadow set of classes that allow me to do Reflection with and without built-in Java reflection, since it assumes that classes can get loaded and reloaded. So I can work around this. But since it seems like TeaVM has given Reflection significant work and thought, I thought I’d check to see if there is some built in way to get those Class methods to work.

jeff


--
You received this message because you are subscribed to a topic in the Google Groups "TeaVM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/teavm/v_e2VTaP_j4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to teavm+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/teavm/e817bff9-1409-489b-b51f-a00bab863b91n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages