Skip to content

Commit 83c2627

Browse files
committed
fixing relative java path
1 parent a8e987d commit 83c2627

6 files changed

Lines changed: 37 additions & 51 deletions

File tree

key.core/src/main/java/de/uka/ilkd/key/proof/io/GZipProofSaver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public GZipProofSaver(Proof proof, String fileName, String internalVersion) {
3737
@Override
3838
protected void save(Path file) throws IOException {
3939
try (var out = new GZIPOutputStream(Files.newOutputStream(file))) {
40-
save(out);
40+
save(file.getParent(), out);
4141
}
4242
}
4343
}

key.core/src/main/java/de/uka/ilkd/key/proof/io/OutputStreamProofSaver.java

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import de.uka.ilkd.key.proof.Node;
2323
import de.uka.ilkd.key.proof.Proof;
2424
import de.uka.ilkd.key.proof.init.IPersistablePO;
25+
import de.uka.ilkd.key.proof.init.KeYUserProblemFile;
2526
import de.uka.ilkd.key.proof.init.Profile;
2627
import de.uka.ilkd.key.proof.init.ProofOblInput;
2728
import de.uka.ilkd.key.proof.io.IProofFileParser.ProofElementID;
@@ -62,6 +63,8 @@
6263
import org.slf4j.Logger;
6364
import org.slf4j.LoggerFactory;
6465

66+
import static org.key_project.util.java.IOUtil.safePathRelativeTo;
67+
6568
/**
6669
* Saves a proof to a given {@link OutputStream}.
6770
*
@@ -84,19 +87,6 @@ public class OutputStreamProofSaver {
8487
protected final boolean saveProofSteps;
8588

8689

87-
/**
88-
* Extracts java source directory from {@link Proof#header()}, if it exists.
89-
*
90-
* @param proof the Proof
91-
* @return the location of the java source code or null if no such exists
92-
*/
93-
public static @Nullable File getJavaSourceLocation(Proof proof) {
94-
KeyAst.@Nullable Declarations header = proof.header();
95-
if (header == null)
96-
return null;
97-
return header.getJavaSourceLocation();
98-
}
99-
10090
public OutputStreamProofSaver(Proof proof) {
10191
this(proof, KeYConstants.INTERNAL_VERSION);
10292
}
@@ -153,7 +143,7 @@ public String writeSettings(ProofSettings ps) {
153143
return String.format("\\settings %s \n", ps.settingsToString());
154144
}
155145

156-
public void save(OutputStream out) throws IOException {
146+
public void save(Path basePath, OutputStream out) throws IOException {
157147
CopyReferenceResolver.copyCachedGoals(proof, null, null, null);
158148
try (var ps = new PrintWriter(out, true, StandardCharsets.UTF_8)) {
159149
final ProofOblInput po =
@@ -179,7 +169,7 @@ public void save(OutputStream out) throws IOException {
179169

180170

181171
var header = proof.header();
182-
String headerStr = makePathsRelative(header);
172+
String headerStr = makePathsRelative(basePath, header);
183173
ps.print(headerStr);
184174

185175
proof.printSymbols(ps);
@@ -214,14 +204,6 @@ public void save(OutputStream out) throws IOException {
214204
}
215205
}
216206

217-
protected @Nullable Path getBasePath() throws IOException {
218-
File javaSourceLocation = getJavaSourceLocation(proof);
219-
if (javaSourceLocation != null) {
220-
return javaSourceLocation.toPath().toAbsolutePath();
221-
} else {
222-
return null;
223-
}
224-
}
225207

226208
/// Searches in the header for absolute paths to Java files and tries to replace them by paths
227209
/// relative to the proof file to be saved.
@@ -239,13 +221,12 @@ public void save(OutputStream out) throws IOException {
239221
///
240222
/// @see KeYUserProblemFile#getProblemHeader()
241223
/// @see de.uka.ilkd.key.proof.init.InitConfig#getProblemHeader()
242-
private String makePathsRelative(KeyAst.@Nullable Declarations header) throws IOException {
224+
private String makePathsRelative(Path basePath, KeyAst.@Nullable Declarations header) throws IOException {
243225
if (header == null) {
244226
return "";
245227
}
246228
StringWriter sw = new StringWriter();
247229
PrintWriter out = new PrintWriter(sw, true);
248-
Path basePath = getBasePath();
249230

250231
header.printDefinitions(out);
251232

@@ -259,20 +240,20 @@ private String makePathsRelative(KeyAst.@Nullable Declarations header) throws IO
259240
var classPath = jm.getClassPath();
260241
if (classPath != null && !classPath.isEmpty()) {
261242
for (Path path : classPath) {
262-
out.printf("\\classpath \"%s\";\\n", IOUtil.safePathRelativeTo(path, basePath));
243+
out.printf("\\classpath \"%s\";\\n", safePathRelativeTo(path, basePath));
263244
}
264245
}
265246

266247
var javaSource = jm.getModelDir();
267248
if (javaSource != null) {
268-
out.printf("\\javaSource \"%s\";\n", IOUtil.safePathRelativeTo(javaSource, basePath));
249+
out.printf("\\javaSource \"%s\";\n", safePathRelativeTo(javaSource, basePath));
269250
}
270251

271252
var includedFiles = jm.getIncludedFiles();
272253
if (includedFiles != null && !includedFiles.isEmpty()) {
273254
for (var includedFile : includedFiles) {
274255
out.printf("\\include \"%s\";\n",
275-
IOUtil.safePathRelativeTo(includedFile, basePath));
256+
safePathRelativeTo(includedFile, basePath));
276257
}
277258
}
278259

key.core/src/main/java/de/uka/ilkd/key/proof/io/ProofBundleSaver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ protected void save(Path file) throws IOException {
5454
String proofFileName = MiscTools.toValidFileName(proof.name() + ".proof");
5555

5656
// save the proof file to the FileRepo (stream is closed by the save method!)
57-
save(repo.createOutputStream(Paths.get(proofFileName)));
57+
final var path = Paths.get(proofFileName);
58+
save(path.toAbsolutePath().getParent(), repo.createOutputStream(path));
5859

5960
// save proof bundle with the help of the FileRepo
6061
((AbstractFileRepo) repo).saveProof(file);

key.core/src/main/java/de/uka/ilkd/key/proof/io/ProofSaver.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public ProofSaver(Proof proof, Path file, String internalVersion, boolean savePr
117117
*/
118118
protected void save(Path file) throws IOException {
119119
try (var out = Files.newOutputStream(file)) {
120-
save(out);
120+
save(file.getParent(), out);
121121
}
122122
}
123123

@@ -141,23 +141,6 @@ public String save() {
141141
return errorMsg;
142142
}
143143

144-
@Override
145-
protected Path getBasePath() throws IOException {
146-
return computeBasePath(file);
147-
}
148-
149-
/**
150-
* Computes the base path of the given proof {@link File}.
151-
* <p>
152-
* This method is used by {@link #getBasePath()} and by the Eclipse integration.
153-
*
154-
* @param proofFile The proof {@link File}.
155-
* @return The computed base path of the given proof {@link File}.
156-
*/
157-
public static Path computeBasePath(Path proofFile) {
158-
return proofFile.toAbsolutePath().getParent().toAbsolutePath();
159-
}
160-
161144
/**
162145
* Adds the {@link ProofSaverListener}.
163146
*

key.ui/src/main/java/de/uka/ilkd/key/gui/actions/SendFeedbackAction.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.nio.charset.Charset;
1212
import java.nio.charset.StandardCharsets;
1313
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
1416
import java.util.LinkedList;
1517
import java.util.List;
1618
import java.util.zip.ZipEntry;
@@ -23,6 +25,7 @@
2325
import de.uka.ilkd.key.core.KeYMediator;
2426
import de.uka.ilkd.key.gui.IssueDialog;
2527
import de.uka.ilkd.key.gui.MainWindow;
28+
import de.uka.ilkd.key.nparser.KeyAst;
2629
import de.uka.ilkd.key.parser.Location;
2730
import de.uka.ilkd.key.proof.Goal;
2831
import de.uka.ilkd.key.proof.Proof;
@@ -32,6 +35,7 @@
3235
import de.uka.ilkd.key.util.KeYConstants;
3336
import de.uka.ilkd.key.util.KeYResourceManager;
3437

38+
import org.jspecify.annotations.Nullable;
3539
import org.key_project.util.Streams;
3640
import org.key_project.util.java.IOUtil;
3741

@@ -69,6 +73,19 @@ private static String serializeStackTrace(Throwable t) {
6973
return sw.toString();
7074
}
7175

76+
/**
77+
* Extracts java source directory from {@link Proof#header()}, if it exists.
78+
*
79+
* @param proof the Proof
80+
* @return the location of the java source code or null if no such exists
81+
*/
82+
public static @Nullable File getJavaSourceLocation(Proof proof) {
83+
KeyAst.@Nullable Declarations header = proof.header();
84+
if (header == null)
85+
return null;
86+
return header.getJavaSourceLocation();
87+
}
88+
7289
private static abstract class SendFeedbackItem implements ActionListener {
7390

7491
final String displayName;
@@ -213,7 +230,7 @@ byte[] retrieveFileData() throws IOException {
213230
Proof proof = mediator.getSelectedProof();
214231
OutputStreamProofSaver saver = new OutputStreamProofSaver(proof);
215232
ByteArrayOutputStream stream = new ByteArrayOutputStream();
216-
saver.save(stream);
233+
saver.save(Paths.get(".").toAbsolutePath(), stream);
217234
return stream.toByteArray();
218235
}
219236

@@ -302,7 +319,7 @@ public JavaSourceItem() {
302319
boolean isEnabled() {
303320
try {
304321
Proof proof = MainWindow.getInstance().getMediator().getSelectedProof();
305-
File javaSourceLocation = OutputStreamProofSaver.getJavaSourceLocation(proof);
322+
File javaSourceLocation = getJavaSourceLocation(proof);
306323
return javaSourceLocation != null;
307324
} catch (Exception e) {
308325
return false;
@@ -322,7 +339,7 @@ private void getJavaFilesRecursively(File directory, List<File> list) {
322339
@Override
323340
void appendDataToZipOutputStream(ZipOutputStream stream) throws IOException {
324341
Proof proof = MainWindow.getInstance().getMediator().getSelectedProof();
325-
File javaSourceLocation = OutputStreamProofSaver.getJavaSourceLocation(proof);
342+
File javaSourceLocation = getJavaSourceLocation(proof);
326343
List<File> javaFiles = new LinkedList<>();
327344
getJavaFilesRecursively(javaSourceLocation, javaFiles);
328345
for (File f : javaFiles) {

key.util/src/main/java/org/key_project/util/java/IOUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,10 @@ public static String safePath(Path path) {
826826
}
827827

828828

829+
/// Returns a string, representing the given path to `source` relatively to `basePath`.
830+
/// @param source a path
831+
/// @param basePath a directory
832+
/// @return a string that is printable (escaped) for KeY files
829833
public static String safePathRelativeTo(Path source, Path basePath) {
830834
var abs = source.toAbsolutePath();
831835
return safePath(basePath.relativize(abs));

0 commit comments

Comments
 (0)