Skip to content

Commit 815567c

Browse files
authored
fix: use && in equals check to avoid possible NPE (#1927)
* fix: use && in equals check to avoid possible NPE * chore: update goldens
1 parent f86eca2 commit 815567c

File tree

70 files changed

+73
-73
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+73
-73
lines changed

gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/resourcename/ResourceNameHelperClassComposer.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1349,8 +1349,8 @@ private static MethodDefinition createEqualsMethod(
13491349
RelationalOperationExpr.equalToWithExprs(
13501350
getClassMethodInvocationExpr,
13511351
getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
1352-
LogicalOperationExpr orLogicalExpr =
1353-
LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
1352+
LogicalOperationExpr andLogicalExpr =
1353+
LogicalOperationExpr.logicalAndWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
13541354

13551355
// Create second if statement's body assignment expression.
13561356
Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
@@ -1399,11 +1399,11 @@ private static MethodDefinition createEqualsMethod(
13991399
.setConditionExpr(oEqualsThisExpr)
14001400
.setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr)))
14011401
.build();
1402-
// Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
1402+
// Code: if (o != null && getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
14031403
// return ..}
14041404
IfStatement secondIfStatement =
14051405
IfStatement.builder()
1406-
.setConditionExpr(orLogicalExpr)
1406+
.setConditionExpr(andLogicalExpr)
14071407
.setBody(
14081408
Arrays.asList(
14091409
ExprStatement.withExpr(thatAssignmentExpr),

gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/resourcename/goldens/AgentName.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public class AgentName implements ResourceName {
168168
if (o == this) {
169169
return true;
170170
}
171-
if (o != null || getClass() == o.getClass()) {
171+
if (o != null && getClass() == o.getClass()) {
172172
AgentName that = ((AgentName) o);
173173
return Objects.equals(this.project, that.project)
174174
&& Objects.equals(this.location, that.location);

gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/resourcename/goldens/BillingAccountLocationName.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public class BillingAccountLocationName implements ResourceName {
124124
if (o == this) {
125125
return true;
126126
}
127-
if (o != null || getClass() == o.getClass()) {
127+
if (o != null && getClass() == o.getClass()) {
128128
BillingAccountLocationName that = ((BillingAccountLocationName) o);
129129
return Objects.equals(this.billingAccount, that.billingAccount)
130130
&& Objects.equals(this.location, that.location);

gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/resourcename/goldens/CollisionResourceName.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public class ResourceName implements com.google.api.resourcenames.ResourceName {
181181
if (o == this) {
182182
return true;
183183
}
184-
if (o != null || getClass() == o.getClass()) {
184+
if (o != null && getClass() == o.getClass()) {
185185
ResourceName that = ((ResourceName) o);
186186
return Objects.equals(this.project, that.project)
187187
&& Objects.equals(this.location, that.location)

gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/resourcename/goldens/FoobarName.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public class FoobarName implements ResourceName {
262262
if (o == this) {
263263
return true;
264264
}
265-
if (o != null || getClass() == o.getClass()) {
265+
if (o != null && getClass() == o.getClass()) {
266266
FoobarName that = ((FoobarName) o);
267267
return Objects.equals(this.project, that.project)
268268
&& Objects.equals(this.foobar, that.foobar)

gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/resourcename/goldens/SessionName.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class SessionName implements ResourceName {
111111
if (o == this) {
112112
return true;
113113
}
114-
if (o != null || getClass() == o.getClass()) {
114+
if (o != null && getClass() == o.getClass()) {
115115
SessionName that = ((SessionName) o);
116116
return Objects.equals(this.session, that.session);
117117
}

gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/resourcename/goldens/TestName.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public class TestName implements ResourceName {
137137
if (o == this) {
138138
return true;
139139
}
140-
if (o != null || getClass() == o.getClass()) {
140+
if (o != null && getClass() == o.getClass()) {
141141
TestName that = ((TestName) o);
142142
return Objects.equals(this.session, that.session)
143143
&& Objects.equals(this.shardId, that.shardId)

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/BlurbName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public boolean equals(Object o) {
290290
if (o == this) {
291291
return true;
292292
}
293-
if (o != null || getClass() == o.getClass()) {
293+
if (o != null && getClass() == o.getClass()) {
294294
BlurbName that = ((BlurbName) o);
295295
return Objects.equals(this.user, that.user)
296296
&& Objects.equals(this.legacyUser, that.legacyUser)

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/ProfileName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
ProfileName that = ((ProfileName) o);
132132
return Objects.equals(this.user, that.user);
133133
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/RoomName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public boolean equals(Object o) {
125125
if (o == this) {
126126
return true;
127127
}
128-
if (o != null || getClass() == o.getClass()) {
128+
if (o != null && getClass() == o.getClass()) {
129129
RoomName that = ((RoomName) o);
130130
return Objects.equals(this.room, that.room);
131131
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/SequenceName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
SequenceName that = ((SequenceName) o);
132132
return Objects.equals(this.sequence, that.sequence);
133133
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/SequenceReportName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
SequenceReportName that = ((SequenceReportName) o);
132132
return Objects.equals(this.sequence, that.sequence);
133133
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/SessionName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
SessionName that = ((SessionName) o);
132132
return Objects.equals(this.session, that.session);
133133
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/StreamingSequenceName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
StreamingSequenceName that = ((StreamingSequenceName) o);
132132
return Objects.equals(this.streamingSequence, that.streamingSequence);
133133
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/StreamingSequenceReportName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public boolean equals(Object o) {
129129
if (o == this) {
130130
return true;
131131
}
132-
if (o != null || getClass() == o.getClass()) {
132+
if (o != null && getClass() == o.getClass()) {
133133
StreamingSequenceReportName that = ((StreamingSequenceReportName) o);
134134
return Objects.equals(this.streamingSequence, that.streamingSequence);
135135
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/TestName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public boolean equals(Object o) {
137137
if (o == this) {
138138
return true;
139139
}
140-
if (o != null || getClass() == o.getClass()) {
140+
if (o != null && getClass() == o.getClass()) {
141141
TestName that = ((TestName) o);
142142
return Objects.equals(this.session, that.session) && Objects.equals(this.test, that.test);
143143
}

showcase/gapic-showcase/proto/src/main/java/com/google/showcase/v1beta1/UserName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public boolean equals(Object o) {
125125
if (o == this) {
126126
return true;
127127
}
128-
if (o != null || getClass() == o.getClass()) {
128+
if (o != null && getClass() == o.getClass()) {
129129
UserName that = ((UserName) o);
130130
return Objects.equals(this.user, that.user);
131131
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/BlurbName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public boolean equals(Object o) {
290290
if (o == this) {
291291
return true;
292292
}
293-
if (o != null || getClass() == o.getClass()) {
293+
if (o != null && getClass() == o.getClass()) {
294294
BlurbName that = ((BlurbName) o);
295295
return Objects.equals(this.user, that.user)
296296
&& Objects.equals(this.legacyUser, that.legacyUser)

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/ProfileName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
ProfileName that = ((ProfileName) o);
132132
return Objects.equals(this.user, that.user);
133133
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/RoomName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public boolean equals(Object o) {
125125
if (o == this) {
126126
return true;
127127
}
128-
if (o != null || getClass() == o.getClass()) {
128+
if (o != null && getClass() == o.getClass()) {
129129
RoomName that = ((RoomName) o);
130130
return Objects.equals(this.room, that.room);
131131
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/SequenceName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
SequenceName that = ((SequenceName) o);
132132
return Objects.equals(this.sequence, that.sequence);
133133
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/SequenceReportName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
SequenceReportName that = ((SequenceReportName) o);
132132
return Objects.equals(this.sequence, that.sequence);
133133
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/SessionName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
SessionName that = ((SessionName) o);
132132
return Objects.equals(this.session, that.session);
133133
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/StreamingSequenceName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
StreamingSequenceName that = ((StreamingSequenceName) o);
132132
return Objects.equals(this.streamingSequence, that.streamingSequence);
133133
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/StreamingSequenceReportName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public boolean equals(Object o) {
129129
if (o == this) {
130130
return true;
131131
}
132-
if (o != null || getClass() == o.getClass()) {
132+
if (o != null && getClass() == o.getClass()) {
133133
StreamingSequenceReportName that = ((StreamingSequenceReportName) o);
134134
return Objects.equals(this.streamingSequence, that.streamingSequence);
135135
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/TestName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public boolean equals(Object o) {
137137
if (o == this) {
138138
return true;
139139
}
140-
if (o != null || getClass() == o.getClass()) {
140+
if (o != null && getClass() == o.getClass()) {
141141
TestName that = ((TestName) o);
142142
return Objects.equals(this.session, that.session) && Objects.equals(this.test, that.test);
143143
}

showcase/proto-gapic-showcase-v1beta1/src/main/java/com/google/showcase/v1beta1/UserName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public boolean equals(Object o) {
125125
if (o == this) {
126126
return true;
127127
}
128-
if (o != null || getClass() == o.getClass()) {
128+
if (o != null && getClass() == o.getClass()) {
129129
UserName that = ((UserName) o);
130130
return Objects.equals(this.user, that.user);
131131
}

test/integration/goldens/apigeeconnect/src/com/google/cloud/apigeeconnect/v1/EndpointName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public boolean equals(Object o) {
137137
if (o == this) {
138138
return true;
139139
}
140-
if (o != null || getClass() == o.getClass()) {
140+
if (o != null && getClass() == o.getClass()) {
141141
EndpointName that = ((EndpointName) o);
142142
return Objects.equals(this.project, that.project)
143143
&& Objects.equals(this.endpoint, that.endpoint);

test/integration/goldens/asset/src/com/google/cloud/asset/v1/FeedName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public boolean equals(Object o) {
240240
if (o == this) {
241241
return true;
242242
}
243-
if (o != null || getClass() == o.getClass()) {
243+
if (o != null && getClass() == o.getClass()) {
244244
FeedName that = ((FeedName) o);
245245
return Objects.equals(this.project, that.project)
246246
&& Objects.equals(this.feed, that.feed)

test/integration/goldens/asset/src/com/google/cloud/asset/v1/FolderName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
FolderName that = ((FolderName) o);
132132
return Objects.equals(this.folder, that.folder);
133133
}

test/integration/goldens/asset/src/com/google/cloud/asset/v1/OrganizationName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
OrganizationName that = ((OrganizationName) o);
132132
return Objects.equals(this.organization, that.organization);
133133
}

test/integration/goldens/asset/src/com/google/cloud/asset/v1/ProjectName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public boolean equals(Object o) {
127127
if (o == this) {
128128
return true;
129129
}
130-
if (o != null || getClass() == o.getClass()) {
130+
if (o != null && getClass() == o.getClass()) {
131131
ProjectName that = ((ProjectName) o);
132132
return Objects.equals(this.project, that.project);
133133
}

test/integration/goldens/asset/src/com/google/cloud/asset/v1/SavedQueryName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public boolean equals(Object o) {
250250
if (o == this) {
251251
return true;
252252
}
253-
if (o != null || getClass() == o.getClass()) {
253+
if (o != null && getClass() == o.getClass()) {
254254
SavedQueryName that = ((SavedQueryName) o);
255255
return Objects.equals(this.project, that.project)
256256
&& Objects.equals(this.savedQuery, that.savedQuery)

test/integration/goldens/bigtable/src/com/google/bigtable/v2/InstanceName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public boolean equals(Object o) {
137137
if (o == this) {
138138
return true;
139139
}
140-
if (o != null || getClass() == o.getClass()) {
140+
if (o != null && getClass() == o.getClass()) {
141141
InstanceName that = ((InstanceName) o);
142142
return Objects.equals(this.project, that.project)
143143
&& Objects.equals(this.instance, that.instance);

test/integration/goldens/bigtable/src/com/google/bigtable/v2/TableName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public boolean equals(Object o) {
154154
if (o == this) {
155155
return true;
156156
}
157-
if (o != null || getClass() == o.getClass()) {
157+
if (o != null && getClass() == o.getClass()) {
158158
TableName that = ((TableName) o);
159159
return Objects.equals(this.project, that.project)
160160
&& Objects.equals(this.instance, that.instance)

test/integration/goldens/credentials/src/com/google/cloud/iam/credentials/v1/ServiceAccountName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public boolean equals(Object o) {
138138
if (o == this) {
139139
return true;
140140
}
141-
if (o != null || getClass() == o.getClass()) {
141+
if (o != null && getClass() == o.getClass()) {
142142
ServiceAccountName that = ((ServiceAccountName) o);
143143
return Objects.equals(this.project, that.project)
144144
&& Objects.equals(this.serviceAccount, that.serviceAccount);

test/integration/goldens/kms/src/com/google/cloud/kms/v1/CryptoKeyName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public boolean equals(Object o) {
175175
if (o == this) {
176176
return true;
177177
}
178-
if (o != null || getClass() == o.getClass()) {
178+
if (o != null && getClass() == o.getClass()) {
179179
CryptoKeyName that = ((CryptoKeyName) o);
180180
return Objects.equals(this.project, that.project)
181181
&& Objects.equals(this.location, that.location)

test/integration/goldens/kms/src/com/google/cloud/kms/v1/CryptoKeyVersionName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public boolean equals(Object o) {
198198
if (o == this) {
199199
return true;
200200
}
201-
if (o != null || getClass() == o.getClass()) {
201+
if (o != null && getClass() == o.getClass()) {
202202
CryptoKeyVersionName that = ((CryptoKeyVersionName) o);
203203
return Objects.equals(this.project, that.project)
204204
&& Objects.equals(this.location, that.location)

test/integration/goldens/kms/src/com/google/cloud/kms/v1/ImportJobName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public boolean equals(Object o) {
175175
if (o == this) {
176176
return true;
177177
}
178-
if (o != null || getClass() == o.getClass()) {
178+
if (o != null && getClass() == o.getClass()) {
179179
ImportJobName that = ((ImportJobName) o);
180180
return Objects.equals(this.project, that.project)
181181
&& Objects.equals(this.location, that.location)

test/integration/goldens/kms/src/com/google/cloud/kms/v1/KeyRingName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public boolean equals(Object o) {
154154
if (o == this) {
155155
return true;
156156
}
157-
if (o != null || getClass() == o.getClass()) {
157+
if (o != null && getClass() == o.getClass()) {
158158
KeyRingName that = ((KeyRingName) o);
159159
return Objects.equals(this.project, that.project)
160160
&& Objects.equals(this.location, that.location)

test/integration/goldens/kms/src/com/google/cloud/kms/v1/LocationName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public boolean equals(Object o) {
137137
if (o == this) {
138138
return true;
139139
}
140-
if (o != null || getClass() == o.getClass()) {
140+
if (o != null && getClass() == o.getClass()) {
141141
LocationName that = ((LocationName) o);
142142
return Objects.equals(this.project, that.project)
143143
&& Objects.equals(this.location, that.location);

test/integration/goldens/library/src/com/google/example/library/v1/BookName.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public boolean equals(Object o) {
137137
if (o == this) {
138138
return true;
139139
}
140-
if (o != null || getClass() == o.getClass()) {
140+
if (o != null && getClass() == o.getClass()) {
141141
BookName that = ((BookName) o);
142142
return Objects.equals(this.shelf, that.shelf) && Objects.equals(this.book, that.book);
143143
}

0 commit comments

Comments
 (0)