我决定使用Simple XML序列化,并且遇到了基本问题.我想java.util.UUID在这个小类中将类实例序列化为final字段:
@Root
public class Identity {
@Attribute
private final UUID id;
public Identity(@Attribute UUID id) {
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
教程显示了如何通过注册转换器来序列化第三方对象:
Registry registry = new Registry();
registry.bind(UUID.class, UUIDConverter.class);
Strategy strategy = new RegistryStrategy(registry);
Serializer serializer = new Persister(strategy);
serializer.write( object, stream );
Run Code Online (Sandbox Code Playgroud)
适用于UUID的转换器非常简单:
public class UUIDConverter implements Converter<UUID> {
@Override
public UUID read(InputNode node) throws Exception {
return new UUID.fromString(node.getValue());
}
@Override
public void write(OutputNode node, UUID value) throws Exception {
node.setValue(value.toString());
} …Run Code Online (Sandbox Code Playgroud) 标准Runnable接口只有非参数化run()方法.还有通用类型返回结果的方法Callable<V>接口call().我需要传递泛型参数,如下所示:
interface MyRunnable<E> {
public abstract void run(E reference);
}Run Code Online (Sandbox Code Playgroud)
是否有任何标准接口用于此目的,或者我必须自己声明基本接口? 我想将任何枚举值传递给实用程序类中的方法,并获取相同枚举类型的另一个枚举值.像这样的东西:
public class XMLUtils {
public static Enum<?> getEnumAttribute(Element element, String name,
Enum<?> defaultValue) {
if (element.hasAttribute(name)) {
String valueName = element.getAttribute(name);
// search for value
for (Enum<?> value: defaultValue.getClass().getEnumConstants())
if (value.toString().equalsIgnoreCase(valueName))
return value;
}
// not found, return default value
return defaultValue;
}
}
Run Code Online (Sandbox Code Playgroud)
使用方法getEnumAttribute():
// simple enum
public enum EUploadMethod {
INSERT, UPDATE, DELETE
}
// read enum value from XML config file
EUploadMethod method = XMLUtils.getEnumAttribute(element, "method",
EUploadMethod.INSERT);
Run Code Online (Sandbox Code Playgroud)
这段代码功能齐全,Eclipse编译并运行它没有警告或错误,它就像一个魅力.
但是当我通过Maven2从命令行清理和编译项目时,它会因为 …