forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaCompilationProvider.java
178 lines (150 loc) · 6.79 KB
/
JavaCompilationProvider.java
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
170
171
172
173
174
175
176
177
178
package io.quarkus.deployment.dev;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import org.jboss.logging.Logger;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import io.quarkus.gizmo.Gizmo;
import io.quarkus.paths.PathCollection;
public class JavaCompilationProvider implements CompilationProvider {
private static final Logger log = Logger.getLogger(JavaCompilationProvider.class);
// -g is used to make the java compiler generate all debugging info
// -parameters is used to generate metadata for reflection on method parameters
// this is useful when people using debuggers against their hot-reloaded app
private static final Set<String> COMPILER_OPTIONS = Set.of("-g", "-parameters");
private static final Set<String> IGNORE_NAMESPACES = Set.of("org.osgi");
JavaCompiler compiler;
StandardJavaFileManager fileManager;
DiagnosticCollector<JavaFileObject> fileManagerDiagnostics;
@Override
public String getProviderKey() {
return "java";
}
@Override
public Set<String> handledExtensions() {
return Set.of(".java");
}
@Override
public void compile(Set<File> filesToCompile, Context context) {
JavaCompiler compiler = this.compiler;
if (compiler == null) {
compiler = this.compiler = ToolProvider.getSystemJavaCompiler();
}
if (compiler == null) {
throw new RuntimeException("No system java compiler provided");
}
try {
if (fileManager == null) {
fileManager = compiler.getStandardFileManager(fileManagerDiagnostics = new DiagnosticCollector<>(), null,
context.getSourceEncoding());
}
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
fileManager.setLocation(StandardLocation.CLASS_PATH, context.getClasspath());
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, List.of(context.getOutputDirectory()));
CompilerFlags compilerFlags = new CompilerFlags(COMPILER_OPTIONS, context.getCompilerOptions(getProviderKey()),
context.getReleaseJavaVersion(), context.getSourceJavaVersion(), context.getTargetJvmVersion());
Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles(filesToCompile);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics,
compilerFlags.toList(), null, sources);
if (!task.call()) {
StringBuilder sb = new StringBuilder("\u001B[91mCompilation Failed:");
for (Diagnostic<? extends JavaFileObject> i : diagnostics.getDiagnostics()) {
sb.append("\n");
sb.append(i.toString());
}
sb.append("\u001b[0m");
throw new RuntimeException(sb.toString());
}
logDiagnostics(diagnostics);
if (!fileManagerDiagnostics.getDiagnostics().isEmpty()) {
logDiagnostics(fileManagerDiagnostics);
fileManager.close();
fileManagerDiagnostics = null;
fileManager = null;
}
} catch (IOException e) {
throw new RuntimeException("Cannot close file manager", e);
}
}
@Override
public Path getSourcePath(Path classFilePath, PathCollection sourcePaths, String classesPath) {
Path sourceFilePath = null;
final RuntimeUpdatesClassVisitor visitor = new RuntimeUpdatesClassVisitor(sourcePaths, classesPath);
try (final InputStream inputStream = Files.newInputStream(classFilePath)) {
final ClassReader reader = new ClassReader(inputStream);
reader.accept(visitor, 0);
sourceFilePath = visitor.getSourceFileForClass(classFilePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
return sourceFilePath;
}
@Override
public void close() throws IOException {
if (fileManager != null) {
fileManager.close();
fileManager = null;
fileManagerDiagnostics = null;
}
}
private void logDiagnostics(final DiagnosticCollector<JavaFileObject> diagnostics) {
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
Logger.Level level = diagnostic.getKind() == Diagnostic.Kind.ERROR ? Logger.Level.ERROR : Logger.Level.WARN;
String message = diagnostic.getMessage(null);
if (level.equals(Logger.Level.WARN) && ignoreWarningForNamespace(message)) {
continue;
}
log.logf(level, "%s, line %d in %s", message, diagnostic.getLineNumber(),
diagnostic.getSource() == null ? "[unknown source]" : diagnostic.getSource().getName());
}
}
private static boolean ignoreWarningForNamespace(String message) {
for (String ignoreNamespace : IGNORE_NAMESPACES) {
if (message.contains(ignoreNamespace)) {
return true;
}
}
return false;
}
static class RuntimeUpdatesClassVisitor extends ClassVisitor {
private final PathCollection sourcePaths;
private final String classesPath;
private String sourceFile;
public RuntimeUpdatesClassVisitor(PathCollection sourcePaths, String classesPath) {
super(Gizmo.ASM_API_VERSION);
this.sourcePaths = sourcePaths;
this.classesPath = classesPath;
}
@Override
public void visitSource(String source, String debug) {
this.sourceFile = source;
}
public Path getSourceFileForClass(final Path classFilePath) {
for (Path sourcesDir : sourcePaths) {
final Path classesDir = Paths.get(classesPath);
final StringBuilder sourceRelativeDir = new StringBuilder();
sourceRelativeDir.append(classesDir.relativize(classFilePath.getParent()));
sourceRelativeDir.append(File.separator);
sourceRelativeDir.append(sourceFile);
final Path sourceFilePath = sourcesDir.resolve(Path.of(sourceRelativeDir.toString()));
if (Files.exists(sourceFilePath)) {
return sourceFilePath;
}
}
return null;
}
}
}