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