Skip to content

[Java] Question on Overrides of Generic Methods #3490

@intrigus-lgtm

Description

@intrigus-lgtm

Description of the issue
https://help.semmle.com/QL/learn-ql/java/types-class-hierarchy.html says:

/** class representing java.util.Collection.toArray(T[]) */
class CollectionToArray extends Method {
   CollectionToArray() {
       this.getDeclaringType().hasQualifiedName("java.util", "Collection") and
       this.hasName("toArray") and
       this.getNumberOfParameters() = 1
   }
}

/** class representing calls to java.util.Collection.toArray(T[]) */
class CollectionToArrayCall extends MethodAccess {
   CollectionToArrayCall() {
       exists(CollectionToArray m |
           this.getMethod().getSourceDeclaration().overrides*(m)
       )
   }

   /** the call's actual return type, as determined from its argument */
   Array getActualReturnType() {
       result = this.getArgument(0).getType()
   }
}

Notice the use of getSourceDeclaration and overrides in the constructor of CollectionToArrayCall: we want to find calls to Collection.toArray and to any method that overrides it, as well as any parameterized instances of these methods. In our example above, for instance, the call l.toArray resolves to method toArray in the raw class ArrayList.

Running this simple query has zero results.
If the query is modified to not require that the method is declared in a specific type,
it has exactly one result in UniquePropertyMessageEvictionStrategy.java

Question:
Why does the query, that requires the method to be declared in a specific type, not work?
If I understand the documentation correctly, this should work.
Am I misunderstanding what " and to any method that overrides it" means?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions