use of com.sun.source.tree.CompilationUnitTree in project error-prone by google.
the class SimpleDateFormatConstant method threadLocalFix.
private static Fix threadLocalFix(VariableTree tree, VisitorState state, final VarSymbol sym) {
SuggestedFix.Builder fix = SuggestedFix.builder().replace(tree.getType(), String.format("ThreadLocal<%s>", state.getSourceForNode(tree.getType()))).prefixWith(tree.getInitializer(), "ThreadLocal.withInitial(() -> ").postfixWith(tree.getInitializer(), ")");
CompilationUnitTree unit = state.getPath().getCompilationUnit();
unit.accept(new TreeScanner<Void, Void>() {
@Override
public Void visitIdentifier(IdentifierTree tree, Void unused) {
if (Objects.equals(ASTHelpers.getSymbol(tree), sym)) {
fix.postfixWith(tree, ".get()");
}
return null;
}
}, null);
return fix.build();
}
use of com.sun.source.tree.CompilationUnitTree in project error-prone by google.
the class CompilerBasedTest method compile.
protected void compile(TreeScanner scanner, JavaFileObject fileObject) {
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(fileObject));
try {
this.sourceFile = SourceFile.create(fileObject);
Iterable<? extends CompilationUnitTree> trees = task.parse();
task.analyze();
for (CompilationUnitTree tree : trees) {
scanner.scan((JCCompilationUnit) tree);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
this.context = task.getContext();
}
use of com.sun.source.tree.CompilationUnitTree in project j2objc by google.
the class JavacParser method parseFiles.
@Override
public void parseFiles(Collection<String> paths, Handler handler, SourceVersion sourceVersion) {
List<File> files = new ArrayList<>();
for (String path : paths) {
files.add(new File(path));
}
try {
JavacEnvironment env = createEnvironment(files, null, false);
List<CompilationUnitTree> units = new ArrayList<>();
for (CompilationUnitTree unit : env.task().parse()) {
units.add(unit);
}
env.task().analyze();
processDiagnostics(env.diagnostics());
if (ErrorUtil.errorCount() == 0) {
for (CompilationUnitTree ast : units) {
com.google.devtools.j2objc.ast.CompilationUnit unit = TreeConverter.convertCompilationUnit(options, env, (JCTree.JCCompilationUnit) ast);
processDiagnostics(env.diagnostics());
handler.handleParsedUnit(unit.getSourceFilePath(), unit);
}
}
} catch (IOException e) {
ErrorUtil.fatalError(e, "javac file manager error");
}
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class Test method test.
void test(File test) throws Exception {
JavacTool tool1 = JavacTool.create();
StandardJavaFileManager fm = tool1.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(test);
// parse test file into a tree, and write it out to a stringbuffer using Pretty
JavacTask t1 = tool1.getTask(null, fm, null, null, null, files);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Iterable<? extends CompilationUnitTree> trees = t1.parse();
for (CompilationUnitTree tree : trees) {
new Pretty(pw, true).printExpr((JCTree) tree);
}
pw.close();
final String out = sw.toString();
System.err.println("generated code:\n" + out + "\n");
// verify the generated code is valid Java by compiling it
JavacTool tool2 = JavacTool.create();
JavaFileObject fo = new SimpleJavaFileObject(URI.create("output"), JavaFileObject.Kind.SOURCE) {
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return out;
}
};
JavacTask t2 = tool2.getTask(null, fm, null, null, null, Collections.singleton(fo));
boolean ok = t2.call();
if (!ok)
throw new Exception("compilation of generated code failed");
File expectedClass = new File(test.getName().replace(".java", ".class"));
if (!expectedClass.exists())
throw new Exception(expectedClass + " not found");
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class T6963934 method main.
public static void main(String[] args) throws Exception {
File testSrc = new File(System.getProperty("test.src"));
File thisSrc = new File(testSrc, T6963934.class.getSimpleName() + ".java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
JavacTask task = (JavacTask) compiler.getTask(null, fileManager, null, null, null, fileManager.getJavaFileObjects(thisSrc));
CompilationUnitTree tree = task.parse().iterator().next();
int count = 0;
for (ImportTree importTree : tree.getImports()) {
System.out.println(importTree);
count++;
}
int expected = 7;
if (count != expected)
throw new Exception("unexpected number of imports found: " + count + ", expected: " + expected);
}
Aggregations