Skip to content

Commit

Permalink
SONARJAVA-4590 S1319: Method parameter uses method not available in i…
Browse files Browse the repository at this point in the history
…nterface (#4558)
  • Loading branch information
kaufco authored Nov 21, 2023
1 parent b450874 commit 1b25ff2
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
Expand Down Expand Up @@ -41,7 +44,7 @@ public EmployeesTopLevel(String s) { }
public ConcurrentHashMap<?,?> concurrentHashMap() { return null; } // Noncompliant [[sc=10;ec=27;quickfixes=ConcurrentHashMap]] {{The return type of this method should be an interface such as "ConcurrentMap" rather than the implementation "ConcurrentHashMap".}}
// fix@ConcurrentHashMap {{Replace "ConcurrentHashMap" by "ConcurrentMap"}}
// edit@ConcurrentHashMap [[sc=10;ec=27]] {{ConcurrentMap}}
// edit@ConcurrentHashMap [[sl=9;sc=47;el=9;ec=47]] {{\nimport java.util.concurrent.ConcurrentMap;}}
// edit@ConcurrentHashMap [[sl=12;sc=47;el=12;ec=47]] {{\nimport java.util.concurrent.ConcurrentMap;}}

public ConcurrentSkipListMap concurrentSkipListMap() { return null; } // Noncompliant {{The return type of this method should be an interface such as "ConcurrentMap" rather than the implementation "ConcurrentSkipListMap".}}

Expand All @@ -52,7 +55,6 @@ class A {
public void foo(HashMap<String, String> map) { } // Noncompliant [[sc=21;ec=28;quickfixes=HashMap]]
// fix@HashMap {{Replace "HashMap" by "Map"}}
// edit@HashMap [[sc=21;ec=28]] {{Map}}
// edit@HashMap [[sl=5;sc=29;el=5;ec=29]] {{\nimport java.util.Map;}}
}

class B extends A {
Expand All @@ -71,3 +73,77 @@ public Set<Employee> getEmployees(){ // Compliant
return employees;
}
}

abstract class ApiEnforcesClassSonarjava4590 {

public void foo1(TreeMap<String, String> map) { } // Noncompliant, no TreeMap specific API used

public Map.Entry<String, String> foo2(TreeMap<String, String> map) { // Compliant, TreeMap specific API used
return map.lowerEntry("bar");
}

public void foo3(TreeSet<Integer> set) { } // Noncompliant, no TreeSet specific API used

public Integer foo4(TreeSet<Integer> set) { // Compliant, TreeSet specific API used
return set.ceiling(42);
}

public Integer foo5(TreeSet<Integer> set) { // Noncompliant, no TreeSet specific API used
return set.size();
}

public Integer foo6(LinkedList<Integer> dq, boolean condition) { // Compliant, LinkedList (Queue) specific API used
if (condition) {
return dq.size();
} else {
var result = 23 + dq.poll() * 42;
return result;
}
}

public Integer foo7(LinkedList<Integer> dq, boolean condition) { // Noncompliant, no LinkedList specific API used
if (condition) {
return dq.size();
} else {
var result = (Integer) (dq.toArray()[23]);
return result;
}
}

public Map.Entry<String, String> foo8(TreeMap<String, String> map) { // Noncompliant due to current limitation of the rule
TreeMap<String, String> map2 = map;
return map2.lowerEntry("bar");
}

public Map.Entry<String, String> foo9(TreeMap<String, String> map) { // Noncompliant due to current limitation of the rule
var map2 = map;
return map2.lowerEntry("bar");
}

public Integer foo10(LinkedList<Integer> l1, LinkedList<Integer> l2, LinkedList<Integer> l3) { // Compliant, LinkedList (Queue) specific API used
return l1.poll() + l2.poll() + l3.poll();
}

public Integer foo11(LinkedList<Integer> l1, LinkedList<Integer> l2, LinkedList<Integer> l3) { // Noncompliant [[sc=48;ec=58]]
return l1.poll() + l2.get(0) + l3.poll();
}

// Noncompliant@+2 [[sc=24;ec=34]]
// Noncompliant@+1 [[sc=72;ec=82]]
public Integer foo12(LinkedList<Integer> l1, LinkedList<Integer> l2, LinkedList<Integer> l3) {
return l1.get(0) + l2.poll() + l3.get(0);
}

// Noncompliant@+3 [[sc=33;ec=43]]
// Noncompliant@+2 [[sc=57;ec=67]]
// Noncompliant@+1 [[sc=81;ec=91]]
public abstract Integer foo13(LinkedList<Integer> l1, LinkedList<Integer> l2, LinkedList<Integer> l3);

public Integer foo14(LinkedList<Integer> list) { // Noncompliant [[sc=24;ec=34]]
return getList().poll();
}

private LinkedList<Integer> getList() {
return null;
}
}
Loading

0 comments on commit 1b25ff2

Please sign in to comment.