我想知道您在比较两个字符串或构建 Stringbuilder 时是否遇到过此类问题。
Correctness - Call to equals() comparing different types findbugs : EC_UNRELATED_TYPES This method calls equals(Object) on two references of different class types with no common subclasses. Therefore, the objects being compared are unlikely to be members of the same class at runtime (unless some application classes were not analyzed, or dynamic class loading can occur at runtime). According to the contract of equals(), objects of different classes should always compare as unequal; therefore, according to the contract defined by java.lang.Object.equals(Object), the result of this comparison will always be false at runtime.
@SuppressWarnings("unchecked")
public List<MaterialUsuarioEO>buscar(FiltroMaterialUsuarioDto filtro){
String vacio="";
StringBuilder sb = new StringBuilder("Select mu from MaterialUsuarioEO mu where 1=1");
if (filtro.getMaterial() != null && !vacio.equals(filtro.getMaterial().getId())) {
sb.append("and upper(mu.material.id) like :id ");
}
if (filtro.getUsuario() != null && !vacio.equals(filtro.getUsuario().getNombre())) {
sb.append("and upper(mu.usuario.nombre) like :nombre ");
}
Query q = em.createQuery(sb.toString());
if (filtro.getMaterial() != null && !"".equals(filtro.getMaterial().getId())) {
q.setParameter("id", "%" + filtro.getMaterial().getId().toUpperCase() + "%");
}
if (filtro.getUsuario() != null && !"".equals(filtro.getUsuario().getNombre())) {
q.setParameter("nombre", "%" + filtro.getUsuario().getNombre().toUpperCase() + "%");
}
return q.getResultList();
}
顺便说一句,我使用 SonarQube 工具得到了这个。不,我确信它正在比较两个字符串,所以我仍然没有线索,不仅我无法确定我失败的地方,而且我也不知道问题试图告诉我什么。
同时我会继续寻找,如果找到我会发布它,仍然会感谢您的更多方法,谢谢。
最佳答案
这些属性是什么类型?
filtro.getUsuario().getNombre() filtro.getMaterial().getId()
如果它们不是 String 类型,则不应通过 equals 来比较它们。 但您可以执行以下操作:
Integer.valueOf(vacio).equals(filtro.getUsuario().getNombre())
因此 vacio 的字符串值被转换为整数,因此可以通过 equals 对照另一个整数 (getNombre()) 进行检查。
关于java - 声纳问题 : Correctness - Call to equals() comparing different types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33437206/