Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 411891c

Browse files
committed
Java: Don't inherit methods from co-/contra-variant supertypes.
1 parent 2f0e693 commit 411891c

3 files changed

Lines changed: 15 additions & 20 deletions

File tree

java/ql/src/semmle/code/java/Type.qll

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -435,22 +435,6 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
435435
hasInterfaceMethod(m, declaringType) and hidden = false
436436
}
437437

438-
private predicate noMethodExtraction() {
439-
not methods(_, _, _, _, this, _) and
440-
exists(Method m | methods(m, _, _, _, getSourceDeclaration(), _) and m.isInheritable())
441-
}
442-
443-
private predicate canInheritFromSupertype(RefType sup) {
444-
sup = getASupertype() and
445-
(noMethodExtraction() implies supertypeSrcDecl(sup, getSourceDeclaration()))
446-
}
447-
448-
pragma[nomagic]
449-
private predicate supertypeSrcDecl(RefType sup, RefType srcDecl) {
450-
sup = getASupertype() and
451-
srcDecl = sup.getSourceDeclaration()
452-
}
453-
454438
private predicate hasNonInterfaceMethod(Method m, RefType declaringType, boolean hidden) {
455439
m = getAMethod() and
456440
this = declaringType and
@@ -464,7 +448,7 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
464448
else h1 = false
465449
) and
466450
(not sup instanceof Interface or this instanceof Interface) and
467-
canInheritFromSupertype(sup) and
451+
this.extendsOrImplements(sup) and
468452
sup.hasNonInterfaceMethod(m, declaringType, h2) and
469453
hidden = h1.booleanOr(h2) and
470454
exists(string signature |
@@ -491,7 +475,7 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
491475
exists(RefType sup |
492476
sup.interfaceMethodCandidateWithSignature(m, signature, declaringType) and
493477
not cannotInheritInterfaceMethod(signature) and
494-
canInheritFromSupertype(sup) and
478+
this.extendsOrImplements(sup) and
495479
m.isInheritable()
496480
)
497481
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| NamingTest.java:4:17:4:22 | equals | Method NamingTest.equals(..) could be confused with overloaded method $@, since dispatch depends on static types. | NamingTest.java:3:17:3:22 | equals | equals |
1+
| NamingTest.java:7:17:7:22 | equals | Method NamingTest.equals(..) could be confused with overloaded method $@, since dispatch depends on static types. | NamingTest.java:6:17:6:22 | equals | equals |
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import java.util.*;
2+
import java.util.function.*;
3+
import java.util.stream.*;
14

25
public class NamingTest {
36
public boolean equals(Object other) { return false; }
47
public boolean equals(NamingTest other) { return true; }
5-
8+
69
public void visit(Object node) {}
710
public void visit(NamingTest t) {}
11+
12+
public class Elem<T> {}
13+
14+
public Object get(List<Elem<String>> lll) {
15+
Predicate<?> p = null;
16+
p.test(null);
17+
return lll.stream().map(l -> l).filter(Objects::nonNull).collect(Collectors.toList());
18+
}
819
}

0 commit comments

Comments
 (0)