github / github/codeql

Shadowing happens when overriding method

Open
#15,525 3 comments 0 reactions 0 assignees View on GitHub
Java question
Dominant language
CodeQL
Stars
10.1k
Forks
2.1k
Avg merge
2d 15h
Merged PRs (30d)
141

Description

Consider the following example:

##### MyCallable.qll
```codeql
import java

class CallableByErasure extends Callable {
override predicate hasQualifiedName(string package, string type, string name) {
this.isDeclaredIn(package, type) and this.hasName(name)
}

predicate isDeclaredIn(string package, string type) {
this.getDeclaringType().getErasure().(RefType).hasQualifiedName(package, type)
}
}

```
##### Query.ql
```codeql
import java
// import MyCallable

from Call c
where c.getCallee().hasQualifiedName("java.util", "Set", "iterator")
select c

```

The query correctly finds one instance (see test case below).
If I uncomment `import MyCallable` however, then the query does not return anything. Is this expected behaviour? How do I prevent this from happening?

This is the test case I used:
```java
import java.util.HashSet;
import java.util.Set;

class GenericsClass {
public void foo() {
Set set = new HashSet();
set.iterator();
}
}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with MyCallable.qll, Query.ql, and the provided GenericsClass Java example. Compare the query result with and without the MyCallable import, then inspect the overriding hasQualifiedName and isDeclaredIn predicates; done means the shadowing behavior is understood and the query behaves as intended in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.