google / google/error-prone

NonApiType reported for overriden methods

Open
#4,563 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

## Version
Error Prone: 2.31.0
JDK: 17

## Description
The pattern `NonApiType` is reported for overridden methods for some types (e.g. `ArrayList`). For overridden methods it might not be possible to change the types. Maybe it is possible if type variables are used but that might imply other changes, and might therefore be out of scope for `NonApiType`.

## Example
```java
import java.util.ArrayList;

public abstract class ObjectFactory {
public abstract T create();

@SuppressWarnings("unchecked")
public static ObjectFactory createFactory(Class c) {
if (c.isAssignableFrom(ArrayList.class)) {
return (ObjectFactory) new ObjectFactory>() {
@Override
// ERROR-PRONE: Reports NonApiType here
public ArrayList create() {
return new ArrayList<>();
}
};
}
throw new UnsupportedOperationException();
}
}
```

## Expected behavior
Either overridden methods should be completely ignored, or maybe more sophisticated detection is needed which only reports this pattern if a covariant return type (more specific return type than needed) is used. For example if a method `List getResult()` is overridden as `ArrayList getResult()`.

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.