GP-0 ignore reserved SaveState key when restoring AnalysisStateInfo

This commit is contained in:
ghidra1
2020-12-11 19:05:56 -05:00
parent e32276b5bd
commit e1e9dc0f8f
3 changed files with 21 additions and 7 deletions
@@ -39,6 +39,9 @@ public class StoredAnalyzerTimes implements CustomOption {
public void readState(SaveState saveState) { public void readState(SaveState saveState) {
taskTimes.clear(); taskTimes.clear();
for (String taskName : saveState.getNames()) { for (String taskName : saveState.getNames()) {
if (CustomOption.CUSTOM_OPTION_CLASS_NAME_KEY.equals(taskName)) {
continue; // skip this reserved key
}
taskTimes.put(taskName, saveState.getLong(taskName, 0)); taskTimes.put(taskName, saveState.getLong(taskName, 0));
} }
names = null; names = null;
@@ -17,6 +17,15 @@ package ghidra.framework.options;
public interface CustomOption { public interface CustomOption {
/**
* <code>SaveState</code> key which corresponds to custom option
* implementation class. The use of this key/value within the stored
* state information is reserved for use by the option storage
* implementation and should be ignored by {@link #readState(SaveState)}
* implementation
*/
public final String CUSTOM_OPTION_CLASS_NAME_KEY = "CUSTOM_OPTION_CLASS";
/** /**
* Concrete subclass of WrappedOption should read all of its * Concrete subclass of WrappedOption should read all of its
* state from the given saveState object. * state from the given saveState object.
@@ -100,14 +100,14 @@ public enum OptionType {
static class IntStringAdapter extends StringAdapter { static class IntStringAdapter extends StringAdapter {
@Override @Override
Object stringToObject(String string) { Object stringToObject(String string) {
return new Integer(string); return Integer.valueOf(string);
} }
} }
static class LongStringAdapter extends StringAdapter { static class LongStringAdapter extends StringAdapter {
@Override @Override
Object stringToObject(String string) { Object stringToObject(String string) {
return new Long(string); return Long.valueOf(string);
} }
} }
@@ -121,7 +121,7 @@ public enum OptionType {
static class DoubleStringAdapter extends StringAdapter { static class DoubleStringAdapter extends StringAdapter {
@Override @Override
Object stringToObject(String string) { Object stringToObject(String string) {
return new Double(string); return Double.valueOf(string);
} }
} }
@@ -155,7 +155,7 @@ public enum OptionType {
static class FloatStringAdapter extends StringAdapter { static class FloatStringAdapter extends StringAdapter {
@Override @Override
Object stringToObject(String string) { Object stringToObject(String string) {
return new Float(string); return Float.valueOf(string);
} }
} }
@@ -216,10 +216,11 @@ public enum OptionType {
@Override @Override
Object stringToObject(String string) { Object stringToObject(String string) {
SaveState saveState = getSaveStateFromXmlString(string); SaveState saveState = getSaveStateFromXmlString(string);
String customOptionClassName = saveState.getString("CUSTOM_OPTION_CLASS", null); String customOptionClassName =
saveState.getString(CustomOption.CUSTOM_OPTION_CLASS_NAME_KEY, null);
try { try {
Class<?> c = Class.forName(customOptionClassName); Class<?> c = Class.forName(customOptionClassName);
CustomOption option = (CustomOption) c.newInstance(); CustomOption option = (CustomOption) c.getConstructor().newInstance();
option.readState(saveState); option.readState(saveState);
return option; return option;
} }
@@ -239,7 +240,8 @@ public enum OptionType {
String objectToString(Object object) { String objectToString(Object object) {
CustomOption customOption = (CustomOption) object; CustomOption customOption = (CustomOption) object;
SaveState saveState = new SaveState(); SaveState saveState = new SaveState();
saveState.putString("CUSTOM_OPTION_CLASS", object.getClass().getName()); saveState.putString(CustomOption.CUSTOM_OPTION_CLASS_NAME_KEY,
object.getClass().getName());
customOption.writeState(saveState); customOption.writeState(saveState);
return saveToXmlString(saveState); return saveToXmlString(saveState);
} }