Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix startup crash if ~/.key/colors.json is not present #3439

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions key.core/src/main/java/de/uka/ilkd/key/nparser/ParsingFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static KeyAst.Taclet parseTaclet(CharStream source) {

// region configuration
/**
* Parses a the configuration determined by the given {@code file}.
* Parses the configuration determined by the given {@code file}.
* A configuration corresponds to the grammar rule {@code cfile} in the {@code KeYParser.g4}.
*
* @param file non-null {@link Path} object
Expand All @@ -221,19 +221,20 @@ public static KeyAst.ConfigurationFile parseConfigurationFile(Path file) throws
}

/**
* @param file non-null file to read as configuration
* @see #parseConfigurationFile(Path)
* @throws IOException if the file is not found or not readable.
*/
public static KeyAst.ConfigurationFile parseConfigurationFile(File file) throws IOException {
return parseConfigurationFile(file.toPath());
}

/**
* Parses a the configuration determined by the given {@code stream}.
* Parses the configuration determined by the given {@code stream}.
* A configuration corresponds to the grammar rule {@code cfile} in the {@code KeYParser.g4}.
*
* @param file non-null {@link CharStream} object
* @param stream non-null {@link CharStream} object
* @return monad that encapsluate the ParserRuleContext
* @throws IOException if the file is not found or not readable.
* @throws BuildingException if the file is syntactical broken.
*/
public static KeyAst.ConfigurationFile parseConfigurationFile(CharStream stream) {
Expand All @@ -244,27 +245,28 @@ public static KeyAst.ConfigurationFile parseConfigurationFile(CharStream stream)
}

/**
* Parses a the configuration determined by the given {@code stream}.
* Parses the configuration determined by the given {@code stream}.
* A configuration corresponds to the grammar rule {@code cfile} in the {@code KeYParser.g4}.
*
* @param file non-null {@link CharStream} object
* @return a configration object with the data deserialize from the given file
* @throws IOException if the file is not found or not readable.
* @param input non-null {@link CharStream} object
* @return a configuration object with the data deserialize from the given file
* @throws BuildingException if the file is syntactical broken.
*/
public static Configuration readConfigurationFile(CharStream input) throws IOException {
public static Configuration readConfigurationFile(CharStream input) {
return parseConfigurationFile(input).asConfiguration();
}

/**
* @see #readConfigurationFile(CharStream)
* @throws IOException if the file is not found or not readable.
*/
public static Configuration readConfigurationFile(Path file) throws IOException {
return readConfigurationFile(CharStreams.fromPath(file));
}

/**
* @see #readConfigurationFile(CharStream)
* @throws IOException if the file is not found or not readable.
*/
public static Configuration readConfigurationFile(File file) throws IOException {
return readConfigurationFile(file.toPath());
Expand Down
39 changes: 8 additions & 31 deletions key.ui/src/main/java/de/uka/ilkd/key/gui/colors/ColorSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;

import de.uka.ilkd.key.gui.settings.SettingsManager;
import de.uka.ilkd.key.settings.AbstractPropertiesSettings;
import de.uka.ilkd.key.settings.Configuration;
import de.uka.ilkd.key.settings.PathConfig;
Expand All @@ -33,21 +30,11 @@
* @version 1 (10.05.19)
*/
public class ColorSettings extends AbstractPropertiesSettings {
public static final String SETTINGS_FILENAME = "colors.properties";
public static final File SETTINGS_FILE =
new File(PathConfig.getKeyConfigDir(), SETTINGS_FILENAME);

public static final File SETTINGS_FILE_NEW =
new File(PathConfig.getKeyConfigDir(), "colors.json");
private static final Logger LOGGER = LoggerFactory.getLogger(ColorSettings.class);
private static ColorSettings INSTANCE;

private ColorSettings(Properties settings) {
super("");
readSettings(settings);
Runtime.getRuntime().addShutdownHook(new Thread(this::save));
}

public ColorSettings(Configuration load) {
super("");
readSettings(load);
Expand All @@ -56,15 +43,17 @@ public ColorSettings(Configuration load) {

public static ColorSettings getInstance() {
if (INSTANCE == null) {
if (SETTINGS_FILE.exists()) {
if (SETTINGS_FILE_NEW.exists()) {
try {
LOGGER.info("Use new configuration format at {}", SETTINGS_FILE_NEW);
return INSTANCE = new ColorSettings(Configuration.load(SETTINGS_FILE_NEW));
LOGGER.info("Load color settings from file {}", SETTINGS_FILE_NEW);
INSTANCE = new ColorSettings(Configuration.load(SETTINGS_FILE_NEW));
return INSTANCE;
} catch (IOException e) {
LOGGER.error("Could not read {}", SETTINGS_FILE_NEW, e);
}
}
return INSTANCE = new ColorSettings(SettingsManager.loadProperties(SETTINGS_FILE));
INSTANCE = new ColorSettings(new Configuration());
return INSTANCE;
}
return INSTANCE;
}
Expand Down Expand Up @@ -94,22 +83,10 @@ public static Color invert(Color c) {
/**
* Writes the current settings to default location.
*
* @see #SETTINGS_FILE
* @see #SETTINGS_FILE_NEW
*/
public void save() {
LOGGER.info("Save color settings to: " + SETTINGS_FILE.getAbsolutePath());
try (Writer writer = new FileWriter(SETTINGS_FILE, StandardCharsets.UTF_8)) {
Properties props = new Properties();
for (Map.Entry<String, Object> entry : properties.entrySet()) {
props.setProperty(entry.getKey(), entry.getValue().toString());
}
props.store(writer, "KeY's Colors");
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}

LOGGER.info("Save color settings to: " + SETTINGS_FILE_NEW.getAbsolutePath());
LOGGER.info("Save color settings to: {}", SETTINGS_FILE_NEW.getAbsolutePath());
try (Writer writer = new FileWriter(SETTINGS_FILE_NEW)) {
var config = new Configuration(properties);
config.save(writer, "KeY's Colors");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ColorSettingsProvider() {
super();
setHeaderText(getDescription());
setSubHeaderText(
"Color settings are stored in: " + ColorSettings.SETTINGS_FILE.getAbsolutePath());
"Color settings are stored in: " + ColorSettings.SETTINGS_FILE_NEW.getAbsolutePath());
add(new JScrollPane(tblColors));
}

Expand Down