-
Notifications
You must be signed in to change notification settings - Fork 146
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
Marcono1234
wants to merge
1
commit into
OpenHFT:ea
Choose a base branch
from
Marcono1234:diagnostics-listener
base: ea
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
|
||
|
@@ -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()) { | ||
|
@@ -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, | ||
@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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not completely sure about the |
||
Class<?> clazz = null; | ||
Map<String, Class<?>> loadedClasses; | ||
synchronized (loadedClassesMap) { | ||
|
@@ -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) { | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.