Skip to content

Java: FunctionalInterface does not find all functional interfaces #3418

@Marcono1234

Description

@Marcono1234

It appears FunctionalInterface from semmle.code.java.dispatch.WrappedInvocation does not find all functional interfaces. In particular it seems for some reason it does not consider an interface a functional interface when it contains static fields. And it might also treat default methods incorrectly, not considering an interface a functional interface as soon as it declares or inherits a default method.

The query below contains a hopefully correct implementation and also shows which functional interfaces are not found:

import java
import semmle.code.java.dispatch.WrappedInvocation

// From WrappedInvocation
private string getAPublicObjectMethodSignature() {
    exists(Method m |
        m.getDeclaringType() instanceof TypeObject and
        m.isPublic() and
        result = m.getSignature()
    )
}

private Method getAnAbstractMethod(Interface interface) {
    interface.inherits(result)
    and result.isAbstract()
    // JLS §9.8, ignore Object methods
    and not result.getSignature() = getAPublicObjectMethodSignature()
    // Make sure that there is no other non-abstract method
    // (e.g. `default`) which overrides the abstract one
    and not exists (Method m |
        interface.inherits(m)
        and not m.isAbstract()
        and m.overrides(result)
    )
}

class FunctionalInterface2 extends Interface {
    FunctionalInterface2() {
        1 = strictcount(getAnAbstractMethod(this))
    }
}

from FunctionalInterface2 i
where
    not i instanceof FunctionalInterface
select i

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