From 9da98fc2d6fecc8a183e9e11ec8f387ae35ed9a9 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Sun, 27 Mar 2022 15:12:53 +0200 Subject: [PATCH] #100: Prototype for compiling multiple classes in one go. --- .../main/java/org/joor/CompilationUnit.java | 39 ++++++++++++++++--- jOOR/src/main/java/org/joor/MultiCompile.java | 6 +-- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/jOOR/src/main/java/org/joor/CompilationUnit.java b/jOOR/src/main/java/org/joor/CompilationUnit.java index ab5b7fd..de2c658 100644 --- a/jOOR/src/main/java/org/joor/CompilationUnit.java +++ b/jOOR/src/main/java/org/joor/CompilationUnit.java @@ -17,45 +17,72 @@ import java.util.Map; import java.util.Set; +/** + * Unit for holding multiple source files to be compiled in one go. + */ public class CompilationUnit { private final Map files = new LinkedHashMap<>(); + /** + * The result of the compilation that holds mapping for each className -> class. + */ public static class Result { private final Map> classes = new LinkedHashMap<>(); - public void addResult(String className, Class clazz) { + void addResult(String className, Class clazz) { classes.put(className, clazz); } + /** + * Gets the compiled class by its class name + * + * @param className the class name + * @return the compiled class + */ public Class getClass(String className) { return classes.get(className); } + /** + * Number of classes in the result + */ public int size() { return classes.size(); } + /** + * Set of the classes by their names + */ public Set getClassNames() { return classes.keySet(); } } - public static CompilationUnit input() { - return new CompilationUnit(); + static CompilationUnit.Result result() { + return new Result(); } - public static CompilationUnit.Result result() { - return new Result(); + /** + * Creates a new compilation unit for holding input files. + */ + public static CompilationUnit input() { + return new CompilationUnit(); } + /** + * Adds input to the compilation unit. + * + * @param className the class name + * @param content the source code for the class + */ public CompilationUnit addClass(String className, String content) { files.put(className, content); return this; } - Map getFiles() { + Map getInput() { return files; } } diff --git a/jOOR/src/main/java/org/joor/MultiCompile.java b/jOOR/src/main/java/org/joor/MultiCompile.java index 2b09195..4b90040 100644 --- a/jOOR/src/main/java/org/joor/MultiCompile.java +++ b/jOOR/src/main/java/org/joor/MultiCompile.java @@ -65,7 +65,7 @@ public static CompilationUnit.Result compileUnit(CompilationUnit unit, CompileOp Lookup lookup = MethodHandles.lookup(); ClassLoader cl = lookup.lookupClass().getClassLoader(); - unit.getFiles().forEach((cn, code) -> { + unit.getInput().forEach((cn, code) -> { try { Class clazz = cl.loadClass(cn); result.addResult(cn, clazz); @@ -139,7 +139,7 @@ public static CompilationUnit.Result compileUnit(CompilationUnit unit, CompileOp for (CharSequenceJavaFileObject f : files) { String className = f.getClassName(); - Class caller = getClassFromIndex(index++); + Class caller = findCompiledClassViaIndex(index++); // If the compiled class is in the same package as the caller class, then // we can use the private-access Lookup of the caller class @@ -180,7 +180,7 @@ public static CompilationUnit.Result compileUnit(CompilationUnit unit, CompileOp } } - private static Class getClassFromIndex(int index) { + private static Class findCompiledClassViaIndex(int index) { StackWalker.StackFrame sf = StackWalker .getInstance(RETAIN_CLASS_REFERENCE) .walk(s -> s