我想知道你在比较两个字符串或构建一个Stringbuilder时是否遇到过这种问题。
@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(); }正确性 - 调用equals()比较不同类型findbugs:EC_UNRELATED_TYPES此方法在不包含公共子类的不同类类型的两个引用上调用equals(Object)。 因此,被比较的对象在运行时不可能是同一类的成员(除非某些应用程序类未被分析,或者在运行时可能发生动态类加载)。 根据equals()的契约,不同类别的对象应该总是比较为不相等; 因此,根据java.lang.Object.equals(Object)定义的契约,此比较的结果在运行时始终为false。
顺便说一下,我使用SonarQube工具获得了这一点。 不,我确定它是比较两个字符串,所以我仍然没有线索,不仅我无法确定我失败的位置,而且我也不知道问题想告诉我什么。
我会继续寻找,如果我找到它,我会发布它,仍然会感谢你的一些更多的方法,谢谢。
I wonder if you've ever encountered this kind of isssue when comparing two Strings or building a Stringbuilder.
@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(); }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.
BTW I get this using the SonarQube tools. And no, I'm sure It's comparing two String, so I still don't have a clue, not only I can't determine where I'm failing at but also I don't know what the problem is trying to tell me.
I will keep looking in the meanwhile, and if I find it I'll post it, still would thank some more of your approaches, thanks.
最满意答案
这些属性有哪些类型?
filtro.getUsuario()。getNombre()filtro.getMaterial()。getId()
如果它们不是String类型,则不应通过等号进行比较。 但是你可以做到以下几点:
Integer.valueOf(vacio).equals(filtro.getUsuario().getNombre())
因此,vacio的String值被转换为Integer,因此可以通过equals检查另一个Integer(getNombre())。
what kind of types are these attributes ?
filtro.getUsuario().getNombre() filtro.getMaterial().getId()
If they're not of type String you shouldn't compare them via equals. But you could do the following:
Integer.valueOf(vacio).equals(filtro.getUsuario().getNombre())
So the String value of vacio is being cast into an Integer and therefore can be checked against another Integer (getNombre()) via equals.
更多推荐
发布评论