Search in sources :

Example 1 with SourceFile

use of com.google.errorprone.apply.SourceFile 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 SourceFile

use of com.google.errorprone.apply.SourceFile 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)

Example 3 with SourceFile

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

the class RefactoringCollection method doApplyProcess.

private void doApplyProcess(FileDestination fileDestination, FileSource fileSource, Collection<DelegatingDescriptionListener> listeners) {
    for (DelegatingDescriptionListener listener : listeners) {
        try {
            SourceFile file = fileSource.readFile(listener.base.getRelevantFileName());
            listener.base.applyDifferences(file);
            fileDestination.writeFile(file);
        } catch (IOException e) {
            logger.log(Level.WARNING, "Failed to apply diff to file " + listener.base.getRelevantFileName(), e);
        }
    }
}
Also used : SourceFile(com.google.errorprone.apply.SourceFile) IOException(java.io.IOException)

Example 4 with SourceFile

use of com.google.errorprone.apply.SourceFile 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)

Aggregations

SourceFile (com.google.errorprone.apply.SourceFile)4 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)3 IOException (java.io.IOException)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 DiagnosticCollector (javax.tools.DiagnosticCollector)2 ImportOrganizer (com.google.errorprone.apply.ImportOrganizer)1 Description (com.google.errorprone.matchers.Description)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