Skip to content

Commit 8a87d57

Browse files
cushonronshapiro
authored andcommitted
Do less path string manipulation
to avoid windows-specific issues with patch application. RELNOTES: N/A ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=156451545
1 parent 487d0ef commit 8a87d57

File tree

13 files changed

+115
-64
lines changed

13 files changed

+115
-64
lines changed

check_api/src/main/java/com/google/errorprone/RefactoringCollection.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static RefactoringCollection refactor(PatchingOptions patchingOptions) {
8282
Function<URI, RefactoringResult> postProcess;
8383

8484
if (patchingOptions.inPlace()) {
85-
fileDestination = new FsFileDestination(rootPath);
85+
fileDestination = new FsFileDestination();
8686
postProcess =
8787
uri ->
8888
RefactoringResult.create(
@@ -160,7 +160,7 @@ RefactoringResult applyChanges(URI uri) throws Exception {
160160
return RefactoringResult.create("", RefactoringResultType.NO_CHANGES);
161161
}
162162

163-
doApplyProcess(fileDestination, new FsFileSource(rootPath), listeners);
163+
doApplyProcess(fileDestination, new FsFileSource(), listeners);
164164
return postProcess.apply(uri);
165165
}
166166

@@ -186,19 +186,16 @@ private void doApplyProcess(
186186
Collection<DelegatingDescriptionListener> listeners) {
187187
for (DelegatingDescriptionListener listener : listeners) {
188188
try {
189-
SourceFile file = fileSource.readFile(listener.base.getRelevantFileName());
189+
SourceFile file = fileSource.readFile(listener.base.getPath());
190190
listener.base.applyDifferences(file);
191191
fileDestination.writeFile(file);
192192
} catch (IOException e) {
193-
logger.log(
194-
Level.WARNING,
195-
"Failed to apply diff to file " + listener.base.getRelevantFileName(),
196-
e);
193+
logger.log(Level.WARNING, "Failed to apply diff to file " + listener.base.getPath(), e);
197194
}
198195
}
199196
}
200197

201-
private final class DelegatingDescriptionListener implements DescriptionListener {
198+
private static final class DelegatingDescriptionListener implements DescriptionListener {
202199
final DescriptionBasedDiff base;
203200
final DescriptionListener listener;
204201

check_api/src/main/java/com/google/errorprone/apply/DescriptionBasedDiff.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.google.errorprone.matchers.Description;
2626
import com.sun.tools.javac.tree.EndPosTable;
2727
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
28+
import java.nio.file.Path;
29+
import java.nio.file.Paths;
2830
import java.util.LinkedHashSet;
2931
import java.util.Set;
3032

@@ -38,7 +40,7 @@
3840
*/
3941
public final class DescriptionBasedDiff implements DescriptionListener, Diff {
4042

41-
private final String sourcePath;
43+
private final Path sourcePath;
4244
private final boolean ignoreOverlappingFixes;
4345
private final JCCompilationUnit compilationUnit;
4446
private final Set<String> importsToAdd;
@@ -62,7 +64,7 @@ private DescriptionBasedDiff(
6264
boolean ignoreOverlappingFixes,
6365
ImportOrganizer importOrganizer) {
6466
this.compilationUnit = checkNotNull(compilationUnit);
65-
this.sourcePath = compilationUnit.getSourceFile().toUri().getPath();
67+
this.sourcePath = Paths.get(compilationUnit.getSourceFile().toUri());
6668
this.ignoreOverlappingFixes = ignoreOverlappingFixes;
6769
this.importsToAdd = new LinkedHashSet<>();
6870
this.importsToRemove = new LinkedHashSet<>();
@@ -71,7 +73,7 @@ private DescriptionBasedDiff(
7173
}
7274

7375
@Override
74-
public String getRelevantFileName() {
76+
public Path getPath() {
7577
return sourcePath;
7678
}
7779

check_api/src/main/java/com/google/errorprone/apply/Diff.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616

1717
package com.google.errorprone.apply;
1818

19+
import java.nio.file.Path;
20+
1921
/**
2022
* All the differences to be applied to a source file to be applied in a refactoring.
2123
*
2224
* @author [email protected] (Simon Nickerson)
2325
*/
2426
public interface Diff {
25-
/** Gets the name of the file this difference applies to */
26-
public String getRelevantFileName();
27+
/** Gets the path of the file this difference applies to */
28+
public Path getPath();
2729

2830
/**
2931
* Applies this difference to the supplied {@code sourceFile}.

check_api/src/main/java/com/google/errorprone/apply/DiffApplier.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.Sets;
2121
import com.google.common.util.concurrent.AbstractService;
2222
import java.io.IOException;
23+
import java.nio.file.Path;
2324
import java.util.Set;
2425
import java.util.concurrent.ArrayBlockingQueue;
2526
import java.util.concurrent.ConcurrentSkipListSet;
@@ -40,8 +41,8 @@
4041
public class DiffApplier extends AbstractService {
4142
private static final Logger logger = Logger.getLogger(DiffApplier.class.getName());
4243
private final ExecutorService workerService;
43-
private final Set<String> refactoredPaths;
44-
private final Set<String> diffsFailedPaths;
44+
private final Set<Path> refactoredPaths;
45+
private final Set<Path> diffsFailedPaths;
4546
private final FileSource source;
4647
private final FileDestination destination;
4748
private final AtomicInteger completedFiles;
@@ -67,7 +68,7 @@ public DiffApplier(int diffParallelism, FileSource source, FileDestination desti
6768
diffParallelism,
6869
5,
6970
TimeUnit.SECONDS,
70-
new ArrayBlockingQueue<Runnable>(50),
71+
new ArrayBlockingQueue<>(50),
7172
new ThreadPoolExecutor.CallerRunsPolicy());
7273
}
7374

@@ -114,7 +115,7 @@ private final class Task implements Runnable {
114115
@Override
115116
public void run() {
116117
try {
117-
SourceFile file = source.readFile(diff.getRelevantFileName());
118+
SourceFile file = source.readFile(diff.getPath());
118119
diff.applyDifferences(file);
119120
destination.writeFile(file);
120121

@@ -123,16 +124,16 @@ public void run() {
123124
logger.log(Level.INFO, String.format("Completed %d files in %s", completed, stopwatch));
124125
}
125126
} catch (IOException | DiffNotApplicableException e) {
126-
logger.log(Level.WARNING, "Failed to apply diff to file " + diff.getRelevantFileName(), e);
127-
diffsFailedPaths.add(diff.getRelevantFileName());
127+
logger.log(Level.WARNING, "Failed to apply diff to file " + diff.getPath(), e);
128+
diffsFailedPaths.add(diff.getPath());
128129
} finally {
129130
decrementTasks();
130131
}
131132
}
132133
}
133134

134135
public Future<?> put(Diff diff) {
135-
if (refactoredPaths.add(diff.getRelevantFileName())) {
136+
if (refactoredPaths.add(diff.getPath())) {
136137
runState.incrementAndGet();
137138
return workerService.submit(new Task(diff));
138139
}

check_api/src/main/java/com/google/errorprone/apply/FileSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package com.google.errorprone.apply;
1818

1919
import java.io.IOException;
20+
import java.nio.file.Path;
2021

2122
/** @author [email protected] (Simon Nickerson) */
2223
public interface FileSource {
2324

24-
SourceFile readFile(String path) throws IOException;
25+
SourceFile readFile(Path path) throws IOException;
2526
}

check_api/src/main/java/com/google/errorprone/apply/FsFileDestination.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,13 @@
1919
import java.io.IOException;
2020
import java.nio.charset.StandardCharsets;
2121
import java.nio.file.Files;
22-
import java.nio.file.Path;
2322

2423
/** A {@link FileDestination} that writes content to a destination on the local filesystem. */
2524
public final class FsFileDestination implements FileDestination {
2625

27-
private final Path rootPath;
28-
29-
public FsFileDestination(Path rootPath) {
30-
this.rootPath = rootPath;
31-
}
32-
3326
@Override
3427
public void writeFile(SourceFile update) throws IOException {
35-
Path targetPath = rootPath.resolve(update.getPath());
36-
Files.write(targetPath, update.getSourceText().getBytes(StandardCharsets.UTF_8));
28+
Files.write(update.getPath(), update.getSourceText().getBytes(StandardCharsets.UTF_8));
3729
}
3830

3931
@Override

check_api/src/main/java/com/google/errorprone/apply/FsFileSource.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,8 @@
2424
/** A FileSource that reads source files from the local filesystem. */
2525
public final class FsFileSource implements FileSource {
2626

27-
private final Path rootPath;
28-
29-
public FsFileSource(Path rootPath) {
30-
this.rootPath = rootPath;
31-
}
32-
3327
@Override
34-
public SourceFile readFile(String path) throws IOException {
35-
return new SourceFile(
36-
path, new String(Files.readAllBytes(rootPath.resolve(path)), StandardCharsets.UTF_8));
28+
public SourceFile readFile(Path path) throws IOException {
29+
return new SourceFile(path, new String(Files.readAllBytes(path), StandardCharsets.UTF_8));
3730
}
3831
}

check_api/src/main/java/com/google/errorprone/apply/SourceFile.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.LineNumberReader;
2424
import java.io.StringReader;
2525
import java.nio.CharBuffer;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
2628
import java.util.ArrayList;
2729
import java.util.List;
2830
import javax.tools.JavaFileObject;
@@ -37,20 +39,26 @@
3739
*/
3840
public class SourceFile {
3941

40-
private final String path;
42+
private final Path path;
4143
private final StringBuilder sourceBuilder;
4244

4345
public static SourceFile create(JavaFileObject fileObject) throws IOException {
44-
return new SourceFile(fileObject.toUri().getPath(), fileObject.getCharContent(false));
46+
Path path;
47+
try {
48+
path = Paths.get(fileObject.toUri());
49+
} catch (IllegalArgumentException e) {
50+
throw new IllegalArgumentException(fileObject.toUri().toString(), e);
51+
}
52+
return new SourceFile(path, fileObject.getCharContent(false));
4553
}
4654

47-
public SourceFile(String path, CharSequence source) {
55+
public SourceFile(Path path, CharSequence source) {
4856
this.path = path;
4957
sourceBuilder = new StringBuilder(source);
5058
}
5159

5260
/** Returns the path for this source file */
53-
public String getPath() {
61+
public Path getPath() {
5462
return path;
5563
}
5664

check_api/src/main/java/com/google/errorprone/fixes/SuggestedFixes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import com.sun.tools.javac.util.Options;
8181
import com.sun.tools.javac.util.Position;
8282
import java.io.IOException;
83+
import java.nio.file.Paths;
8384
import java.util.ArrayDeque;
8485
import java.util.ArrayList;
8586
import java.util.Arrays;
@@ -642,7 +643,7 @@ public static boolean compilesWithFix(Fix fix, VisitorState state) {
642643
try {
643644
fixSource =
644645
new SourceFile(
645-
modifiedFile.getName(),
646+
Paths.get(modifiedFile.toUri()),
646647
modifiedFile.getCharContent(false /*ignoreEncodingErrors*/));
647648
} catch (IOException e) {
648649
return false;

check_api/src/test/java/com/google/errorprone/apply/SourceFileTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020

21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
2123
import java.util.Arrays;
2224
import java.util.List;
2325
import org.junit.Before;
@@ -33,7 +35,7 @@
3335
@RunWith(JUnit4.class)
3436
public class SourceFileTest {
3537

36-
private static final String DUMMY_PATH = "java/com/google/foo/bar/FooBar.java";
38+
private static final Path DUMMY_PATH = Paths.get("java/com/google/foo/bar/FooBar.java");
3739
private static final String SOURCE_TEXT =
3840
"// Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\n"
3941
+ "// eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\n"

0 commit comments

Comments
 (0)