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);
}
}
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();
}
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();
}
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();
}
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;
}
Aggregations