-
Notifications
You must be signed in to change notification settings - Fork 134
Added errorprone checks for Guavas static factory methods. #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bulldozer-bot
merged 14 commits into
palantir:develop
from
aioobe:guava-static-factory-methods
Oct 14, 2019
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ecc4ebe
Added errorprone checks for Guavas static factory methods.
aioobe 5bdded8
Changed from SeverityLevel.WARNING to SeverityLevel.SUGGESTION.
aioobe dc575f0
Added changelog entry.
aioobe 627d860
Organized imports.
aioobe 5b23e75
Applied spotless.
aioobe f1e8730
Check parameter types using withParameters instead.
aioobe 5bbb366
Reduced cyclomatic complexity by introducing a map.
aioobe 1426502
Always return a result after first match.
aioobe 41101f2
Moved most common matchers (according to a big repo I happened to hav…
aioobe 3899dac
Update baseline-error-prone/src/main/java/com/palantir/baseline/error…
aioobe 0d78c96
Added REQUIRES_HUMAN_ATTENTION in annotation.
aioobe 73c534e
Added PreferCollectionConstructors to BaselineErrorProneExtension.DEF…
aioobe 06f5f5c
Dropped rerwrite rules for *WithExpectedSize methods.
aioobe fa06726
Update BaselineErrorProneExtension.java
carterkozak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
345 changes: 345 additions & 0 deletions
345
...or-prone/src/main/java/com/palantir/baseline/errorprone/PreferCollectionConstructors.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,345 @@ | ||
| /* | ||
| * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * 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 com.palantir.baseline.errorprone; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import com.google.errorprone.BugPattern; | ||
| import com.google.errorprone.VisitorState; | ||
| import com.google.errorprone.bugpatterns.BugChecker; | ||
| import com.google.errorprone.fixes.SuggestedFix; | ||
| import com.google.errorprone.fixes.SuggestedFixes; | ||
| import com.google.errorprone.matchers.Description; | ||
| import com.google.errorprone.matchers.Matcher; | ||
| import com.google.errorprone.matchers.method.MethodMatchers; | ||
| import com.sun.source.tree.ExpressionTree; | ||
| import com.sun.source.tree.MethodInvocationTree; | ||
| import com.sun.tools.javac.code.Types; | ||
| import com.sun.tools.javac.tree.JCTree.JCExpression; | ||
| import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; | ||
| import com.sun.tools.javac.util.List; | ||
| import java.util.ArrayList; | ||
| import java.util.EnumMap; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.LinkedList; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.TreeMap; | ||
| import java.util.TreeSet; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
| import java.util.concurrent.CopyOnWriteArraySet; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @AutoService(BugChecker.class) | ||
| @BugPattern( | ||
| name = "PreferCollectionConstructors", | ||
| link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
| linkType = BugPattern.LinkType.CUSTOM, | ||
aioobe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| severity = BugPattern.SeverityLevel.SUGGESTION, | ||
| summary = "Since Java 7 the standard collection constructors should be used instead of the static factory " | ||
| + "methods provided by Guava.") | ||
| public final class PreferCollectionConstructors extends BugChecker implements BugChecker.MethodInvocationTreeMatcher { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_ARRAY_LIST = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Lists") | ||
| .named("newArrayList") | ||
aioobe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_ARRAY_LIST_WITH_ITERABLE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Lists") | ||
| .named("newArrayList") | ||
| .withParameters("java.lang.Iterable"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_ARRAY_LIST_WITH_CAPACITY = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Lists") | ||
| .namedAnyOf("newArrayListWithCapacity", "newArrayListWithExpectedSize") | ||
aioobe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .withParameters("int"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_LIST = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Lists") | ||
| .named("newLinkedList") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_LIST_WITH_ITERABLE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Lists") | ||
| .named("newLinkedList") | ||
| .withParameters("java.lang.Iterable"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_COPY_ON_WRITE_ARRAY_LIST = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Lists") | ||
| .named("newCopyOnWriteArrayList") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_COPY_ON_WRITE_ARRAY_LIST_WITH_ITERABLE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Lists") | ||
| .named("newCopyOnWriteArrayList") | ||
| .withParameters("java.lang.Iterable"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_CONCURRENT_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newConcurrentMap") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_HASH_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newHashMap") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_HASH_MAP_WITH_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newHashMap") | ||
| .withParameters("java.util.Map"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_HASH_MAP_WITH_EXPECTED_SIZE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newHashMapWithExpectedSize") | ||
| .withParameters("int"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_TREE_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newTreeMap") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_TREE_MAP_WITH_COMPARATOR = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newTreeMap") | ||
| .withParameters("java.util.Comparator"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_TREE_MAP_WITH_SORTED_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newTreeMap") | ||
| .withParameters("java.util.SortedMap"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_COPY_ON_WRITE_ARRAY_SET = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newCopyOnWriteArraySet") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_COPY_ON_WRITE_ARRAY_SET_WITH_ITERABLE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newCopyOnWriteArraySet") | ||
| .withParameters("java.lang.Iterable"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_HASH_SET = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newLinkedHashSet") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_HASH_SET_WITH_ITERABLE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newLinkedHashSet") | ||
| .withParameters("java.lang.Iterable"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_HASH_SET_WITH_EXPECTED_SIZE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newLinkedHashSetWithExpectedSize") | ||
| .withParameters("int"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_TREE_SET = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newTreeSet") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_TREE_SET_WITH_COMPARATOR = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newTreeSet") | ||
| .withParameters("java.util.Comparator"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_TREE_SET_WITH_ITERABLE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newTreeSet") | ||
| .withParameters("java.lang.Iterable"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_HASH_SET = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newHashSet") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_HASH_SET_WITH_ITERABLE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newHashSet") | ||
| .withParameters("java.lang.Iterable"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_HASH_SET_WITH_EXPECTED_SIZE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Sets") | ||
| .named("newHashSetWithExpectedSize") | ||
| .withParameters("int"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_HASH_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newLinkedHashMap") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_HASH_MAP_WITH_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newLinkedHashMap") | ||
| .withParameters("java.util.Map"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_LINKED_HASH_MAP_WITH_EXPECTED_SIZE = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newLinkedHashMapWithExpectedSize") | ||
| .withParameters("int"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_ENUM_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newEnumMap") | ||
| .withParameters(); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_ENUM_MAP_WITH_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newEnumMap") | ||
| .withParameters("java.util.Map"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_ENUM_MAP_WITH_CLASS = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newEnumMap") | ||
| .withParameters("java.lang.Class"); | ||
|
|
||
| private static final Matcher<ExpressionTree> NEW_IDENTITY_HASH_MAP = | ||
| MethodMatchers.staticMethod() | ||
| .onClass("com.google.common.collect.Maps") | ||
| .named("newIdentityHashMap"); | ||
|
|
||
| private static final Map<Matcher<ExpressionTree>, Class<?>> classMap = | ||
| ImmutableMap.<Matcher<ExpressionTree>, Class<?>>builder() | ||
| .put(NEW_ARRAY_LIST, ArrayList.class) | ||
| .put(NEW_ARRAY_LIST_WITH_ITERABLE, ArrayList.class) | ||
| .put(NEW_ARRAY_LIST_WITH_CAPACITY, ArrayList.class) | ||
| .put(NEW_LINKED_LIST, LinkedList.class) | ||
| .put(NEW_LINKED_LIST_WITH_ITERABLE, LinkedList.class) | ||
| .put(NEW_COPY_ON_WRITE_ARRAY_LIST, CopyOnWriteArrayList.class) | ||
| .put(NEW_COPY_ON_WRITE_ARRAY_LIST_WITH_ITERABLE, CopyOnWriteArrayList.class) | ||
| .put(NEW_CONCURRENT_MAP, ConcurrentHashMap.class) | ||
| .put(NEW_HASH_MAP, HashMap.class) | ||
| .put(NEW_HASH_MAP_WITH_MAP, HashMap.class) | ||
| .put(NEW_HASH_MAP_WITH_EXPECTED_SIZE, HashMap.class) | ||
| .put(NEW_COPY_ON_WRITE_ARRAY_SET, CopyOnWriteArraySet.class) | ||
| .put(NEW_COPY_ON_WRITE_ARRAY_SET_WITH_ITERABLE, CopyOnWriteArraySet.class) | ||
| .put(NEW_LINKED_HASH_SET, LinkedHashSet.class) | ||
| .put(NEW_LINKED_HASH_SET_WITH_ITERABLE, LinkedHashSet.class) | ||
| .put(NEW_LINKED_HASH_SET_WITH_EXPECTED_SIZE, LinkedHashSet.class) | ||
| .put(NEW_TREE_SET, TreeSet.class) | ||
| .put(NEW_TREE_SET_WITH_COMPARATOR, TreeSet.class) | ||
| .put(NEW_TREE_SET_WITH_ITERABLE, TreeSet.class) | ||
| .put(NEW_HASH_SET, HashSet.class) | ||
| .put(NEW_HASH_SET_WITH_ITERABLE, HashSet.class) | ||
| .put(NEW_HASH_SET_WITH_EXPECTED_SIZE, HashSet.class) | ||
| .put(NEW_TREE_MAP, TreeMap.class) | ||
| .put(NEW_TREE_MAP_WITH_SORTED_MAP, TreeMap.class) | ||
| .put(NEW_TREE_MAP_WITH_COMPARATOR, TreeMap.class) | ||
| .put(NEW_LINKED_HASH_MAP, LinkedHashMap.class) | ||
| .put(NEW_LINKED_HASH_MAP_WITH_MAP, LinkedHashMap.class) | ||
| .put(NEW_LINKED_HASH_MAP_WITH_EXPECTED_SIZE, LinkedHashMap.class) | ||
| .put(NEW_ENUM_MAP, EnumMap.class) | ||
| .put(NEW_ENUM_MAP_WITH_CLASS, EnumMap.class) | ||
| .put(NEW_ENUM_MAP_WITH_MAP, EnumMap.class) | ||
| .put(NEW_IDENTITY_HASH_MAP, IdentityHashMap.class) | ||
| .build(); | ||
|
|
||
| private static final Set<Matcher<ExpressionTree>> requiresCollectionArg = ImmutableSet.of( | ||
| NEW_ARRAY_LIST_WITH_ITERABLE, | ||
| NEW_LINKED_LIST_WITH_ITERABLE, | ||
| NEW_COPY_ON_WRITE_ARRAY_LIST_WITH_ITERABLE, | ||
| NEW_COPY_ON_WRITE_ARRAY_SET_WITH_ITERABLE, | ||
| NEW_LINKED_HASH_SET_WITH_ITERABLE, | ||
| NEW_TREE_SET_WITH_ITERABLE, | ||
| NEW_HASH_SET_WITH_ITERABLE); | ||
|
|
||
| @Override | ||
| public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
|
|
||
| Class<?> collectionClass = findCollectionClassToUse(state, tree); | ||
aioobe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (collectionClass == null) { | ||
| return Description.NO_MATCH; | ||
| } | ||
|
|
||
| SuggestedFix.Builder fixBuilder = SuggestedFix.builder(); | ||
| String collectionType = SuggestedFixes.qualifyType(state, fixBuilder, collectionClass.getName()); | ||
| String typeArgs = tree.getTypeArguments() | ||
| .stream() | ||
| .map(state::getSourceForNode) | ||
| .collect(Collectors.joining(", ")); | ||
| String arg = tree.getArguments().isEmpty() ? "" : state.getSourceForNode(tree.getArguments().get(0)); | ||
| String replacement = "new " + collectionType + "<" + typeArgs + ">(" + arg + ")"; | ||
| return buildDescription(tree) | ||
| .setMessage("The factory method call should be replaced with a constructor call. " | ||
| + "See https://git.io/JeCT6 for more information.") | ||
aioobe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .addFix(fixBuilder.replace(tree, replacement).build()) | ||
| .build(); | ||
| } | ||
|
|
||
| private Class<?> findCollectionClassToUse(VisitorState state, ExpressionTree tree) { | ||
| boolean firstArgIsCollection = isFirstArgCollection(state, tree); | ||
| for (Map.Entry<Matcher<ExpressionTree>, Class<?>> entry : classMap.entrySet()) { | ||
| Matcher<ExpressionTree> matcher = entry.getKey(); | ||
| if (matcher.matches(tree, state) && (!requiresCollectionArg.contains(matcher) || firstArgIsCollection)) { | ||
aioobe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return entry.getValue(); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private boolean isFirstArgCollection(VisitorState state, ExpressionTree tree) { | ||
| List<JCExpression> args = ((JCMethodInvocation) tree).args; | ||
| if (args.isEmpty()) { | ||
| return false; | ||
| } | ||
| Types types = state.getTypes(); | ||
| return types.isSubtype( | ||
| types.erasure(args.get(0).type), | ||
| types.erasure(state.getTypeFromString("java.util.Collection"))); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.