Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NonApiType reported for overriden methods #4563

Open
Marcono1234 opened this issue Sep 6, 2024 · 0 comments
Open

NonApiType reported for overriden methods #4563

Marcono1234 opened this issue Sep 6, 2024 · 0 comments

Comments

@Marcono1234
Copy link
Contributor

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

import java.util.ArrayList;

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

  @SuppressWarnings("unchecked")
  public static <T> ObjectFactory<T> createFactory(Class<T> c) {
    if (c.isAssignableFrom(ArrayList.class)) {
      return (ObjectFactory<T>) new ObjectFactory<ArrayList<Object>>() {
        @Override
        // ERROR-PRONE: Reports NonApiType here
        public ArrayList<Object> 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<String> getResult() is overridden as ArrayList<String> getResult().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant