Skip to content

Commit

Permalink
jOOQ#100: Prototype for compiling multiple classes in one go.
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Mar 27, 2022
1 parent 540c9ad commit 9da98fc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
39 changes: 33 additions & 6 deletions jOOR/src/main/java/org/joor/CompilationUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> files = new LinkedHashMap<>();

/**
* The result of the compilation that holds mapping for each className -> class.
*/
public static class Result {
private final Map<String, Class<?>> 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<String> 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<String, String> getFiles() {
Map<String, String> getInput() {
return files;
}
}
6 changes: 3 additions & 3 deletions jOOR/src/main/java/org/joor/MultiCompile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9da98fc

Please sign in to comment.