我必须为不同的测试用例保存配置对象。我决定使用GSON,因为我已经使用过了。问题是配置文件的结构/继承在其定义中包含泛型。如果这是重复的,我已阅读该主题,但无法将其应用于我的问题。代码被细分为最低限度。
除了其他易于序列化的组件外,它还有一个ArrayList<T>
。
//There are classes that inherit from that too
public class Configuration<T extends ParamSet> implements Iterable<T>{
// this list is what i want to set from my ApplicationClass
private ArrayList<T> _paramSets = new ArrayList<T>();
//getter, setter and constructor
}
这些课程没什么特别之处。只需整数和布尔值。
public class ParamSet {}
public class ChildParamSet extends ParamSet{}
这是保存过程开始的地方。我已经定义了一个抽象方法,后来在运行时已知T
的类中实现。
public abstract class ApplicationClass<T extends ParamSet>{
private Configuration<T> config;
// in this method i want to set either a new config or new params in the config
public setupConfig(){
//some checks and if the user wants to load a config from file
loadConfig(file);
}
//abstract method to be called in more concrete classes
abstract protected Configuration<T> loadConfig(File file);
}
那么我知道T
的类型,我想调用getConfiguration
方法。
public abstract class MoreConcreteApplicationClass extends ApplicationClass<ChildParamSet>{
//constructor and other stuff
@Override
protected Configuration<ChildParamSet> loadConfig(File file){
return ConfigurationPersistenceHelper.getConfiguration(file);
}
}
public class ConfigurationPersistenceHelper(){
//i get the configuration with a linked treeMap --> wrong type
public static <T extends ParamSet> Configuration<T> getConfiguration(final File location){
GsonBuilder builder = new GsonBuilder();
GsonBuilder builder.enableComplexMapKeySerialization.create();
final Type type = new TypeToken<Configuration<T>>(){}.getType();
final String jsonRepresentation = new String(Files.readAllBytes(location.toPath()));
return gson.fromJson(jsonRepresentation, type);
}
}
所以问题是ConfigurationPersistenceHelper中的getConfiguration
方法。当我运行此方法时,而不是ArrayList<ChildParamSet>
我在配置中获得Linked TreeMap
。
我想这是因为在运行时期间对泛型的类型擦除以及我使用T extends ParamSet
定义方法。
但我该如何解决这个问题呢?是否有另一种方法来调用方法,以便类型不会丢失?
我知道我可以在每个具体应用程序类的loadConfig
方法中实现反序列化逻辑。但是这会导致很多重复的代码。这是我目前的解决方案。
我也知道我可以将类型从loadConfig
(类型在运行时仍然可用)传递给序列化器到getConfiguration
方法。这是不好的做法吗?
我也知道我可以编写自己的反序列化程序但是我还需要运行时的类型,这是我目前的问题。
答案 0 :(得分:1)
是的,Type Erasure就是问题所在。只需传递类型信息:
abstract protected Configuration<T> loadConfig(File file, Class<T> clazz);
在该方法中使用ArrayList的自定义ParameterizedType,请参阅: