Search in sources :

Example 1 with DescriptionBasedDiff

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

the class CodeTransformerTestHelper method transform.

public JavaFileObject transform(JavaFileObject original) {
    JavaCompiler compiler = JavacTool.create();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnosticsCollector, Locale.ENGLISH, UTF_8);
    JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, ImmutableList.<String>of(), null, ImmutableList.of(original));
    try {
        SourceFile sourceFile = SourceFile.create(original);
        Iterable<? extends CompilationUnitTree> trees = task.parse();
        task.analyze();
        JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(trees, JCCompilationUnit.class));
        DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, ImportOrganizer.STATIC_FIRST_ORGANIZER);
        transformer().apply(new TreePath(tree), task.getContext(), diff);
        diff.applyDifferences(sourceFile);
        return JavaFileObjects.forSourceString(Iterables.getOnlyElement(Iterables.filter(tree.getTypeDecls(), JCClassDecl.class)).sym.getQualifiedName().toString(), sourceFile.getSourceText());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavaCompiler(javax.tools.JavaCompiler) IOException(java.io.IOException) JavaFileObject(javax.tools.JavaFileObject) TreePath(com.sun.source.util.TreePath) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) SourceFile(com.google.errorprone.apply.SourceFile)

Example 2 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff 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 3 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff 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 4 with DescriptionBasedDiff

use of com.google.errorprone.apply.DescriptionBasedDiff 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 5 with DescriptionBasedDiff

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

the class SuggestedFixes method compilesWithFix.

/**
 * Returns true if the current compilation would succeed with the given fix applied. Note that
 * calling this method is very expensive as it requires rerunning the entire compile, so it should
 * be used with restraint.
 */
public static boolean compilesWithFix(Fix fix, VisitorState state) {
    if (fix.isEmpty()) {
        return true;
    }
    JCCompilationUnit compilationUnit = (JCCompilationUnit) state.getPath().getCompilationUnit();
    JavaFileObject modifiedFile = compilationUnit.getSourceFile();
    JavacTaskImpl javacTask = (JavacTaskImpl) state.context.get(JavacTask.class);
    if (javacTask == null) {
        throw new IllegalArgumentException("No JavacTask in context.");
    }
    Arguments arguments = Arguments.instance(javacTask.getContext());
    List<JavaFileObject> fileObjects = new ArrayList<>(arguments.getFileObjects());
    for (int i = 0; i < fileObjects.size(); i++) {
        final JavaFileObject oldFile = fileObjects.get(i);
        if (modifiedFile.toUri().equals(oldFile.toUri())) {
            DescriptionBasedDiff diff = DescriptionBasedDiff.create(compilationUnit, ImportOrganizer.STATIC_FIRST_ORGANIZER);
            diff.handleFix(fix);
            SourceFile fixSource;
            try {
                fixSource = new SourceFile(modifiedFile.getName(), modifiedFile.getCharContent(false));
            } catch (IOException e) {
                return false;
            }
            diff.applyDifferences(fixSource);
            fileObjects.set(i, new SimpleJavaFileObject(modifiedFile.toUri(), Kind.SOURCE) {

                @Override
                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                    return fixSource.getAsSequence();
                }
            });
            break;
        }
    }
    DiagnosticCollector<JavaFileObject> diagnosticListener = new DiagnosticCollector<>();
    Context context = new Context();
    Options.instance(context).putAll(Options.instance(javacTask.getContext()));
    context.put(Arguments.class, arguments);
    JavacTask newTask = JavacTool.create().getTask(CharStreams.nullWriter(), state.context.get(JavaFileManager.class), diagnosticListener, ImmutableList.of(), arguments.getClassNames(), fileObjects, context);
    try {
        newTask.analyze();
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return countErrors(diagnosticListener) == 0;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) Context(com.sun.tools.javac.util.Context) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JavaFileManager(javax.tools.JavaFileManager) Arguments(com.sun.tools.javac.main.Arguments) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) SourceFile(com.google.errorprone.apply.SourceFile) JavacTask(com.sun.source.util.JavacTask)

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