Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support providing DiagnosticListener for compiling #141

Open
wants to merge 1 commit into
base: ea
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 100 additions & 48 deletions src/main/java/net/openhft/compiler/CachedCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
@SuppressWarnings("StaticNonFinalField")
public class CachedCompiler implements Closeable {
private static final Logger LOG = LoggerFactory.getLogger(CachedCompiler.class);
/** Writes to {@link System#err} */
private static final PrintWriter DEFAULT_WRITER = new PrintWriter(System.err);
private static final List<String> DEFAULT_OPTIONS = Arrays.asList("-g", "-nowarn");

Expand All @@ -57,16 +58,28 @@ public class CachedCompiler implements Closeable {

private final ConcurrentMap<String, JavaFileObject> javaFileObjects = new ConcurrentHashMap<>();

/**
* Delegates to {@link #CachedCompiler(File, File, List)} with default {@code javac} compilation
* options {@code -g} (generate debug information) and {@code -nowarn}.
*/
public CachedCompiler(@Nullable File sourceDir, @Nullable File classDir) {
this(sourceDir, classDir, DEFAULT_OPTIONS);
}

/**
* @param sourceDir where to write {@code .java} source code files to be compiled; {@code null}
* to not write them to the file system
* @param classDir where to write compiled {@code .class} files; {@code null} to not write them
* to the file system
* @param options {@code javac} compilation options
*/
public CachedCompiler(@Nullable File sourceDir, @Nullable File classDir, @NotNull List<String> options) {
this.sourceDir = sourceDir;
this.classDir = classDir;
this.options = options;
}

@Override
public void close() {
try {
for (MyJavaFileManager fileManager : fileManagerMap.values()) {
Expand All @@ -77,67 +90,58 @@ public void close() {
}
}

/**
* Delegates to {@link #loadFromJava(ClassLoader, String, String, PrintWriter, DiagnosticListener)}.
* <ul>
* <li>The class loader of {@link CachedCompiler} is used for defining and loading the class
* <li>Only error diagnostics are collected, and are written to {@link System#err}
* </ul>
*/
public Class<?> loadFromJava(@NotNull String className, @NotNull String javaCode) throws ClassNotFoundException {
return loadFromJava(getClass().getClassLoader(), className, javaCode, DEFAULT_WRITER);
}

/**
* Delegates to {@link #loadFromJava(ClassLoader, String, String, PrintWriter, DiagnosticListener)}.
* Only error diagnostics are collected, and are written to {@link System#err}.
*/
public Class<?> loadFromJava(@NotNull ClassLoader classLoader,
@NotNull String className,
@NotNull String javaCode) throws ClassNotFoundException {
return loadFromJava(classLoader, className, javaCode, DEFAULT_WRITER);
}

@NotNull
Map<String, byte[]> compileFromJava(@NotNull String className, @NotNull String javaCode, MyJavaFileManager fileManager) {
return compileFromJava(className, javaCode, DEFAULT_WRITER, fileManager);
}

@NotNull
Map<String, byte[]> compileFromJava(@NotNull String className,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved this method down a few lines so that all loadFromJava methods are grouped together.

@NotNull String javaCode,
final @NotNull PrintWriter writer,
MyJavaFileManager fileManager) {
Iterable<? extends JavaFileObject> compilationUnits;
if (sourceDir != null) {
String filename = className.replaceAll("\\.", '\\' + File.separator) + ".java";
File file = new File(sourceDir, filename);
writeText(file, javaCode);
if (s_standardJavaFileManager == null)
s_standardJavaFileManager = s_compiler.getStandardFileManager(null, null, null);
compilationUnits = s_standardJavaFileManager.getJavaFileObjects(file);

} else {
javaFileObjects.put(className, new JavaSourceFromString(className, javaCode));
compilationUnits = new ArrayList<>(javaFileObjects.values()); // To prevent CME from compiler code
}
// reuse the same file manager to allow caching of jar files
boolean ok = s_compiler.getTask(writer, fileManager, new DiagnosticListener<JavaFileObject>() {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
writer.println(diagnostic);
}
}
}, options, null, compilationUnits).call();

if (!ok) {
// compilation error, so we want to exclude this file from future compilation passes
if (sourceDir == null)
javaFileObjects.remove(className);

// nothing to return due to compiler error
return Collections.emptyMap();
} else {
Map<String, byte[]> result = fileManager.getAllBuffers();

return result;
}
/**
* Delegates to {@link #loadFromJava(ClassLoader, String, String, PrintWriter, DiagnosticListener)}.
* Only error diagnostics are collected, and are written to {@code writer}.
*/
public Class<?> loadFromJava(@NotNull ClassLoader classLoader,
@NotNull String className,
@NotNull String javaCode,
@Nullable PrintWriter writer) throws ClassNotFoundException {
return loadFromJava(classLoader, className, javaCode, writer, null);
}

/**
* Gets a previously compiled and loaded class, or compiles the given Java code and
* loads the class.
*
* @param classLoader class loader for defining and loading the class
* @param className binary name of the class to load, for example {@code com.example.MyClass$Nested}
* @param javaCode Java code to compile, in case the class had not been compiled and loaded before
* @param writer writer for compilation information and diagnostics (should be thread-safe);
* when {@code null} defaults to writing to {@link System#err}
* @param diagnosticListener listener for diagnostics emitted by the compiler (should be thread-safe);
* when {@code null}, error diagnostics are written to the {@code writer}, other diagnostics are ignored
* @return the loaded class
* @throws ClassNotFoundException if compiling or loading the class failed; inspect {@code writer} or
* {@code diagnosticListener} for additional details
*/
public Class<?> loadFromJava(@NotNull ClassLoader classLoader,
@NotNull String className,
@NotNull String javaCode,
@Nullable PrintWriter writer) throws ClassNotFoundException {
@Nullable PrintWriter writer,
@Nullable DiagnosticListener<? super JavaFileObject> diagnosticListener) throws ClassNotFoundException {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not completely sure about the ? super JavaFileObject; maybe users make assumptions then about the JavaFileObject of the diagnostics, which are actually implementation details of Java-Runtime-Compiler, e.g. the file paths.

Class<?> clazz = null;
Map<String, Class<?>> loadedClasses;
synchronized (loadedClassesMap) {
Expand All @@ -147,17 +151,29 @@ public Class<?> loadFromJava(@NotNull ClassLoader classLoader,
else
clazz = loadedClasses.get(className);
}
PrintWriter printWriter = (writer == null ? DEFAULT_WRITER : writer);

if (clazz != null)
return clazz;

PrintWriter printWriter = writer == null ? DEFAULT_WRITER : writer;
if (diagnosticListener == null) {
diagnosticListener = new DiagnosticListener<JavaFileObject>() {
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
printWriter.println(diagnostic);
}
}
};
}

MyJavaFileManager fileManager = fileManagerMap.get(classLoader);
if (fileManager == null) {
StandardJavaFileManager standardJavaFileManager = s_compiler.getStandardFileManager(null, null, null);
fileManager = getFileManager(standardJavaFileManager);
fileManagerMap.put(classLoader, fileManager);
}
final Map<String, byte[]> compiled = compileFromJava(className, javaCode, printWriter, fileManager);
final Map<String, byte[]> compiled = compileFromJava(className, javaCode, printWriter, diagnosticListener, fileManager);
for (Map.Entry<String, byte[]> entry : compiled.entrySet()) {
String className2 = entry.getKey();
synchronized (loadedClassesMap) {
Expand Down Expand Up @@ -191,6 +207,42 @@ public Class<?> loadFromJava(@NotNull ClassLoader classLoader,
return clazz;
}

@NotNull
Map<String, byte[]> compileFromJava(@NotNull String className,
@NotNull String javaCode,
@NotNull PrintWriter writer,
@NotNull DiagnosticListener<? super JavaFileObject> diagnosticListener,
MyJavaFileManager fileManager) {
Iterable<? extends JavaFileObject> compilationUnits;
if (sourceDir != null) {
String filename = className.replaceAll("\\.", '\\' + File.separator) + ".java";
File file = new File(sourceDir, filename);
writeText(file, javaCode);
if (s_standardJavaFileManager == null)
s_standardJavaFileManager = s_compiler.getStandardFileManager(null, null, null);
compilationUnits = s_standardJavaFileManager.getJavaFileObjects(file);

} else {
javaFileObjects.put(className, new JavaSourceFromString(className, javaCode));
compilationUnits = new ArrayList<>(javaFileObjects.values()); // To prevent CME from compiler code
}
// reuse the same file manager to allow caching of jar files
boolean ok = s_compiler.getTask(writer, fileManager, diagnosticListener, options, null, compilationUnits).call();

if (!ok) {
// compilation error, so we want to exclude this file from future compilation passes
if (sourceDir == null)
javaFileObjects.remove(className);

// nothing to return due to compiler error
return Collections.emptyMap();
} else {
Map<String, byte[]> result = fileManager.getAllBuffers();

return result;
}
}

private @NotNull MyJavaFileManager getFileManager(StandardJavaFileManager fm) {
return fileManagerOverride != null
? fileManagerOverride.apply(fm)
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/net/openhft/compiler/CompilerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
Expand All @@ -42,11 +42,15 @@
public enum CompilerUtils {
; // none
public static final boolean DEBUGGING = isDebug();
/**
* Singleton {@link CachedCompiler}. Uses default {@code javac} options of
* {@link CachedCompiler#CachedCompiler(File, File)}, and does not write {@code .java}
* source files and {@code .class} files to the file system.
*/
public static final CachedCompiler CACHED_COMPILER = new CachedCompiler(null, null);

private static final Logger LOGGER = LoggerFactory.getLogger(CompilerUtils.class);
private static final Method DEFINE_CLASS_METHOD;
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final String JAVA_CLASS_PATH = "java.class.path";
static JavaCompiler s_compiler;
static StandardJavaFileManager s_standardJavaFileManager;
Expand Down Expand Up @@ -160,7 +164,7 @@ public static void defineClass(@NotNull String className, @NotNull byte[] bytes)
*/
public static Class<?> defineClass(@Nullable ClassLoader classLoader, @NotNull String className, @NotNull byte[] bytes) {
try {
return (Class) DEFINE_CLASS_METHOD.invoke(classLoader, className, bytes, 0, bytes.length);
return (Class<?>) DEFINE_CLASS_METHOD.invoke(classLoader, className, bytes, 0, bytes.length);
} catch (IllegalAccessException e) {
throw new AssertionError(e);
} catch (InvocationTargetException e) {
Expand All @@ -173,7 +177,7 @@ private static String readText(@NotNull String resourceName) throws IOException
if (resourceName.startsWith("="))
return resourceName.substring(1);
StringWriter sw = new StringWriter();
Reader isr = new InputStreamReader(getInputStream(resourceName), UTF_8);
Reader isr = new InputStreamReader(getInputStream(resourceName), StandardCharsets.UTF_8);
try {
char[] chars = new char[8 * 1024];
int len;
Expand All @@ -187,11 +191,7 @@ private static String readText(@NotNull String resourceName) throws IOException

@NotNull
private static String decodeUTF8(@NotNull byte[] bytes) {
try {
return new String(bytes, UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return new String(bytes, StandardCharsets.UTF_8);
}

@Nullable
Expand Down Expand Up @@ -230,11 +230,7 @@ public static boolean writeText(@NotNull File file, @NotNull String text) {

@NotNull
private static byte[] encodeUTF8(@NotNull String text) {
try {
return text.getBytes(UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return text.getBytes(StandardCharsets.UTF_8);
}

public static boolean writeBytes(@NotNull File file, @NotNull byte[] bytes) {
Expand Down
Loading