1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package compiler.lib.compile_framework;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
/**
* This is the entry-point for the Compile Framework. Its purpose it to allow
* compilation and execution of Java and Jasm sources generated at runtime.
*
* <p> Please reference the README.md for more details and examples.
*/
public class CompileFramework {
private final List<SourceCode> javaSources = new ArrayList<>();
private final List<SourceCode> jasmSources = new ArrayList<>();
private final Path sourceDir = Utils.makeUniqueDir("compile-framework-sources-");
private final Path classesDir = Utils.makeUniqueDir("compile-framework-classes-");
private ClassLoader classLoader;
/**
* Set up a new Compile Framework instance, for a new compilation unit.
*/
public CompileFramework() {}
/**
* Add a Java source to the compilation.
*
* @param className Class name of the class (e.g. "{@code p.xyz.YXZ}").
* @param code Java code for the class, in the form of a {@link String}.
*/
public void addJavaSourceCode(String className, String code) {
javaSources.add(new SourceCode(className, "java", code));
}
/**
* Add a Jasm source to the compilation.
*
* @param className Class name of the class (e.g. "{@code p.xyz.YXZ}").
* @param code Jasm code for the class, in the form of a {@link String}.
*/
public void addJasmSourceCode(String className, String code) {
jasmSources.add(new SourceCode(className, "jasm", code));
}
/**
* Compile all sources: store the sources to the {@link sourceDir} directory, compile
* Java and Jasm sources and store the generated class-files in the {@link classesDir}
* directory.
*/
public void compile() {
if (classLoader != null) {
throw new CompileFrameworkException("Cannot compile twice!");
}
Utils.printlnVerbose("------------------ CompileFramework: SourceCode -------------------");
Utils.printlnVerbose(sourceCodesAsString(jasmSources));
Utils.printlnVerbose(sourceCodesAsString(javaSources));
System.out.println("------------------ CompileFramework: Compilation ------------------");
System.out.println("Source directory: " + sourceDir);
System.out.println("Classes directory: " + classesDir);
Compile.compileJasmSources(jasmSources, sourceDir, classesDir);
Compile.compileJavaSources(javaSources, sourceDir, classesDir);
classLoader = ClassLoaderBuilder.build(classesDir);
}
private static String sourceCodesAsString(List<SourceCode> sourceCodes) {
StringBuilder builder = new StringBuilder();
for (SourceCode sourceCode : sourceCodes) {
builder.append("SourceCode: ").append(sourceCode.filePathName()).append(System.lineSeparator());
builder.append(sourceCode.code()).append(System.lineSeparator());
}
return builder.toString();
}
/**
* Access a class from the compiled code.
*
* @param name Name of the class to be retrieved.
* @return The class corresponding to the {@code name}.
*/
public Class<?> getClass(String name) {
try {
return Class.forName(name, true, classLoader);
} catch (ClassNotFoundException e) {
throw new CompileFrameworkException("Class not found:", e);
}
}
/**
* Invoke a static method from the compiled code.
*
* @param className Class name of a compiled class.
* @param methodName Method name of the class.
* @param args List of arguments for the method invocation.
* @return Return value from the invocation.
*/
public Object invoke(String className, String methodName, Object[] args) {
Method method = findMethod(className, methodName);
try {
return method.invoke(null, args);
} catch (IllegalAccessException e) {
throw new CompileFrameworkException("Illegal access:", e);
} catch (InvocationTargetException e) {
throw new CompileFrameworkException("Invocation target:", e);
}
}
private Method findMethod(String className, String methodName) {
Class<?> c = getClass(className);
Method[] methods = c.getDeclaredMethods();
Method method = null;
for (Method m : methods) {
if (m.getName().equals(methodName)) {
if (method != null) {
throw new CompileFrameworkException("Method name \"" + methodName + "\" not unique in class \n" + className + "\".");
}
method = m;
}
}
if (method == null) {
throw new CompileFrameworkException("Method \"" + methodName + "\" not found in class \n" + className + "\".");
}
return method;
}
/**
* Returns the classpath appended with the {@link classesDir}, where
* the compiled classes are stored. This enables another VM to load
* the compiled classes. Note, the string is already backslash escaped,
* so that Windows paths which use backslashes can be used directly
* as strings.
*
* @return Classpath appended with the path to the compiled classes.
*/
public String getEscapedClassPathOfCompiledClasses() {
return Utils.getEscapedClassPathAndClassesDir(classesDir);
}
}
|