Skip to content

Commit 7d46e6f

Browse files
authored
Merge pull request #8895 from mbien/robust-taskcache-loaderrors
Make TaskCache#loadErrors more robust on parsing failure
2 parents 4886ee8 + 7287e8e commit 7d46e6f

File tree

1 file changed

+49
-54
lines changed
  • ide/parsing.indexing/src/org/netbeans/modules/parsing/impl/indexing/errors

1 file changed

+49
-54
lines changed

ide/parsing.indexing/src/org/netbeans/modules/parsing/impl/indexing/errors/TaskCache.java

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ private <T> boolean dumpErrors(File output, Iterable<? extends T> errors, Conver
161161
boolean existed = interestedInReturnValue && output.exists();
162162
output.getParentFile().mkdirs();
163163
try {
164-
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8));
165-
try {
164+
try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8))) {
166165
for (T err : errors) {
167166
pw.print(convertor.getKind(err).name());
168167
pw.print(':'); //NOI18N
@@ -185,8 +184,6 @@ private <T> boolean dumpErrors(File output, Iterable<? extends T> errors, Conver
185184
pw.println(description);
186185
}
187186
}
188-
} finally {
189-
pw.close();
190187
}
191188
} catch (FileNotFoundException fnf) {
192189
if (!output.getParentFile().canWrite()) {
@@ -288,50 +285,58 @@ private <T> void dumpErrors(TransactionContext c, URL root, Indexable i, Iterabl
288285

289286
private <T> List<T> loadErrors(File input, ReverseConvertor<T> convertor) throws IOException {
290287
List<T> result = new LinkedList<>();
291-
BufferedReader pw = new BufferedReader(new InputStreamReader(new FileInputStream(input), StandardCharsets.UTF_8));
292-
String line;
288+
try (BufferedReader pw = new BufferedReader(new InputStreamReader(new FileInputStream(input), StandardCharsets.UTF_8))) {
289+
String line;
293290

294-
while ((line = pw.readLine()) != null) {
295-
String[] parts = line.split(":"); //NOI18N
296-
if (parts.length != 3) {
297-
continue;
298-
}
291+
while ((line = pw.readLine()) != null) {
292+
String[] parts = line.split(":"); //NOI18N
293+
if (parts.length != 3) {
294+
continue;
295+
}
299296

300-
ErrorKind kind = null;
301-
try {
302-
kind = ErrorKind.valueOf(parts[0]);
303-
} catch (IllegalArgumentException iae) {
304-
LOG.log(Level.FINE, "Invalid ErrorKind: {0}", line); //NOI18N
305-
}
306-
307-
if (kind == null) {
308-
continue;
309-
}
297+
ErrorKind kind = null;
298+
try {
299+
kind = ErrorKind.valueOf(parts[0]);
300+
} catch (IllegalArgumentException iae) {
301+
LOG.log(Level.FINE, "Invalid ErrorKind: {0}", line); //NOI18N
302+
}
310303

311-
ErrorsCache.Range range;
312-
Matcher matcher = PATTERN.matcher(parts[1]);
313-
if (matcher.matches()) {
314-
ErrorsCache.Position start = new ErrorsCache.Position(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)));
315-
ErrorsCache.Position end = matcher.group(3) != null && matcher.group(4) != null ? new ErrorsCache.Position(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4))) : null;
316-
range = new ErrorsCache.Range(start, end);
317-
} else {
318-
int lineNumber = Integer.parseInt(parts[1]);
319-
range = new ErrorsCache.Range(new ErrorsCache.Position(lineNumber, 1), null);
320-
}
304+
if (kind == null) {
305+
continue;
306+
}
321307

322-
String message = parts[2];
308+
ErrorsCache.Range range;
309+
Matcher matcher = PATTERN.matcher(parts[1]);
310+
try {
311+
if ("-1,-1".equals(parts[1])) { //NOI18N
312+
range = new ErrorsCache.Range(new ErrorsCache.Position(-1, -1), null);
313+
} else if (matcher.matches()) {
314+
ErrorsCache.Position start = new ErrorsCache.Position(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)));
315+
ErrorsCache.Position end = matcher.group(3) != null && matcher.group(4) != null
316+
? new ErrorsCache.Position(Integer.parseInt(matcher.group(3)), Integer.parseInt(matcher.group(4)))
317+
: null;
318+
range = new ErrorsCache.Range(start, end);
319+
} else {
320+
int lineNumber = Integer.parseInt(parts[1]);
321+
range = new ErrorsCache.Range(new ErrorsCache.Position(lineNumber, 1), null);
322+
}
323+
} catch (NumberFormatException ex) {
324+
LOG.log(Level.FINE, "Can't parse error line: " + line, ex); //NOI18N
325+
continue;
326+
}
323327

324-
message = message.replace("\\d", ":") //NOI18N
325-
.replace("\\n", "\n") //NOI18N
326-
.replace("\\\\", "\\"); //NOI18N
328+
String message = parts[2];
327329

328-
T item = convertor.get(kind, range, message);
329-
if (null != item) {
330-
result.add(item);
330+
message = message.replace("\\d", ":") //NOI18N
331+
.replace("\\n", "\n") //NOI18N
332+
.replace("\\\\", "\\"); //NOI18N
333+
334+
T item = convertor.get(kind, range, message);
335+
if (item != null) {
336+
result.add(item);
337+
}
331338
}
332339
}
333-
334-
pw.close();
335340

336341
return result;
337342
}
@@ -521,13 +526,8 @@ private static Properties loadRelocation(
521526
final Properties result = new Properties();
522527
final File relocationFile = new File (cacheRoot, RELOCATION_FILE);
523528
if (relocationFile.canRead()) {
524-
try {
525-
final FileInputStream in = new FileInputStream(relocationFile);
526-
try {
527-
result.load(in);
528-
} finally {
529-
in.close();
530-
}
529+
try (FileInputStream in = new FileInputStream(relocationFile)) {
530+
result.load(in);
531531
} catch (IOException ioe) {
532532
Exceptions.printStackTrace(ioe);
533533
}
@@ -539,13 +539,8 @@ private static void storeRelocation(
539539
@NonNull final File cacheRoot,
540540
@NonNull final Properties relocation) {
541541
final File relocationFile = new File (cacheRoot, RELOCATION_FILE);
542-
try {
543-
final OutputStream out = new FileOutputStream(relocationFile);
544-
try {
545-
relocation.store(out, null);
546-
} finally {
547-
out.close();
548-
}
542+
try (OutputStream out = new FileOutputStream(relocationFile)) {
543+
relocation.store(out, null);
549544
} catch (IOException ioe) {
550545
Exceptions.printStackTrace(ioe);
551546
}

0 commit comments

Comments
 (0)