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

EI_EXPOSE_REP2 being reported based off purely on method name #1797

Open
pnsantos opened this issue Nov 5, 2021 · 7 comments
Open

EI_EXPOSE_REP2 being reported based off purely on method name #1797

pnsantos opened this issue Nov 5, 2021 · 7 comments
Assignees

Comments

@pnsantos
Copy link

pnsantos commented Nov 5, 2021

Not really sure if this is a bug or a feature, but it seems spotbugs (4.3+) is reporting EI_EXPOSE_REP2 purely based off the method name. For example I have the following:

public class SomeClass {

  public CompletableFuture<Void> delete(String id) {
    // delete something in the db
  }

}

and then

public class AnotherClass {

  private final SomeClass repo;

  public AnotherClass(SomeClass repo) {
    this.repo = repo;
  }

}
  • Spotbugs 4.2 reports no violations
  • Spotbugs 4.3+ reports EI_EXPOSE_REP2 (tested all the way to 4.4.2)

However if I rename delete to something else (say deleete) then it no longer reports EI_EXPOSE_REP2 which makes me think it's just looking at the method name?

Kinda makes this Bug useless... All our projects are suddenly reporting hundreds of EI_EXPOSE_REP2

Is this expected behaviour?

@welcome
Copy link

welcome bot commented Nov 5, 2021

Thanks for opening your first issue here! 😃
Please check our contributing guideline. Especially when you report a problem, make sure you share a Minimal, Complete, and Verifiable example to reproduce it in this issue.

vilppuvuorinen added a commit to jubicoy/snoozy-starter that referenced this issue Dec 13, 2021
- Upgrade easyvalue to 1.3.6 due to default value bug.

- Upgrade easymapper to 0.5.2 due to the easyvalue bug mentioned above.

- Upgrade easyconfig to 0.8.4 to revert HikariCP upgrade that breaks
slf4j 1.7 compatibility.

- Downgrade spotbugs-maven-plugin to 4.2.3 due to an EI_EXPOSE_REP2 bug
(spotbugs/spotbugs#1797).

- Upgrade checkstyle to 9.2.
@pensionarchitects-ben
Copy link

We also have a problem with this, we upgraded to spotbugs 4.5.3 (from 4.0.0) and suddenly hundreds of errors while most of them are false positives. we disabled the warning for now. Subscribed to hear about progress on this.

@brunomuniz-encora
Copy link

Same here. Lots of reports of EI_EXPOSE_REP2 with the new versions of spotbugs, some might be false positives. Also subscribing to hear about progress on this.

@rratliff
Copy link

I found a workaround. Using the original poster's example:

public class AnotherClass {

  private final SomeClass repo;

  public AnotherClass(SomeClass repo) {
    this.repo = Objects.requireNonNull(repo, "repo");
  }

}

It seems wrapping it in Objects.requireNonNull() is sufficient to thwart the detection of this "bug."

@gtoison
Copy link
Contributor

gtoison commented Aug 9, 2023

MutableClasses.looksLikeASetter is used to detect a mutable class, it checks if a method name starts with one of the suspicious prefixes and if the return type is different compared to the parameter type:

public static boolean looksLikeASetter(String methodName, String classSig, String retSig) {
if (Objects.equals(classSig, retSig)) {
return false;
}
return SETTER_LIKE_PREFIXES.stream().anyMatch(name -> methodName.startsWith(name));

private static final List<String> SETTER_LIKE_PREFIXES = Arrays.asList(
"set", "put", "add", "insert", "delete", "remove", "erase", "clear", "push", "pop",
"enqueue", "dequeue", "write", "append", "replace");

I think the heuristic is a bit too aggressive here: considering the interest in this issue, there are many false positives detected.
@baloghadamsoftware what do you think?

@hazendaz
Copy link
Member

fixed via #2514

@hazendaz hazendaz self-assigned this Aug 15, 2023
@gtoison
Copy link
Contributor

gtoison commented Aug 16, 2023

Hello @hazendaz, the issue is not fixed unfortunately: #2514 mitigates the problem for enums and records, but other classes might be recognized as mutable solely because they have a method whose name starts with one of the suspcious prefixes:

private static final List<String> SETTER_LIKE_PREFIXES = Arrays.asList(
"set", "put", "add", "insert", "delete", "remove", "erase", "clear", "push", "pop",
"enqueue", "dequeue", "write", "append", "replace");

One way to improve would be to let the users supply their own "setter like prefixes"?

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

6 participants