Skip to content

Commit f81a1cb

Browse files
Enable & fix inspection: Object instantiation inside equals() or hashCode() (#2822)
* Enable & fix inspection: Object instantiation inside `equals()` or `hashCode()` 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.
1 parent 0fce198 commit f81a1cb

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

.idea/inspectionProfiles/AWS_Java_SDK_2_0.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/sdk-core/src/main/java/software/amazon/awssdk/core/document/internal/BooleanDocument.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.List;
1919
import java.util.Map;
20-
import java.util.Objects;
2120
import software.amazon.awssdk.annotations.Immutable;
2221
import software.amazon.awssdk.annotations.SdkInternalApi;
2322
import software.amazon.awssdk.core.SdkNumber;
@@ -138,11 +137,11 @@ public boolean equals(Object o) {
138137
return false;
139138
}
140139
BooleanDocument that = (BooleanDocument) o;
141-
return Objects.equals(value, that.value);
140+
return value == that.value;
142141
}
143142

144143
@Override
145144
public int hashCode() {
146-
return Objects.hashCode(value);
145+
return Boolean.hashCode(value);
147146
}
148147
}

http-client-spi/src/main/java/software/amazon/awssdk/http/DefaultSdkHttpFullResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public boolean equals(Object o) {
9191
return false;
9292
}
9393
DefaultSdkHttpFullResponse that = (DefaultSdkHttpFullResponse) o;
94-
return Objects.equals(statusCode, that.statusCode) &&
94+
return (statusCode == that.statusCode) &&
9595
Objects.equals(statusText, that.statusText) &&
9696
Objects.equals(headers, that.headers);
9797
}

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/endpoints/S3EndpointResolverContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public boolean equals(Object o) {
8686
Objects.equals(originalRequest, that.originalRequest) &&
8787
Objects.equals(region, that.region) &&
8888
Objects.equals(serviceConfiguration, that.serviceConfiguration) &&
89-
Objects.equals(disableHostPrefixInjection, that.disableHostPrefixInjection);
89+
(disableHostPrefixInjection == that.disableHostPrefixInjection);
9090
}
9191

9292
@Override

0 commit comments

Comments
 (0)