Merge pull request #2942 from xuwenping123/v5_dev_compile

add source code for dynamic compile
This commit is contained in:
Golden Looly
2023-03-03 20:41:14 +08:00
committed by GitHub
3 changed files with 32 additions and 3 deletions

View File

@@ -28,6 +28,11 @@ class JavaSourceFileObject extends SimpleJavaFileObject {
*/
private InputStream inputStream;
/**
* Source code.
*/
private String sourceCode;
/**
* 构造支持File等路径类型的源码
*
@@ -82,9 +87,12 @@ class JavaSourceFileObject extends SimpleJavaFileObject {
*/
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
try(final InputStream in = openInputStream()){
return IoUtil.readUtf8(in);
if (sourceCode == null) {
try(final InputStream in = openInputStream()){
sourceCode = IoUtil.readUtf8(in);
}
}
return sourceCode;
}
}
}

View File

@@ -40,4 +40,17 @@ public class JavaSourceCompilerTest {
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);
}
}
}

View File

@@ -0,0 +1,8 @@
package error;
public class ErrorClazz {
public static void 123main(String[] args) {
System.out.println("hello world");
}
}