SourceBuddy Brings Eval To Java

submited by
Style Pass
2023-01-24 06:00:08

SourceBuddy is a Java library that compiles and loads dynamically generated Java source code. This has the advantage of providing Java with an eval facility such as those found in interpreted languages.

Languages like Perl and Javascript have eval  for evaluating code at runtime that gets passed into the function as a plain string. For instance :

Caveat only, to be extra cautious when needing to evaluate user supplied code; you don't want to compile and run a 'system -rf' do you?

foreach $module in (@modulesArray) { use lib "$FindBin::Bin/modules"; eval "use $module"; if ($@) {   die "can't load $module";   } }

Java gets that kind of functionality now too thanks to SourceBuddy, a Java source compiler facade in front of the JDK-provided javac compiler.

With SourceBuddy you can compile Java source code you created dynamically, in your Java application. Your program can create the source code of one or more Java classes, pass the strings to SourceBuddy and then use the classes. An example code is the following:

Leave a Comment