mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add source code for dynamic compile
This commit is contained in:
parent
f5cb27a608
commit
f0d94c9806
@ -28,6 +28,11 @@ class JavaSourceFileObject extends SimpleJavaFileObject {
|
|||||||
*/
|
*/
|
||||||
private InputStream inputStream;
|
private InputStream inputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Source code.
|
||||||
|
*/
|
||||||
|
private String sourceCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造,支持File等路径类型的源码
|
* 构造,支持File等路径类型的源码
|
||||||
*
|
*
|
||||||
@ -82,9 +87,12 @@ class JavaSourceFileObject extends SimpleJavaFileObject {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
||||||
|
if (sourceCode == null) {
|
||||||
try(final InputStream in = openInputStream()){
|
try(final InputStream in = openInputStream()){
|
||||||
return IoUtil.readUtf8(in);
|
sourceCode = IoUtil.readUtf8(in);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return sourceCode;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -40,4 +40,17 @@ public class JavaSourceCompilerTest {
|
|||||||
Assert.assertTrue(String.valueOf(obj).startsWith("c.C@"));
|
Assert.assertTrue(String.valueOf(obj).startsWith("c.C@"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testErrorCompile() {
|
||||||
|
Exception exception = null;
|
||||||
|
try {
|
||||||
|
CompilerUtil.getCompiler(null)
|
||||||
|
.addSource(FileUtil.file("test-compile/error/ErrorClazz.java"))
|
||||||
|
.compile();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
exception = ex;
|
||||||
|
} finally {
|
||||||
|
Assert.assertTrue(exception instanceof CompilerException);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package error;
|
||||||
|
|
||||||
|
public class ErrorClazz {
|
||||||
|
|
||||||
|
public static void 123main(String[] args) {
|
||||||
|
System.out.println("hello world");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user