我真的想喜欢仿制药,但到目前为止,他们已经胜过任何利益造成的麻烦。请告诉我我错了。铸造一个可比,然后比较
我理解增加@SuppressWarnings的必要性(“未登记”)使用无通用的框架时(春,休眠)。这一点确实降低了泛型的价值,正如要求将类传递给构造函数以避免删除的缺陷一样。然而,真正的刺总是似乎在铸造。我通常会尝试一段时间以获得正确的语法,但是放弃纯粹的尝试,添加@SuppressWarnings,并继续我的生活。
这里有一个例子:我反映了一个bean来寻找两个实例之间的差异。一些属性实现Comparable,使得(a.equals(b)== false)但(a.compareTo(b)== 0)(例如BigDecimal,Date)。在这些情况下,我希望该财产被视为相同。
MyObject original = getOriginal();
MyObject updated = getUpdated();
for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(MyObject.class)) {
// Assume I'm putting in the try/catch block
Object pOriginal = pd.getReadMethod().invoke(original, (Object[]) null);
Object pUpdated = pd.getReadMethod().invoke(updated, (Object[]) null);
boolean isPropertySame;
if (Comparable.class.isAssignableFrom(pOriginal.getClass())) {
// Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized
isPropertySame = Comparable.class.cast(pOriginal).compareTo(Comparable.class.cast(pUpdated)) == 0;
// The method compareTo(capture#19-of ?) in the type Comparable<capture#19-of ?> is not applicable for the arguments (capture#21-of ? extends Comparable)
Comparable<?> comparable = Comparable.class.cast(pOriginal);
isPropertySame = comparable.compareTo(comparable.getClass().getTypeParameters()[0].getGenericDeclaration().cast(pUpdated)) == 0;
// Even if I get the generics right, I still get an error if pOriginal is java.sql.Timestamp and pUpdated is java.util.Date (happens all the time with Hibernate).
isPropertySame = (help);
} else {
isPropertySame = pOriginal.equals(pUpdated);
}
if (!isPropertySame) {
PropertyDelta delta = new PropertyDelta(pd, pOriginal, pUpdated);
dao.save(delta);
}
}
关于我可以投入(帮助)的任何想法?
'Timestamp'和'Date'的问题是一个众所周知的问题:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4631234。它已经在Java 6中修复(可能也在5u6)。 – notnoop 2009-08-06 22:00:54
变量“originalValue”和“updatedValue”分别应该是“pOriginal”和“pUpdated”吗? – erickson 2009-08-06 22:02:08
我很高兴Timestamp/Date混淆被修复。恐怕它不能帮助我,因为我被困在1.5,但很高兴知道未来。 – 2009-08-06 22:32:39