Notes about the evaluation of expressions at run-time with Java. (eval, Java, expression, expressions, evaluation at run-time; java string evaluation;), notes, page 722123
https://www.purl.org/stefan_ram/pub/evaluating-expressions-with-java (permalink) is the canonical URI of this page.
Stefan Ram

Evaluating Expression with Java 

These are some notes about evaluating expressions in Java. This is not a lesson explaining every detail from the beginning.

Evaluating Expression by Compiling and Running them

The following program was tested using the Java SE 6 JDK.

Main.java

public class Main
{
public static void main
( final java.lang.String[] args )
throws java.lang.Throwable
{ java.lang.System.out.printf( "%.02f%n", System.nanoTime() / 1E9 );
java.lang.System.out.println( eval( "new java.lang.Boolean( true )" ));
java.lang.System.out.printf( "%.02f%n", System.nanoTime() / 1E9 );
java.lang.System.out.println( eval( "2 + 3" ));
java.lang.System.out.printf( "%.02f%n", System.nanoTime() / 1E9 ); }

public static boolean writeSource
( final java.lang.String sourcePath,
final java.lang.String sourceCode )
throws java.io.FileNotFoundException
{ java.io.PrintWriter writer =
new java.io.PrintWriter( sourcePath );
writer.println( sourceCode );
writer.close(); return true; }

public static boolean compile
( final java.lang.String sourcePath )
throws java.io.IOException
{ final javax.tools.JavaCompilerTool compiler =
javax.tools.ToolProvider.defaultJavaCompiler();
final javax.tools.JavaFileManager manager =
compiler.getStandardFileManager();
final javax.tools.JavaFileObject source =
manager.getFileForInput( sourcePath ); /* java.io.IOException */
final javax.tools.JavaCompilerTool.CompilationTask task =
compiler.run( null, source );
return task.getResult(); }

public static final java.lang.Class loadExpression
( final java.lang.String path, final java.lang.String className )
throws
java.net.MalformedURLException,
java.lang.ClassNotFoundException
{ /* java.net.MalformedURLException */
final java.net.URLClassLoader myLoader =
new java.net.URLClassLoader
( new java.net.URL[]{ new java.io.File( path ).toURI().toURL() });
/* java.lang.ClassNotFoundException */
return java.lang.Class.forName( className, true, myLoader ); }

public static java.lang.Object evalExpression
( final java.lang.Class test )
throws
java.lang.NoSuchMethodException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
{ final java.lang.Class[] parameterType = null;
/* java.lang.NoSuchMethodException */
final java.lang.reflect.Method method =
test.getMethod( "expression", parameterType );
final java.lang.Object[] argument = null; Object instance = null;
/* java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException */
return method.invoke( instance, argument ); }

public static java.lang.Object eval
( final java.lang.String expression )
throws
java.io.FileNotFoundException,
java.io.IOException,
java.net.MalformedURLException,
java.lang.ClassNotFoundException,
java.lang.NoSuchMethodException,
java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
{ final java.lang.Object result;
final java.lang.String path = "c:/";
final java.lang.String className = "ExpressionWrapper";
final java.lang.String sourcePath = path + className + ".java";
writeSource( /* to */ sourcePath,
"public final class " + className + "\n" +
"{ public static java.lang.Object expression()\n" +
" { return " + expression + "; }}\n" );
if( compile( sourcePath ))
{ final java.lang.Class class_ =
loadExpression( /* from */ path, className );
result = evalExpression( class_ ); }
else { result = null; }
return result; }}

System.out
33432,94
true
33441,99
5
33443,41
Aussprachehinweis
close cloz

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram722123 stefan_ram:722123 eval, Java, expression, expressions, evaluation at run-time; java string evaluation; java string evaluation: free, online, on-line, on line, download, down load, english, information, service, server, about, keyword, keywords, key word, keywords, internet, web, www, world wide web, experience, application, it, 2002, 2003, 2004, 2005, 2006, 2007 what is, what are, contents, html, xhtml, digital, electronic, general, intro, introduction, course, article, talk, lecture, lectures, lecture note, lecture notes, seminar, training, free teaching material, free teaching materials, teaching unit, teaching units, distance education, instruction, schooling, advanced training, continuing education, further education, further training, vocational training, education and training, course of instruction, preparatory training, course handout, hand out, trainer, didactics, class, classes, school, tuition, apprenticeship training, day release, theoretical training for apprentices, primer, howto, how-to, how to, textbook, schoolbook, book, books, specialised book, report, tutorial, tutorials, teacher, consulter, advisor, guidance, instruction, instructions, manual, work, reference, solution, solutions, definition of, laymans explanation, explanations, about the topic, FAQ, FAQs, learn, notion, word explanation, example, school, preparation, paper, presentation, hint, tips and tricks, method, methodology, functionality, composition, design, developement, structure, principle, basis, foundation, foundations, structure, structures, question, questions, answer, answers, first step, first steps, overview, first steps, online learning, learn and understand, Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd722123, slrprddef722123, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Copyright 1998-2014 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/evaluating-expressions-with-java