mirror of https://github.com/hyperledger/besu
Equals cleanup (#1434)
* Don't copy collections if we don't need to. Change types higher up if needed. * Don't use Guava's Object.equal, use Java's Objects.equals. ** add errorprone test to enforce the banning of Guava's Objects class. Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>pull/2/head
parent
9bc8d8531b
commit
16982ebad2
@ -0,0 +1,55 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2019 ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
*/ |
||||||
|
package tech.pegasys.errorpronechecks; |
||||||
|
|
||||||
|
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
||||||
|
import static com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; |
||||||
|
import static com.google.errorprone.matchers.Description.NO_MATCH; |
||||||
|
import static com.google.errorprone.matchers.Matchers.allOf; |
||||||
|
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; |
||||||
|
|
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import com.google.auto.service.AutoService; |
||||||
|
import com.google.common.collect.ImmutableMap; |
||||||
|
import com.google.errorprone.BugPattern; |
||||||
|
import com.google.errorprone.VisitorState; |
||||||
|
import com.google.errorprone.bugpatterns.BugChecker; |
||||||
|
import com.google.errorprone.matchers.Description; |
||||||
|
import com.google.errorprone.matchers.Matcher; |
||||||
|
import com.sun.source.tree.ExpressionTree; |
||||||
|
import com.sun.source.tree.MethodInvocationTree; |
||||||
|
|
||||||
|
@AutoService(BugChecker.class) |
||||||
|
@BugPattern( |
||||||
|
name = "BannedMethod", |
||||||
|
summary = "Some methods should not be used, make sure that doesn't happen.", |
||||||
|
severity = WARNING) |
||||||
|
public class BannedMethod extends BugChecker implements MethodInvocationTreeMatcher { |
||||||
|
|
||||||
|
private static final ImmutableMap<Matcher<ExpressionTree>, String> BANNED_METHOD_LIST = |
||||||
|
ImmutableMap.of( |
||||||
|
allOf(staticMethod().onClass("com.google.common.base.Objects").withAnyName()), |
||||||
|
"Do not use com.google.common.base.Objects methods, use java.util.Objects methods instead."); |
||||||
|
|
||||||
|
@Override |
||||||
|
public Description matchMethodInvocation( |
||||||
|
final MethodInvocationTree tree, final VisitorState state) { |
||||||
|
for (final Map.Entry<Matcher<ExpressionTree>, String> entry : BANNED_METHOD_LIST.entrySet()) { |
||||||
|
if (entry.getKey().matches(tree, state)) { |
||||||
|
return buildDescriptionFromChecker(tree, this).setMessage(entry.getValue()).build(); |
||||||
|
} |
||||||
|
} |
||||||
|
return NO_MATCH; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,37 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
*/ |
||||||
|
package tech.pegasys.errorpronechecks; |
||||||
|
|
||||||
|
import com.google.errorprone.CompilationTestHelper; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
public class BannedMethodTest { |
||||||
|
|
||||||
|
private CompilationTestHelper compilationHelper; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setup() { |
||||||
|
compilationHelper = CompilationTestHelper.newInstance(BannedMethod.class, getClass()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void doNotReturnNullPositiveCases() { |
||||||
|
compilationHelper.addSourceFile("BannedMethodPositiveCases.java").doTest(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void doNotReturnNullNegativeCases() { |
||||||
|
compilationHelper.addSourceFile("BannedMethodNegativeCases.java").doTest(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
*/ |
||||||
|
package tech.pegasys.errorpronechecks; |
||||||
|
|
||||||
|
import java.util.Objects; |
||||||
|
|
||||||
|
public class BannedMethodNegativeCases { |
||||||
|
|
||||||
|
public void callsObjectsEquals() throws Exception { |
||||||
|
Objects.equals("1", "1"); |
||||||
|
} |
||||||
|
|
||||||
|
public void callsObjectsHashCode() throws Exception { |
||||||
|
Objects.hash("1", "1"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
*/ |
||||||
|
package tech.pegasys.errorpronechecks; |
||||||
|
|
||||||
|
import com.google.common.base.Objects; |
||||||
|
|
||||||
|
public class BannedMethodPositiveCases { |
||||||
|
|
||||||
|
public void callsObjectsEquals() throws Exception { |
||||||
|
// BUG: Diagnostic contains: Do not use com.google.common.base.Objects methods, use
|
||||||
|
// java.util.Objects methods instead.
|
||||||
|
Objects.equal("1", "1"); |
||||||
|
} |
||||||
|
|
||||||
|
public void callsObjectsHashCode() throws Exception { |
||||||
|
// BUG: Diagnostic contains: Do not use com.google.common.base.Objects methods, use
|
||||||
|
// java.util.Objects methods instead.
|
||||||
|
Objects.hashCode("1", "1"); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue