Skip to content

Commit 41f5b53

Browse files
FliegendeWurstwadoon
authored andcommitted
Formatting and documentation
1 parent a7f9079 commit 41f5b53

4 files changed

Lines changed: 224 additions & 104 deletions

File tree

key.ui/src/main/java/de/uka/ilkd/key/gui/IssueDialog.java

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@
6060
public final class IssueDialog extends JDialog {
6161
private static final Logger LOGGER = LoggerFactory.getLogger(IssueDialog.class);
6262

63+
/**
64+
* Default text for critical issues (runtime exceptions).
65+
*/
66+
private static final String CRITICAL_ISSUE = "The following exception occurred:";
67+
/**
68+
* Default text for non-critical issues (JML specification warnings).
69+
*/
70+
private static final String NON_CRITICAL_ISSUE = String.format(
71+
"The following non-fatal problems occurred when translating your %s specifications:",
72+
SLEnvInput.getLanguage());
73+
6374
/** regex to find web urls in string messages */
6475
private static final Pattern HTTP_REGEX = Pattern.compile("https?://[^\\s]+");
6576

@@ -129,7 +140,20 @@ public final class IssueDialog extends JDialog {
129140

130141
public IssueDialog(Window owner, String title, Set<PositionedIssueString> issues,
131142
boolean critical) {
132-
this(owner, title, issues, critical, null);
143+
this(owner, title, critical ? CRITICAL_ISSUE : NON_CRITICAL_ISSUE, issues, critical, null);
144+
}
145+
146+
/**
147+
* Create an issue dialog with the given title and description.
148+
*
149+
* @param owner parent window
150+
* @param title window title
151+
* @param description description to show
152+
* @param issues the issues
153+
*/
154+
public IssueDialog(Window owner, String title, String description,
155+
Set<PositionedIssueString> issues) {
156+
this(owner, title, description, issues, false, null);
133157
}
134158

135159
/**
@@ -166,8 +190,33 @@ private static List<PositionedIssueString> decorateHTML(Set<PositionedIssueStrin
166190
}).collect(Collectors.toList());
167191
}
168192

193+
/**
194+
* Construct a new issue dialog based on the title, the warnings to show and the exception to show.
195+
*
196+
* @param owner parent window
197+
* @param title dialog title
198+
* @param warnings warnings to show
199+
* @param critical whether the issue is critical
200+
* @param throwable exception to show (may be null)
201+
*/
169202
IssueDialog(Window owner, String title, Set<PositionedIssueString> warnings,
170203
boolean critical, Throwable throwable) {
204+
this(owner, title, critical ? CRITICAL_ISSUE : NON_CRITICAL_ISSUE, warnings, critical,
205+
throwable);
206+
}
207+
208+
/**
209+
* Construct a new issue dialog given the title, description, warnings and exception.
210+
*
211+
* @param owner parent window
212+
* @param title dialog title
213+
* @param head description
214+
* @param warnings warnings to show
215+
* @param critical criticality of the issue
216+
* @param throwable exception to show (may be null)
217+
*/
218+
IssueDialog(Window owner, String title, String head, Set<PositionedIssueString> warnings,
219+
boolean critical, Throwable throwable) {
171220
super(owner, title, ModalityType.APPLICATION_MODAL);
172221

173222
this.throwable = throwable;
@@ -195,14 +244,6 @@ private static List<PositionedIssueString> decorateHTML(Set<PositionedIssueStrin
195244
// stTextArea
196245

197246
// set descriptive text in top label
198-
final String head;
199-
if (critical) {
200-
head = "The following exception occurred:";
201-
} else {
202-
head = String.format(
203-
"The following non-fatal problems occurred when translating your %s specifications:",
204-
SLEnvInput.getLanguage());
205-
}
206247
JLabel label = new JLabel(head);
207248
label.setBorder(BorderFactory.createEmptyBorder(5, 5, 2, 5));
208249
add(label, BorderLayout.NORTH);

key.ui/src/main/java/de/uka/ilkd/key/gui/plugins/javac/JavaCompilerCheckFacade.java

Lines changed: 81 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ public class JavaCompilerCheckFacade {
4343
* and
4444
* reports any issues to the provided <code>listener</code>
4545
*
46-
* @param listener the {@link ProblemInitializer.ProblemInitializerListener} to be informed
47-
* about any
48-
* issues found in the target Java program
46+
* @param listener the {@link ProblemInitializer.ProblemInitializerListener} to be informed
47+
* about any issues found in the target Java program
4948
* @param bootClassPath the {@link File} referring to the path containing the core Java classes
50-
* @param classPath the {@link List} of {@link File}s referring to the directory that make up
51-
* the target Java programs classpath
52-
* @param javaPath the {@link String} with the path to the source of the target Java program
53-
* @return
49+
* @param classPath the {@link List} of {@link File}s referring to the directory that make up
50+
* the target Java programs classpath
51+
* @param javaPath the {@link String} with the path to the source of the target Java program
52+
* @return future providing the list of diagnostics
5453
*/
5554
@Nonnull
5655
public static CompletableFuture<List<PositionedIssueString>> check(
@@ -71,60 +70,77 @@ public static CompletableFuture<List<PositionedIssueString>> check(
7170
return CompletableFuture.completedFuture(Collections.emptyList());
7271
}
7372

74-
var fileManager =
75-
new JavaFileManagerDelegate(
76-
compiler.getStandardFileManager(
77-
diagnostics, Locale.ENGLISH, Charset.defaultCharset()));
73+
JavaFileManagerDelegate fileManager =
74+
new JavaFileManagerDelegate(
75+
compiler.getStandardFileManager(
76+
diagnostics, Locale.ENGLISH, Charset.defaultCharset()));
7877

79-
var output = new StringWriter();
80-
var classes = new ArrayList<String>();
78+
StringWriter output = new StringWriter();
79+
List<String> classes = new ArrayList<>();
8180

82-
var paths = new ArrayList<File>();
81+
// gather configured bootstrap classpath and regular classpath
82+
List<File> paths = new ArrayList<>();
8383
if (bootClassPath != null) {
8484
paths.add(bootClassPath);
8585
}
8686
if (classPath != null && !classPath.isEmpty()) {
8787
paths.addAll(classPath);
8888
}
8989
paths.add(javaPath);
90-
var compilationUnits =
91-
fileManager.getJavaFileObjects(paths.stream()
92-
.filter(File::isDirectory)
93-
.flatMap(it -> {
94-
try {
95-
return Files.walk(it.toPath())
96-
.filter(f -> !Files.isDirectory(f))
97-
.filter(f -> f.getFileName().toString().endsWith(".java"));
98-
} catch (IOException e) {
99-
LOGGER.info("", e);
100-
return Stream.empty();
101-
}
102-
}).toArray(Path[]::new));
103-
104-
var task = compiler.getTask(output, fileManager, diagnostics,
105-
new ArrayList<>(), classes, compilationUnits);
90+
Iterable<? extends JavaFileObject> compilationUnits =
91+
fileManager.getJavaFileObjects(paths.stream()
92+
.filter(File::isDirectory)
93+
.flatMap(it -> {
94+
try {
95+
return Files.walk(it.toPath())
96+
.filter(f -> !Files.isDirectory(f))
97+
.filter(f -> f.getFileName().toString().endsWith(".java"));
98+
} catch (IOException e) {
99+
LOGGER.info("", e);
100+
return Stream.empty();
101+
}
102+
}).toArray(Path[]::new));
103+
104+
JavaCompiler.CompilationTask task = compiler.getTask(output, fileManager, diagnostics,
105+
new ArrayList<>(), classes, compilationUnits);
106106

107107
return CompletableFuture.supplyAsync(() -> {
108108
long start = System.currentTimeMillis();
109109
var b = task.call();
110110
LOGGER.info("Javac check took {} ms.", System.currentTimeMillis() - start);
111-
for (var diagnostic : diagnostics.getDiagnostics()) {
111+
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
112112
LOGGER.info("{}", diagnostic);
113113
}
114114
return diagnostics.getDiagnostics().stream().map(
115-
it -> new PositionedIssueString(
116-
it.getMessage(Locale.ENGLISH),
117-
fileManager.asPath(it.getSource()).toFile().getAbsolutePath(),
118-
new Position((int) it.getLineNumber(), (int) it.getColumnNumber()),
119-
"" + it.getCode() + " " + it.getKind()))
115+
it -> new PositionedIssueString(
116+
it.getMessage(Locale.ENGLISH),
117+
fileManager.asPath(it.getSource()).toFile().getAbsolutePath(),
118+
new Position((int) it.getLineNumber(), (int) it.getColumnNumber()),
119+
"" + it.getCode() + " " + it.getKind()))
120120
.collect(Collectors.toList());
121121
});
122122
}
123123
}
124124

125+
126+
/**
127+
* Wrapper around a {@link StandardJavaFileManager} that returns a dummy output for
128+
* class files ({@link #getJavaFileForOutput(Location, String, JavaFileObject.Kind, FileObject)}.
129+
* Every other request is delegated to the provided file manager.
130+
*
131+
* @author Alexander Weigl
132+
*/
125133
class JavaFileManagerDelegate implements StandardJavaFileManager {
134+
/**
135+
* The file manager most calls are delegated to.
136+
*/
126137
private final StandardJavaFileManager fileManager;
127138

139+
/**
140+
* Construct a new wrapper.
141+
*
142+
* @param jfm file manager
143+
*/
128144
public JavaFileManagerDelegate(StandardJavaFileManager jfm) {
129145
this.fileManager = jfm;
130146
}
@@ -135,14 +151,16 @@ public boolean isSameFile(FileObject a, FileObject b) {
135151
}
136152

137153
@Override
138-
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
154+
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(
155+
Iterable<? extends File> files) {
139156
return fileManager.getJavaFileObjectsFromFiles(files);
140157
}
141158

142159

143160
@Override
144161
@Deprecated(since = "13")
145-
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(Iterable<? extends Path> paths) {
162+
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
163+
Iterable<? extends Path> paths) {
146164
return fileManager.getJavaFileObjectsFromPaths(paths);
147165
}
148166

@@ -157,7 +175,8 @@ public Iterable<? extends JavaFileObject> getJavaFileObjects(Path... paths) {
157175
}
158176

159177
@Override
160-
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
178+
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(
179+
Iterable<String> names) {
161180
return fileManager.getJavaFileObjectsFromStrings(names);
162181
}
163182

@@ -172,12 +191,14 @@ public void setLocation(Location location, Iterable<? extends File> files) throw
172191
}
173192

174193
@Override
175-
public void setLocationFromPaths(Location location, Collection<? extends Path> paths) throws IOException {
194+
public void setLocationFromPaths(Location location, Collection<? extends Path> paths)
195+
throws IOException {
176196
fileManager.setLocationFromPaths(location, paths);
177197
}
178198

179199
@Override
180-
public void setLocationForModule(Location location, String moduleName, Collection<? extends Path> paths) throws IOException {
200+
public void setLocationForModule(Location location, String moduleName,
201+
Collection<? extends Path> paths) throws IOException {
181202
fileManager.setLocationForModule(location, moduleName, paths);
182203
}
183204

@@ -207,7 +228,8 @@ public ClassLoader getClassLoader(Location location) {
207228
}
208229

209230
@Override
210-
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
231+
public Iterable<JavaFileObject> list(Location location, String packageName,
232+
Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
211233
return fileManager.list(location, packageName, kinds, recurse);
212234
}
213235

@@ -227,13 +249,16 @@ public boolean hasLocation(Location location) {
227249
}
228250

229251
@Override
230-
public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException {
252+
public JavaFileObject getJavaFileForInput(Location location, String className,
253+
JavaFileObject.Kind kind) throws IOException {
231254
return fileManager.getJavaFileForInput(location, className, kind);
232255
}
233256

234257
@Override
235-
public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
258+
public JavaFileObject getJavaFileForOutput(Location location, String className,
259+
JavaFileObject.Kind kind, FileObject sibling) throws IOException {
236260
if (kind == JavaFileObject.Kind.CLASS && location == StandardLocation.CLASS_OUTPUT) {
261+
// do not save compiled .class files on disk
237262
try {
238263
return new IgnoreOutputJavaFileObject(className, kind);
239264
} catch (URISyntaxException e) {
@@ -245,12 +270,14 @@ public JavaFileObject getJavaFileForOutput(Location location, String className,
245270

246271

247272
@Override
248-
public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
273+
public FileObject getFileForInput(Location location, String packageName, String relativeName)
274+
throws IOException {
249275
return fileManager.getFileForInput(location, packageName, relativeName);
250276
}
251277

252278
@Override
253-
public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
279+
public FileObject getFileForOutput(Location location, String packageName, String relativeName,
280+
FileObject sibling) throws IOException {
254281
return fileManager.getFileForOutput(location, packageName, relativeName, sibling);
255282
}
256283

@@ -276,7 +303,8 @@ public Location getLocationForModule(Location location, JavaFileObject fo) throw
276303
}
277304

278305
@Override
279-
public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service) throws IOException {
306+
public <S> ServiceLoader<S> getServiceLoader(Location location, Class<S> service)
307+
throws IOException {
280308
return fileManager.getServiceLoader(location, service);
281309
}
282310

@@ -302,6 +330,11 @@ public int isSupportedOption(String option) {
302330
}
303331

304332

333+
/**
334+
* Java file object that ignores all writes.
335+
*
336+
* @author Alexander Weigl
337+
*/
305338
class IgnoreOutputJavaFileObject extends SimpleJavaFileObject {
306339
public IgnoreOutputJavaFileObject(final String name, Kind kind) throws URISyntaxException {
307340
super(new URI("memory://" + name + ".class"), kind);
@@ -310,10 +343,6 @@ public IgnoreOutputJavaFileObject(final String name, Kind kind) throws URISyntax
310343
// ignore written class output
311344
@Override
312345
public OutputStream openOutputStream() {
313-
return new OutputStream() {
314-
@Override
315-
public void write(int b) {
316-
}
317-
};
346+
return OutputStream.nullOutputStream();
318347
}
319348
}

0 commit comments

Comments
 (0)