【发布时间】:2019-02-21 03:47:13
【问题描述】:
public abstract class Formatter<P, R> {
public static Object format(Object src, Class<? extends Formatter> formatter) {
Formatter fmt;
try {
fmt = formatter.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return fmt.format(src); // Unchecked call warning here
}
public abstract R format(P src);
}
如何在调用时避免泛型警告
fmt.format(src)
格式化程序是格式化程序的子类,定义如下
PhoneFormatter extends Formatter<String, String>
这是调用代码
if (annotation instanceof LogFormat) {
LogFormat logFormat = (LogFormat) annotation;
arg = Formatter.format(arg, logFormat.formatter());
}
-
public class User {
@LogFormat(formatter = PhoneNumberFormatter.class)
private String mobile;
}
我不想(或者说我不能)使用任何类型参数调用此方法。
【问题讨论】:
-
邮政编码作为文本,而不是作为图像。
-
警告是什么?
-
这个方法有什么用?为什么你一方面使用泛型,然后这样做
Object-Object-mapping 放弃所有类型安全? -
@luk2302 其实我找不到通用的方法来定义格式方法。