Skip to content

Java: Static method access with explicit qualifier on generic type claims declaring type is raw type #5593

@Marcono1234

Description

@Marcono1234

Description

When a static method of a generic type is accessed with an explicit qualifier (type access or instance), then CodeQL reports the getDeclaringType() (and getReceiverType()) as raw type.
This is incorrect and also pretty problematic because raw types have a <> suffix in their name; therefore queries checking for a specific qualified name will most likely not match, causing false negatives (see #5521).

Affected cases

Method

  • Explicit type access qualifier for static method, e.g. Generic.staticMethod()
    • Questionable: getReceiverType() reports raw type
      Should probably report generic type
    • getMethod().getDeclaringType() reports raw type
  • Raw type instance qualifier for static method, e.g. Generic raw = ...; raw.staticMethod()
    • getMethod().getDeclaringType() reports raw type

Field

  • Explicit type access qualifier for static field, e.g. Generic.staticField = 1
    • Questionable: getQualifier().getType() reports raw type (though getField() is correct)
      Should probably report generic type

Example

class Generic<T> {
    static void doSomething() { }
    static <T> void doSomethingT() { }
    static String staticF;
    String f;
    
    void test() {
        // GOOD: These report generic type
        doSomething();
        doSomethingT();
        /*
         * BAD: Using declaring type as qualifier causes `getReceiverType()` and
         * `getMethod().getDeclaringType()` to have raw type as result
         */
        Generic.doSomething();
        Generic.doSomethingT();
        // GOOD: These report generic type
        this.doSomething();
        this.doSomethingT();
        
        // GOOD: For fields result of `getField()` seems to be always correct
        staticF = "";
        /*
         * QUESTIONABLE: When declaring type is used as qualifier, `getQualifier()`
         * has raw type (though `getField()` is correct)
         */
        Generic.staticF = "";
        // GOOD: Generic type is reported
        this.staticF = "";
    }
    
    void testRaw(Generic raw) {
        // GOOD: Raw type is reported
        raw.test();
        /*
         * BAD: Accessing static method with raw type as qualifier causes
         * `getMethod().getDeclaringType()` to have raw type as result
         */
        raw.doSomething();
        raw.doSomethingT();
        
        // GOOD: Result of `getField()` is correct, and `getQualifier()` type is raw
        raw.f = "";
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    JavaquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions