Sun JDK 1.5 compiler BUG: JDK-6302954 : Inference fails for type variable return constraint
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954
编译报错如下:
type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds
@SuppressWarnings("unchecked")
public static <T> T readField(Field field, Object target, boolean forceAccess)
throws IllegalAccessException {
if (field == null) {
throw new IllegalArgumentException("The field must not be null");
}
if (forceAccess && !field.isAccessible()) {
field.setAccessible(true);
} else {
setAccessibleWorkaround(field);
}
return (T) field.get(target);
}
public static <T> T readField(Field field, Object target)
throws IllegalAccessException {
return FieldUtils.readField(field, target, false); //这里编译报错
}
修正代码:
public static <T> T readField(Field field, Object target)
throws IllegalAccessException {
return FieldUtils.<T> readField(field, target, false);
}
FieldUtils.<T> readField(field, target, false)