google / google/error-prone

False positive FunctionalInterfaceMethodChanged when signature changes

Open
#1,410 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

### Description of the problem:

When a functional interface `A` is extended by another functional interface `B` whose abstract method has an incompatible signature, there is no danger of a `B` lambda being cast to `A`. (The possibility of such a cast is what FunctionalInterfaceMethodChanged complains about.)

### What's the simplest, easiest way to reproduce this bug?

Here's the actual code that triggered the false positive for us. Spring MVC defines a simple [StreamingResponseBody](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBody.html) functional interface. I wrote the following adapter that permits us to write character data instead of just bytes.

```java
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

/**
* A {@link StreamingResponseBody} that provides a UTF-8 {@link Writer} for convenience.
*/
@FunctionalInterface
public interface StreamingUtf8ResponseBody extends StreamingResponseBody {
void writeTo(Writer writer) throws IOException;

@Override
default void writeTo(OutputStream outputStream) throws IOException {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
writeTo(writer);
}
}
}
```

### What version of Error Prone are you using?

2.3.3

### Have you found anything relevant by searching the web?

No.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.