Skip to content

Enable & fix inspection: Object instantiation inside equals() or hashCode() #2822

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

Merged

Conversation

Bennett-Lynch
Copy link
Contributor

Reports construction of (temporary) new objects inside equals(),
hashCode(), compareTo(), and Comparator.compare() methods.

Besides constructor invocations, new objects can also be created by
autoboxing or iterator creation inside a foreach statement. This can
cause performance problems, for example, when objects are added to a Set
or Map, where these methods will be called often.

The inspection will not report when the objects are created in a throw
or assert statement.

Example:

  class Person {
    private String name;
    private int age;

    public boolean equals(Object o) {
      return Arrays.equals(new Object[] {name, age}, new Object[] {((Foo)o).name, ((Foo)o).age});
    }

    public int hashCode() {
      return (name + age).hashCode();
    }
  }

In this example, two additional arrays are created inside equals(),
usages of age field require boxing, and name + age implicitly creates a
new string.

License

  • I confirm that this pull request can be released under the Apache 2 license

Bennett Lynch and others added 3 commits November 3, 2021 16:50
…ashCode()`

Reports construction of (temporary) new objects inside equals(),
hashCode(), compareTo(), and Comparator.compare() methods.

Besides constructor invocations, new objects can also be created by
autoboxing or iterator creation inside a foreach statement. This can
cause performance problems, for example, when objects are added to a Set
 or Map, where these methods will be called often.

The inspection will not report when the objects are created in a throw
or assert statement.

Example:

```
  class Person {
    private String name;
    private int age;

    public boolean equals(Object o) {
      return Arrays.equals(new Object[] {name, age}, new Object[] {((Foo)o).name, ((Foo)o).age});
    }

    public int hashCode() {
      return (name + age).hashCode();
    }
  }
```

In this example, two additional arrays are created inside equals(),
usages of age field require boxing, and name + age implicitly creates a
new string.
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 4, 2021

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

60.0% 60.0% Coverage
0.0% 0.0% Duplication

@Bennett-Lynch Bennett-Lynch merged commit f81a1cb into aws:master Nov 4, 2021
aws-sdk-java-automation added a commit that referenced this pull request Nov 30, 2023
…82a28849e

Pull request: release <- staging/96d147bb-3680-429a-b3b8-5c582a28849e
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

Successfully merging this pull request may close these issues.

2 participants