Skip to content

[java] Unresolved target type for lambdas make overload resolution fail #5338

@oowekyala

Description

@oowekyala

In the following UnusedPrivateMethod considers createListener unused if BytesParser is missing from the classpath. This is because when the target type for a lambda is unresolved (here the target type is BytesParser), we consider that the lambda does not match that type, as it is not a functional interface. I think when the target type is unresolved then we should assume it could be a functional interface that matches, and not fail the inference outright. This would allow createListener to resolve properly to its overload, even if eg the lambda parameter bytes would have an unknown type.


I encountered such an interesting situation: try check file BadClass.java

package com.semrush.siteaudit.crowly.kafka.component;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.concurrent.CompletableFuture;

/**
 * Test.
 */
@ExtendWith(MockitoExtension.class)
class SomeTest {

    @Test
    void test() {
        final BytesListener listener = createListener(
            (bytes) -> CompletableFuture.completedFuture("HI!")
        );
        Assertions.assertNotNull(listener.onRecord(new byte[0]));
    }

    private static BytesListener createListener(
        BytesParser<String> parser
    ) {
        return bytes -> parser
            .parse(bytes)
            .thenAccept(System.out::println);
    }
}

OK - no issues, if there is a class BytesParser.java in ClassPath.

As soon as I delete the class BytesParser.java (argument type) thatUnusedPrivateMethod is triggered.

Avoid unused private methods such as 'createListener(BytesParser<String>)'

Removing the second class BytesListener.java (method return type) does not trigger the issue tUnusedPrivateMethod.

Why is it important for a static analyzer to have a neighbor class?

Originally posted by @deripas in #5184 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions