14장 컴파일링과 스크립팅

Jasik·2021년 12월 14일
0

컴파일러 호출

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
OutputStream = outStream = ...;
OutputStream = errStream = ...;
int result = compilter.run(null, outStream, errStream, "-sourcepath", "src", "Test.java");

result 가 0이면 컴파일 성공

컴파일 태스크 실행

JavaCompilter.CompilationTask task = compiler.getTask(
    errorWriter,
    fileManager,
    diagnostics,
    options, // ex) Arrays.asList("-d", "bin")
    classes,
    sources
);

sources 얻기

StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles("File1.java", "File2.java");
JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, options, null, sources);

// 컴파일 시작
Boolean success = task.call();

메모리에서 소스파일 읽기

public class StringSource extends SimpleJavaFileObject {
    private String code;

    StringSource(String name, String code) {
        super(URI.create("string:///" + name.replace('.', '/') + ".java"), Kind.SOURCE);
        this.code = code;
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
        return code;
    }
}


String pointCode = ...;
String rectangleCode = ...;
List<StringSource> sources = Arrays.asList(
        new StringSource("Point", pointCode),
        new StringSource("Rectangle", rectangleCode)
);
task = compiler.getTask(null, null, null, null, null, sources);
profile
가자~

0개의 댓글