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();
}
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();
}
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();
}
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());
}
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);
}
Aggregations