SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Bug Cor 54

Inspired by

Original Description

“This call to a generic collection method contains an argument with an incompatible class from that of the collection's parameter (i.e., the type of the argument is neither a supertype nor a subtype of the corresponding generic type argument). Therefore, it is unlikely that the collection contains any objects that are equal to the method argument used here. Most likely, the wrong value is being passed to the method.

In general, instances of two unrelated classes are not equal. For example, if the Foo and Bar classes are not related by subtyping, then an instance of Foo should not be equal to an instance of Bar. Among other issues, doing so will likely result in an equals method that is not symmetrical. For example, if you define the Foo class so that a Foo can be equal to a String, your equals method isn't symmetrical since a String can only be equal to a String.

In rare cases, people do define nonsymmetrical equals methods and still manage to make their code work. Although none of the APIs document or guarantee it, it is typically the case that if you check if a Collection<String> contains a Foo, the equals method of argument (e.g., the equals method of the Foo class) used to perform the equality checks. ” – 2013-11-25

Detailed Description

This call to a generic collection's method would always return false if the type of the parameter of the generic method has no relations to the type of the argument.

Sample Problem Scenario

<code Java> public void wrong1(){

Collection<Integer> colInt = new LinkedList<Integer>();
String collStr = "Test";
colInt.contains(collStr);

} </Code>

<code Java> public void wrong2(String collStr){

Collection<Integer> colInt = new LinkedList<Integer>();	
colInt.contains(collStr);

} </Code>

<code Java> private Collection<String> collstr; public void wrong3(){

Collection<Integer> colInt = new LinkedList<Integer>();
colInt.contains(collstr);

} </Code>

Sample Counter Scenario

Sample Fix

Let the programmer to decide the type.

Evaluation Results

Benchmark project Precision Recall
FB JT Delta FB JT Delta
Project … …% …% …% …% …% …%
Project … …% …% …% …% …% …%

FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBugs

teaching/labs/mdse/2013/bug_descriptions/jt-bug-cor54.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023