Skip to content

Commit

Permalink
Add a test for ArraysAsListPrimitiveArray
Browse files Browse the repository at this point in the history
and then fix the check so the test passes.

MOE_MIGRATED_REVID=127252244
  • Loading branch information
cushon committed Jul 13, 2016
1 parent 0d11c44 commit 865a6b8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 34 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import static com.google.errorprone.BugPattern.Category.JDK; import static com.google.errorprone.BugPattern.Category.JDK;
import static com.google.errorprone.BugPattern.MaturityLevel.EXPERIMENTAL; import static com.google.errorprone.BugPattern.MaturityLevel.EXPERIMENTAL;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.staticMethod; import static com.google.errorprone.matchers.Matchers.staticMethod;


import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern; import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState; import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
Expand All @@ -29,38 +33,39 @@
import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher; import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers; import com.google.errorprone.matchers.Matchers;

import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Type; import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ArrayType; import com.sun.tools.javac.code.Type.ArrayType;
import com.sun.tools.javac.tree.JCTree;

import java.util.Collections;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.Map; import java.util.Map;

import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;


@BugPattern(name = "ArraysAsListPrimitiveArray", @BugPattern(
summary = name = "ArraysAsListPrimitiveArray",
"Arrays.asList does not autobox primitive arrays, as one might expect.", summary = "Arrays.asList does not autobox primitive arrays, as one might expect.",
explanation = explanation =
"Arrays.asList does not autobox primitive arrays, as one might expect. " + "Arrays.asList does not autobox primitive arrays, as one might expect. "
"If you intended to autobox the primitive array, use an asList method " + + "If you intended to autobox the primitive array, use an asList method "
"from Guava that does autobox. If you intended to create a singleton " + + "from Guava that does autobox. If you intended to create a singleton "
"list containing the primitive array, use Collections.singletonList to " + + "list containing the primitive array, use Collections.singletonList to "
"make your intent clearer.", + "make your intent clearer.",
category = JDK, severity = ERROR, maturity = EXPERIMENTAL) category = JDK,
severity = ERROR,
maturity = EXPERIMENTAL
)
public class ArraysAsListPrimitiveArray extends BugChecker implements MethodInvocationTreeMatcher { public class ArraysAsListPrimitiveArray extends BugChecker implements MethodInvocationTreeMatcher {
private static final Matcher<MethodInvocationTree> ARRAYS_AS_LIST_SINGLE_ARRAY = Matchers.allOf(
staticMethod().onClass("java.util.Arrays").named("asList"),
Matchers.argumentCount(1),
Matchers.argument(1, Matchers.<ExpressionTree>isArrayType()));


private static final Map<TypeKind, String> GUAVA_UTILS; private static final Matcher<MethodInvocationTree> ARRAYS_AS_LIST_SINGLE_ARRAY =
allOf(
staticMethod().onClass("java.util.Arrays").named("asList"),
Matchers.argumentCount(1),
Matchers.argument(0, Matchers.<ExpressionTree>isArrayType()));


static { private static final ImmutableMap<TypeKind, String> GUAVA_UTILS = getGuavaUtils();

static ImmutableMap<TypeKind, String> getGuavaUtils() {
Map<TypeKind, String> guavaUtils = new EnumMap<>(TypeKind.class); Map<TypeKind, String> guavaUtils = new EnumMap<>(TypeKind.class);
guavaUtils.put(TypeKind.BOOLEAN, "Booleans"); guavaUtils.put(TypeKind.BOOLEAN, "Booleans");
guavaUtils.put(TypeKind.BYTE, "Bytes"); guavaUtils.put(TypeKind.BYTE, "Bytes");
Expand All @@ -70,25 +75,28 @@ public class ArraysAsListPrimitiveArray extends BugChecker implements MethodInvo
guavaUtils.put(TypeKind.CHAR, "Chars"); guavaUtils.put(TypeKind.CHAR, "Chars");
guavaUtils.put(TypeKind.FLOAT, "Floats"); guavaUtils.put(TypeKind.FLOAT, "Floats");
guavaUtils.put(TypeKind.DOUBLE, "Doubles"); guavaUtils.put(TypeKind.DOUBLE, "Doubles");
GUAVA_UTILS = Collections.unmodifiableMap(guavaUtils); return ImmutableMap.copyOf(guavaUtils);
} }


@Override @Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (ARRAYS_AS_LIST_SINGLE_ARRAY.matches(tree, state)) { if (!ARRAYS_AS_LIST_SINGLE_ARRAY.matches(tree, state)) {
JCTree array = (JCTree) tree.getArguments().get(0); return NO_MATCH;
Type componentType = ((ArrayType) array.type).getComponentType(); }
String guavaUtils = GUAVA_UTILS.get(componentType.getKind()); ExpressionTree array = Iterables.getOnlyElement(tree.getArguments());
if (guavaUtils != null) { Type componentType = ((ArrayType) ASTHelpers.getType(array)).getComponentType();
Fix fix = SuggestedFix.builder() if (!componentType.isPrimitive()) {
return NO_MATCH;
}
String guavaUtils = GUAVA_UTILS.get(componentType.getKind());
if (guavaUtils == null) {
return NO_MATCH;
}
Fix fix =
SuggestedFix.builder()
.addImport("com.google.common.primitives." + guavaUtils) .addImport("com.google.common.primitives." + guavaUtils)
.replace(tree.getMethodSelect(), guavaUtils + ".asList") .replace(tree.getMethodSelect(), guavaUtils + ".asList")
.build(); .build();
return describeMatch(tree, fix); return describeMatch(tree, fix);
}
}

return Description.NO_MATCH;
} }
} }

Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2016 Google 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.google.errorprone.bugpatterns;

import com.google.errorprone.CompilationTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** {@link ArraysAsListPrimitiveArray}Test */
@RunWith(JUnit4.class)
public class ArraysAsListPrimitiveArrayTest {

private final CompilationTestHelper compilationHelper =
CompilationTestHelper.newInstance(ArraysAsListPrimitiveArray.class, getClass());

@Test
public void positive() throws Exception {
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.Arrays;",
"class Test {",
"void f() {",
" // BUG: Diagnostic contains: Booleans.asList(new boolean[0]);",
" Arrays.asList(new boolean[0]);",
" // BUG: Diagnostic contains: Bytes.asList(new byte[0]);",
" Arrays.asList(new byte[0]);",
" // BUG: Diagnostic contains: Shorts.asList(new short[0]);",
" Arrays.asList(new short[0]);",
" // BUG: Diagnostic contains: Chars.asList(new char[0]);",
" Arrays.asList(new char[0]);",
" // BUG: Diagnostic contains: Ints.asList(new int[0]);",
" Arrays.asList(new int[0]);",
" // BUG: Diagnostic contains: Longs.asList(new long[0]);",
" Arrays.asList(new long[0]);",
" // BUG: Diagnostic contains: Doubles.asList(new double[0]);",
" Arrays.asList(new double[0]);",
" // BUG: Diagnostic contains: Floats.asList(new float[0]);",
" Arrays.asList(new float[0]);",
" }",
"}")
.doTest();
}

@Test
public void negative() throws Exception {
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.Arrays;",
"class Test {",
"void f() {",
" Arrays.asList(new Object());",
" Arrays.asList(new Object[0]);",
" Arrays.asList(new Integer[0]);",
" }",
"}")
.doTest();
}
}

0 comments on commit 865a6b8

Please sign in to comment.