Skip to content

Commit 5025589

Browse files
committed
fixes
1 parent ef3c6ec commit 5025589

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/main/java/org/variantsync/vevos/simulation/io/TextIO.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ public static void write(final Path p, final String text) throws IOException {
136136
Files.writeString(p, text, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
137137
}
138138

139+
/**
140+
* Append the given text to the given file.
141+
* Assumes that the given file already exists.
142+
*
143+
* @param p Existing file to append text to.
144+
* @param text Text to write to file.
145+
* @throws IOException if an I/O error occurs while writing to the file, or the text cannot be encoded using the specified charset.
146+
*/
147+
public static void append(final Path p, final String text) throws IOException {
148+
Files.writeString(p, text, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
149+
}
150+
139151
public static String readAsString(final Path p) throws IOException {
140152
try (final BufferedReader reader = new BufferedReader(new FileReader(p.toFile()))) {
141153
return reader.lines().collect(Collectors.joining());

src/main/java/org/variantsync/vevos/simulation/variability/pc/SourceCodeFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Result<GroundTruth, Exception> generateVariant(
7171
.readLines(sourceFile.path())
7272
.bind(splLines -> Result.Try(() ->
7373
// write all lines that should be included in the variant to the text file
74-
TextIO.write(
74+
TextIO.append(
7575
targetFile.path(),
7676
String.join(
7777
TextIO.LINEBREAK,
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
package org.variantsync.vevos.simulation.variability.pc.variantlines;
22

3+
import org.variantsync.vevos.simulation.util.Logger;
34
import org.variantsync.vevos.simulation.variability.pc.options.VariantGenerationOptions;
45

56
import java.util.List;
67

78
public record VariantLine(Integer lineNumber) implements VariantLineChunk {
89
@Override
910
public List<String> project(final VariantGenerationOptions projectionOptions, final List<String> splFileLines) {
10-
return List.of(splFileLines.get(lineNumber - 1));
11+
// The list splFileLines is 0-based.
12+
// Our lineNumber is 1-based because line numbers are typically given 1-based.
13+
final int sourceLineNo = lineNumber - 1;
14+
15+
if (sourceLineNo >= splFileLines.size()) {
16+
final String logMessage = "Skipped copying line "
17+
+ sourceLineNo
18+
+ " as it is out of bounds [1, "
19+
+ splFileLines.size()
20+
+ "]!";
21+
22+
if (sourceLineNo > splFileLines.size()) {
23+
// This was logged frequently and is caused by https://bugs.openjdk.java.net/browse/JDK-8199413
24+
// Skipping the line really is the best solution, as the empty line is created by appending a line separator
25+
// to the previous line. I added the additional if-statement, to only catch cases in which more than one line
26+
// is out of bounds, which indicates a serious problem.
27+
Logger.error(logMessage);
28+
}
29+
30+
return List.of();
31+
} else {
32+
return List.of(splFileLines.get(sourceLineNo));
33+
}
1134
}
1235
}

0 commit comments

Comments
 (0)