From 3c5fec7277657389960231c8ee650256028a1d7a Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 23 Feb 2015 11:12:44 +0100 Subject: [PATCH] Add missing null check on equals --- .../src/main/java/org/sonar/server/issue/Result.java | 8 ++++---- .../java/org/sonar/server/issue/RulesAggregation.java | 10 ++++++---- .../duplications/detector/suffixtree/TextSet.java | 2 +- .../java/org/sonar/duplications/index/CloneGroup.java | 6 ++++-- .../java/org/sonar/duplications/index/ClonePart.java | 6 ++++-- .../org/sonar/duplications/statement/Statement.java | 6 ++++-- .../main/java/org/sonar/duplications/token/Token.java | 6 ++++-- .../main/java/org/sonar/api/rules/ActiveRuleParam.java | 10 ++++++---- .../debt/internal/DefaultDebtRemediationFunction.java | 8 ++++---- 9 files changed, 37 insertions(+), 25 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java b/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java index 330f60e74ab..45e32f9658a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/Result.java @@ -120,13 +120,13 @@ public class Result { } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { + if (o == null || this.getClass() != o.getClass()) { + return false; + } if (this == o) { return true; } - if (this.getClass() != o.getClass()) { - return false; - } Message message = (Message) o; if (l10nKey != null ? !l10nKey.equals(message.l10nKey) : message.l10nKey != null) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java b/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java index 8567a40d295..ec2629f6828 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/RulesAggregation.java @@ -25,6 +25,8 @@ import com.google.common.collect.Multiset; import org.sonar.api.rule.RuleKey; import org.sonar.core.rule.RuleDto; +import javax.annotation.Nullable; + import java.util.Collection; public class RulesAggregation { @@ -67,13 +69,13 @@ public class RulesAggregation { } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { + if (o == null || this.getClass() != o.getClass()) { + return false; + } if (this == o) { return true; } - if (this.getClass() != o.getClass()) { - return false; - } Rule rule = (Rule) o; return ruleKey.equals(rule.ruleKey); diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java index 51ecbe54294..e92398b26f5 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/detector/suffixtree/TextSet.java @@ -91,7 +91,7 @@ public final class TextSet extends AbstractText { @Override public boolean equals(Object obj) { - return (this.getClass() == obj.getClass()) && (((Terminator) obj).stringNumber == stringNumber); + return obj != null && this.getClass() == obj.getClass() && (((Terminator) obj).stringNumber == stringNumber); } @Override diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java index b840c2f55f0..818e018d4bb 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/CloneGroup.java @@ -23,6 +23,8 @@ import com.google.common.annotations.Beta; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import javax.annotation.Nullable; + import java.util.ArrayList; import java.util.List; @@ -134,8 +136,8 @@ public class CloneGroup { * Two groups are equal, if they have same length, same origins and contain same parts in same order. */ @Override - public boolean equals(Object object) { - if (this.getClass() != object.getClass()) { + public boolean equals(@Nullable Object object) { + if (object == null || this.getClass() != object.getClass()) { return false; } CloneGroup another = (CloneGroup) object; diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java b/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java index 630f52c1350..0130f167197 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/index/ClonePart.java @@ -21,6 +21,8 @@ package org.sonar.duplications.index; import org.sonar.duplications.CodeFragment; +import javax.annotation.Nullable; + public class ClonePart implements CodeFragment { private final String resourceId; @@ -63,8 +65,8 @@ public class ClonePart implements CodeFragment { } @Override - public boolean equals(Object obj) { - if (this.getClass() == obj.getClass()) { + public boolean equals(@Nullable Object obj) { + if (obj != null && this.getClass() == obj.getClass()) { ClonePart another = (ClonePart) obj; return another.resourceId.equals(resourceId) && another.startLine == startLine diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java b/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java index dc1c8ac402d..f698e61805b 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/statement/Statement.java @@ -22,6 +22,8 @@ package org.sonar.duplications.statement; import org.sonar.duplications.CodeFragment; import org.sonar.duplications.token.Token; +import javax.annotation.Nullable; + import java.util.List; public class Statement implements CodeFragment { @@ -81,8 +83,8 @@ public class Statement implements CodeFragment { } @Override - public boolean equals(Object obj) { - if (this.getClass() != obj.getClass()) { + public boolean equals(@Nullable Object obj) { + if (obj == null || this.getClass() != obj.getClass()) { return false; } Statement other = (Statement) obj; diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java b/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java index c1c374e7f52..e738ebc404e 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/token/Token.java @@ -19,6 +19,8 @@ */ package org.sonar.duplications.token; +import javax.annotation.Nullable; + public class Token { private final int line; @@ -49,8 +51,8 @@ public class Token { } @Override - public boolean equals(Object object) { - if (this.getClass() == object.getClass()) { + public boolean equals(@Nullable Object object) { + if (object != null && this.getClass() == object.getClass()) { Token anotherToken = (Token) object; return anotherToken.line == line && anotherToken.column == column && anotherToken.value.equals(value); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java index 7f83179429f..770e212975c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/ActiveRuleParam.java @@ -19,6 +19,8 @@ */ package org.sonar.api.rules; +import javax.annotation.Nullable; + public class ActiveRuleParam implements Cloneable { private Integer id; @@ -103,13 +105,13 @@ public class ActiveRuleParam implements Cloneable { } @Override - public boolean equals(Object obj) { + public boolean equals(@Nullable Object obj) { + if (obj == null || this.getClass() != obj.getClass()) { + return false; + } if (this == obj) { return true; } - if (this.getClass() != obj.getClass()) { - return false; - } ActiveRuleParam other = (ActiveRuleParam) obj; return other.getKey().equals(getKey()); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java index 1445f50883d..f2f1d431a64 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/debt/internal/DefaultDebtRemediationFunction.java @@ -100,13 +100,13 @@ public class DefaultDebtRemediationFunction implements DebtRemediationFunction { } @Override - public boolean equals(Object o) { + public boolean equals(@Nullable Object o) { + if (o == null || this.getClass() != o.getClass()) { + return false; + } if (this == o) { return true; } - if (this.getClass() != o.getClass()) { - return false; - } DefaultDebtRemediationFunction other = (DefaultDebtRemediationFunction) o; return new EqualsBuilder() .append(coefficient, other.coefficient()) -- 2.39.5