Search in sources :

Example 6 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class DescriptionBasedDiffTest method oneDiff.

@Test
public void oneDiff() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    diff.onDescribed(new Description(null, "message", SuggestedFix.replace(137, 140, "bar"), SeverityLevel.SUGGESTION));
    diff.applyDifferences(sourceFile);
    assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import org.bar.Baz;", "import com.foo.Bar;", "", "class Foo {", "  public static void main(String[] args) {", "    System.out.println(\"bar\");", "  }", "}").inOrder();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 7 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class DescriptionBasedDiffTest method applyDifferences_preservesOrder_whenRemovingNonExistentImport.

@Test
public void applyDifferences_preservesOrder_whenRemovingNonExistentImport() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    diff.onDescribed(new Description(null, "message", SuggestedFix.builder().removeImport("com.google.foo.Bar").build(), SeverityLevel.SUGGESTION));
    diff.applyDifferences(sourceFile);
    assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import org.bar.Baz;", "import com.foo.Bar;", "", "class Foo {", "  public static void main(String[] args) {", "    System.out.println(\"foo\");", "  }", "}").inOrder();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 8 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class DescriptionBasedDiffTest method applyDifferences_preservesImportOrder_whenAddingExistingImport.

@Test
public void applyDifferences_preservesImportOrder_whenAddingExistingImport() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    diff.onDescribed(new Description(null, "message", SuggestedFix.builder().addImport("com.foo.Bar").build(), SeverityLevel.SUGGESTION));
    diff.applyDifferences(sourceFile);
    assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import org.bar.Baz;", "import com.foo.Bar;", "", "class Foo {", "  public static void main(String[] args) {", "    System.out.println(\"foo\");", "  }", "}").inOrder();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 9 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class AbstractJUnit4InitMethodNotRun method matchMethod.

/**
 * Matches if all of the following conditions are true: 1) The method matches {@link
 * #methodMatcher()}, (looks like setUp() or tearDown(), and none of the overrides in the
 * hierarchy of the method have the appropriate @Before or @After annotations) 2) The method is
 * not annotated with @Test 3) The enclosing class has an @RunWith annotation and does not extend
 * TestCase. This marks that the test is intended to run with JUnit 4.
 */
@Override
public Description matchMethod(MethodTree methodTree, VisitorState state) {
    boolean matches = allOf(methodMatcher(), not(hasAnnotationOnAnyOverriddenMethod(JUNIT_TEST)), enclosingClass(isJUnit4TestClass)).matches(methodTree, state);
    if (!matches) {
        return Description.NO_MATCH;
    }
    // For each annotationReplacement, replace the first annotation that matches. If any of them
    // matches, don't try and do the rest of the work.
    Description description;
    for (AnnotationReplacements replacement : annotationReplacements()) {
        description = tryToReplaceAnnotation(methodTree, state, replacement.badAnnotation, replacement.goodAnnotation);
        if (description != null) {
            return description;
        }
    }
    // Search for another @Before annotation on the method and replace the import
    // if we find one
    String correctAnnotation = correctAnnotation();
    String unqualifiedClassName = getUnqualifiedClassName(correctAnnotation);
    for (AnnotationTree annotationNode : methodTree.getModifiers().getAnnotations()) {
        Symbol annoSymbol = ASTHelpers.getSymbol(annotationNode);
        if (annoSymbol.getSimpleName().toString().equals(unqualifiedClassName)) {
            SuggestedFix.Builder suggestedFix = SuggestedFix.builder().removeImport(annoSymbol.getQualifiedName().toString()).addImport(correctAnnotation);
            makeProtectedPublic(methodTree, state, suggestedFix);
            return describeMatch(annotationNode, suggestedFix.build());
        }
    }
    // Add correctAnnotation() to the unannotated method
    // (and convert protected to public if it is)
    SuggestedFix.Builder suggestedFix = SuggestedFix.builder().addImport(correctAnnotation);
    makeProtectedPublic(methodTree, state, suggestedFix);
    suggestedFix.prefixWith(methodTree, "@" + unqualifiedClassName + "\n");
    return describeMatch(methodTree, suggestedFix.build());
}
Also used : Description(com.google.errorprone.matchers.Description) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Symbol(com.sun.tools.javac.code.Symbol) AnnotationTree(com.sun.source.tree.AnnotationTree) Builder(com.google.errorprone.fixes.SuggestedFix.Builder)

Example 10 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class ByteBufferBackingArray method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (!BYTE_BUFFER_ARRAY_MATCHER.matches(tree, state)) {
        return Description.NO_MATCH;
    }
    // Checks for validating use on method call chain.
    ExpressionTree receiver = tree;
    do {
        receiver = ASTHelpers.getReceiver(receiver);
        if (isValidInitializerOrNotAByteBuffer(receiver, state)) {
            return Description.NO_MATCH;
        }
    } while (receiver instanceof MethodInvocationTree);
    Symbol bufferSymbol = ASTHelpers.getSymbol(receiver);
    // Checks for validating use on method scope.
    if (bufferSymbol.owner instanceof MethodSymbol) {
        MethodTree enclosingMethod = ASTHelpers.findMethod((MethodSymbol) bufferSymbol.owner, state);
        if (enclosingMethod == null || ValidByteBufferArrayScanner.scan(enclosingMethod, state, bufferSymbol)) {
            return Description.NO_MATCH;
        }
    }
    // Checks for validating use on fields.
    if (bufferSymbol.owner instanceof ClassSymbol) {
        ClassTree enclosingClass = ASTHelpers.findClass((ClassSymbol) bufferSymbol.owner, state);
        if (enclosingClass == null) {
            return Description.NO_MATCH;
        }
        Optional<? extends Tree> validMemberTree = enclosingClass.getMembers().stream().filter(memberTree -> ValidByteBufferArrayScanner.scan(memberTree, state, bufferSymbol)).findFirst();
        if (validMemberTree.isPresent()) {
            return Description.NO_MATCH;
        }
    }
    return describeMatch(tree);
}
Also used : Matchers.anyOf(com.google.errorprone.matchers.Matchers.anyOf) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) MethodMatchers.instanceMethod(com.google.errorprone.matchers.method.MethodMatchers.instanceMethod) VariableTree(com.sun.source.tree.VariableTree) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ByteBuffer(java.nio.ByteBuffer) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) BugPattern(com.google.errorprone.BugPattern) JDK(com.google.errorprone.BugPattern.Category.JDK) Matcher(com.google.errorprone.matchers.Matcher) Matchers.isSameType(com.google.errorprone.matchers.Matchers.isSameType) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) MethodInvocationTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher) Matchers.staticMethod(com.google.errorprone.matchers.Matchers.staticMethod) ExpressionTree(com.sun.source.tree.ExpressionTree) Symbol(com.sun.tools.javac.code.Symbol) TreeScanner(com.sun.source.util.TreeScanner) Description(com.google.errorprone.matchers.Description) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ASTHelpers(com.google.errorprone.util.ASTHelpers) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Symbol(com.sun.tools.javac.code.Symbol) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

Description (com.google.errorprone.matchers.Description)56 Tree (com.sun.source.tree.Tree)23 VisitorState (com.google.errorprone.VisitorState)22 BugPattern (com.google.errorprone.BugPattern)21 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)20 ASTHelpers (com.google.errorprone.util.ASTHelpers)20 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)17 ExpressionTree (com.sun.source.tree.ExpressionTree)17 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)17 JDK (com.google.errorprone.BugPattern.Category.JDK)16 Symbol (com.sun.tools.javac.code.Symbol)16 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)14 Type (com.sun.tools.javac.code.Type)14 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)11 VariableTree (com.sun.source.tree.VariableTree)11 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)11 NO_MATCH (com.google.errorprone.matchers.Description.NO_MATCH)10 ClassTree (com.sun.source.tree.ClassTree)10 Optional (java.util.Optional)10 MethodTree (com.sun.source.tree.MethodTree)9