fix(gui): IllegalArgumentException when saving project to a different directory than the APK file (#1387)(PR #1388)

This commit is contained in:
Jan S
2022-02-23 10:27:04 +01:00
committed by GitHub
parent ed2a3c8458
commit e7151ad7b2
@@ -5,12 +5,18 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
public class RelativePathTypeAdapter extends TypeAdapter<Path> {
private static final Logger LOG = LoggerFactory.getLogger(RelativePathTypeAdapter.class);
private final Path basePath;
public RelativePathTypeAdapter(Path basePath) {
@@ -23,8 +29,14 @@ public class RelativePathTypeAdapter extends TypeAdapter<Path> {
out.nullValue();
} else {
value = value.toAbsolutePath().normalize();
String relativePath = basePath.relativize(value).toString();
out.value(relativePath);
Path resultPath;
try {
resultPath = basePath.relativize(value);
} catch (IllegalArgumentException e) {
LOG.warn("Unable to build a relative path to {} - using absolute path", value);
resultPath = value;
}
out.value(resultPath.toString());
}
}