声纳问题:正确性

编程入门 行业动态 更新时间:2024-10-21 18:29:56
声纳问题:正确性 - 调用equals()比较不同的类型(Sonar Issue: Correctness - Call to equals() comparing different types)

我想知道你在比较两个字符串或构建一个Stringbuilder时是否遇到过这种问题。

正确性 - 调用equals()比较不同类型findbugs:EC_UNRELATED_TYPES此方法在不包含公共子类的不同类类型的两个引用上调用equals(Object)。 因此,被比较的对象在运行时不可能是同一类的成员(除非某些应用程序类未被分析,或者在运行时可能发生动态类加载)。 根据equals()的契约,不同类别的对象应该总是比较为不相等; 因此,根据java.lang.Object.equals(Object)定义的契约,此比较的结果在运行时始终为false。

@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工具获得了这一点。 不,我确定它是比较两个字符串,所以我仍然没有线索,不仅我无法确定我失败的位置,而且我也不知道问题想告诉我什么。

我会继续寻找,如果我找到它,我会发布它,仍然会感谢你的一些更多的方法,谢谢。

I wonder if you've ever encountered this kind of isssue when comparing two Strings or building a 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(); }

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.

更多推荐

本文发布于:2023-08-05 00:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声纳   正确性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!