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

70 files changed

Lines changed: 73 additions & 73 deletions

File tree

Some content is hidden

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

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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)