Search in sources :

Example 1 with FileDestination

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

the class RefactoringCollection method refactor.

static RefactoringCollection refactor(PatchingOptions patchingOptions) {
    Path rootPath = buildRootPath();
    FileDestination fileDestination;
    Function<URI, RefactoringResult> postProcess;
    if (patchingOptions.inPlace()) {
        fileDestination = new FsFileDestination(rootPath);
        postProcess = uri -> RefactoringResult.create(String.format("Refactoring changes were successfully applied to %s," + " please check the refactored code and recompile.", uri), RefactoringResultType.CHANGED);
    } else {
        Path baseDir = rootPath.resolve(patchingOptions.baseDirectory());
        Path patchFilePath = baseDir.resolve("error-prone.patch");
        PatchFileDestination patchFileDestination = new PatchFileDestination(baseDir, rootPath);
        postProcess = new Function<URI, RefactoringResult>() {

            private final AtomicBoolean first = new AtomicBoolean(true);

            @Override
            public RefactoringResult apply(URI uri) {
                try {
                    writePatchFile(first, uri, patchFileDestination, patchFilePath);
                    return RefactoringResult.create("Changes were written to " + patchFilePath + ". Please inspect the file and apply with: " + "patch -p0 -u -i error-prone.patch", RefactoringResultType.CHANGED);
                } catch (IOException e) {
                    throw new RuntimeException("Failed to emit patch file!", e);
                }
            }
        };
        fileDestination = patchFileDestination;
    }
    ImportOrganizer importOrganizer = patchingOptions.importOrganizer();
    return new RefactoringCollection(rootPath, fileDestination, postProcess, importOrganizer);
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) URI(java.net.URI) ImportOrganizer(com.google.errorprone.apply.ImportOrganizer) FsFileDestination(com.google.errorprone.apply.FsFileDestination) FileDestination(com.google.errorprone.apply.FileDestination) PatchFileDestination(com.google.errorprone.apply.PatchFileDestination) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FsFileDestination(com.google.errorprone.apply.FsFileDestination) PatchFileDestination(com.google.errorprone.apply.PatchFileDestination)

Aggregations

FileDestination (com.google.errorprone.apply.FileDestination)1 FsFileDestination (com.google.errorprone.apply.FsFileDestination)1 ImportOrganizer (com.google.errorprone.apply.ImportOrganizer)1 PatchFileDestination (com.google.errorprone.apply.PatchFileDestination)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1