可以生成可執行的class文件
直接上能執行代碼:
代碼如下:
public class Test {
/**
* @param args
*/
@SuppressWarnings("static-access")
public static void main(String[] args) {
try {
new Test().calculate("234 - ( 1 + 45 * 4 ) / 5");
} catch (Exception e) {
e.printStackTrace();
}
}
private static double calculate(String expr) throws Exception {
String className = "CalculatorMain";
String methodName = "calculate";
String source = "class " + className
+ " { public static void main(String[] args){System.out.println(calculate());} public static double " + methodName + "() { return "
+ expr + "; } }";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(
null, null, null);
StringSourceJavaObject sourceObject = new Test.StringSourceJavaObject(
"Main", source);
Iterable<? extends JavaFileObject> fileObjects = Arrays
.asList(sourceObject);
CompilationTask task = compiler.getTask(null, fileManager, null, null,
null, fileObjects);
boolean result = task.call();
if (result) {
ClassLoader loader = Thread.currentThread().getClass()
.getClassLoader();
try {
Class<?> clazz = loader.loadClass(className);
Method method = clazz.getMethod(methodName, new Class<?>[] {});
Object value = method.invoke(null, new Object[] {});
return (Double)value;
} catch (Exception e) {
// throw new CalculationException("內部錯誤。");
}
} else {
// / new CalculationException("錯誤的表達式。");
}
return 0;
}
static class StringSourceJavaObject extends SimpleJavaFileObject {
private String content = null;
public StringSourceJavaObject(String name, String content)
throws URISyntaxException {
super(URI.create("string:///" + name.replace('.', '/')
+ Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors)
throws IOException {
return content;
}
}
}