Skip to content

Commit

Permalink
fix(VariableAccessFilter): ensure all accesses are returned for gener…
Browse files Browse the repository at this point in the history
…ic fields #5391
  • Loading branch information
Luro02 committed Aug 27, 2023
1 parent 2c64874 commit 7e6792c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
import spoon.reflect.visitor.Filter;

/**
* This simple filter matches all the accesses to a given field.
* This simple filter matches all the accesses to a given variable.
*/
public class VariableAccessFilter<T extends CtVariableAccess<?>> implements Filter<T> {
CtVariableReference<?> variable;

/**
* Creates a new field access filter.
* Creates a new variable access filter.
*
* @param variable
* the accessed variable
* @param variable the variable to find accesses to, must not be {@code null}
*/
public VariableAccessFilter(CtVariableReference<?> variable) {
if (variable == null) {
Expand All @@ -32,7 +31,20 @@ public VariableAccessFilter(CtVariableReference<?> variable) {

@Override
public boolean matches(T variableAccess) {
return variable.equals(variableAccess.getVariable());
return this.variable.equals(variableAccess.getVariable())
// if this.variable is a reference to a generic field, then the reference of the variableAccess might not
// be equal.
//
// For example:
//
// Given class A<T> { T t }, the reference would be to `T t`,
// but an access to `t` could look like this:
// `A<String> a = new A<>(); a.t = "foo";`
// ^^^ reference to `String t`
// => (String t) != (T t), but the same variable is accessed
// Therefore, it is checked that the variable they are referencing is the same.
|| this.variable.getDeclaration() != null
&& this.variable.getDeclaration().equals(variableAccess.getVariable().getDeclaration());
}

}

0 comments on commit 7e6792c

Please sign in to comment.