Search in sources :

Example 6 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.

the class DescriptionBasedDiffTest method addImport.

@Test
public void addImport() {
    DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit);
    diff.onDescribed(new Description(null, "message", SuggestedFix.builder().addImport("com.google.foo.Bar").build(), SeverityLevel.SUGGESTION));
    diff.applyDifferences(sourceFile);
    assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import com.foo.Bar;", "import com.google.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 7 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.

the class BugCheckerRefactoringTestHelper method applyDiff.

private JavaFileObject applyDiff(JavaFileObject sourceFileObject, Context context, JCCompilationUnit tree) throws IOException {
    ImportOrganizer importOrganizer = ImportOrderParser.getImportOrganizer(importOrder);
    final DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, importOrganizer);
    transformer(refactoringBugChecker).apply(new TreePath(tree), context, new DescriptionListener() {

        @Override
        public void onDescribed(Description description) {
            if (!description.fixes.isEmpty()) {
                diff.handleFix(fixChooser.choose(description.fixes));
            }
        }
    });
    SourceFile sourceFile = SourceFile.create(sourceFileObject);
    diff.applyDifferences(sourceFile);
    JavaFileObject transformed = JavaFileObjects.forSourceString(getFullyQualifiedName(tree), sourceFile.getSourceText());
    return transformed;
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) JavaFileObject(javax.tools.JavaFileObject) Description(com.google.errorprone.matchers.Description) TreePath(com.sun.source.util.TreePath) SourceFile(com.google.errorprone.apply.SourceFile) ImportOrganizer(com.google.errorprone.apply.ImportOrganizer)

Example 8 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.

the class DescriptionBasedDiffTest method overlappingDiffs_throws.

@Test
public void overlappingDiffs_throws() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    assertThrows(IllegalArgumentException.class, () -> diff.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(137, 140, "baz").replace(137, 140, "bar").build(), SeverityLevel.SUGGESTION)));
    DescriptionBasedDiff diff2 = createDescriptionBasedDiff();
    diff2.onDescribed(new Description(null, "bah", SuggestedFix.builder().replace(137, 140, "baz").build(), SeverityLevel.SUGGESTION));
    assertThrows(IllegalArgumentException.class, () -> diff2.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(137, 140, "bar").build(), SeverityLevel.SUGGESTION)));
    DescriptionBasedDiff diff3 = DescriptionBasedDiff.createIgnoringOverlaps(compilationUnit, ImportOrganizer.STATIC_FIRST_ORGANIZER);
    diff3.onDescribed(new Description(null, "bah", SuggestedFix.builder().replace(137, 140, "baz").build(), SeverityLevel.SUGGESTION));
    // No throw, since it's lenient. Refactors to the first "baz" replacement and ignores this.
    diff3.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(137, 140, "bar").build(), SeverityLevel.SUGGESTION));
    diff3.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(\"baz\");", "  }", "}").inOrder();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 9 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.

the class DescriptionBasedDiffTest method twoDiffs.

@Test
public void twoDiffs() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    diff.onDescribed(new Description(null, "message", SuggestedFix.builder().replace(124, 127, "longer").replace(137, 140, "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.longer.println(\"bar\");", "  }", "}").inOrder();
}
Also used : DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) Description(com.google.errorprone.matchers.Description) Test(org.junit.Test)

Example 10 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff in project error-prone by google.

the class DescriptionBasedDiffTest method applyDifferences_addsImportAndSorts_whenAddingNewImport.

@Test
public void applyDifferences_addsImportAndSorts_whenAddingNewImport() {
    DescriptionBasedDiff diff = createDescriptionBasedDiff();
    diff.onDescribed(new Description(null, "message", SuggestedFix.builder().addImport("com.google.foo.Bar").build(), SeverityLevel.SUGGESTION));
    diff.applyDifferences(sourceFile);
    assertThat(sourceFile.getLines()).containsExactly("package foo.bar;", "import com.foo.Bar;", "import com.google.foo.Bar;", "import org.bar.Baz;", "", "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)

Aggregations

DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)14 Description (com.google.errorprone.matchers.Description)11 Test (org.junit.Test)11 SourceFile (com.google.errorprone.apply.SourceFile)3 JavaFileObject (javax.tools.JavaFileObject)3 TreePath (com.sun.source.util.TreePath)2 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)2 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)2 IOException (java.io.IOException)2 DiagnosticCollector (javax.tools.DiagnosticCollector)2 ImportOrganizer (com.google.errorprone.apply.ImportOrganizer)1 JavacTask (com.sun.source.util.JavacTask)1 Arguments (com.sun.tools.javac.main.Arguments)1 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)1 Context (com.sun.tools.javac.util.Context)1 ArrayList (java.util.ArrayList)1 JavaCompiler (javax.tools.JavaCompiler)1 JavaFileManager (javax.tools.JavaFileManager)1 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)1 StandardJavaFileManager (javax.tools.StandardJavaFileManager)1