use of com.sun.tools.javac.api.JavacTaskImpl 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.sun.tools.javac.api.JavacTaskImpl in project error-prone by google.
the class GuardedByBinderTest method bind.
private String bind(String className, String exprString, JavaFileObject fileObject) {
JavaCompiler javaCompiler = JavacTool.create();
JavacTaskImpl task = (JavacTaskImpl) javaCompiler.getTask(new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true), fileManager, null, Collections.<String>emptyList(), null, Arrays.asList(fileObject));
Iterable<? extends CompilationUnitTree> compilationUnits = task.parse();
task.analyze();
for (CompilationUnitTree compilationUnit : compilationUnits) {
FindClass finder = new FindClass();
finder.visitTopLevel((JCTree.JCCompilationUnit) compilationUnit);
for (JCTree.JCClassDecl classDecl : finder.decls) {
if (classDecl.getSimpleName().contentEquals(className)) {
Optional<GuardedByExpression> guardExpression = GuardedByBinder.bindString(exprString, GuardedBySymbolResolver.from(ASTHelpers.getSymbol(classDecl), compilationUnit, task.getContext(), null));
if (!guardExpression.isPresent()) {
throw new IllegalGuardedBy(exprString);
}
return guardExpression.get().debugPrint();
}
}
}
throw new AssertionError("Couldn't find a class with the given name: " + className);
}
use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon by eclipse.
the class T6330997 method main.
public static void main(String... args) {
increaseMajor("T1.class", 1);
increaseMajor("T2.class", 2);
javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
JavaCompiler compiler = JavaCompiler.instance(task.getContext());
try {
compiler.resolveIdent("T1").complete();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed: unexpected exception while reading class T1");
}
try {
compiler.resolveIdent("T2").complete();
} catch (BadClassFile e) {
System.err.println("Passed: expected completion failure " + e.getClass().getName());
return;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed: unexpected exception while reading class T2");
}
throw new RuntimeException("Failed: no error reported");
}
use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon by eclipse.
the class TestResolveIdent method main.
public static void main(String[] args) throws IOException {
javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
JavaCompiler compiler = JavaCompiler.instance(task.getContext());
System.out.println(compiler.resolveIdent(getDeprecatedClass().getCanonicalName()));
}
use of com.sun.tools.javac.api.JavacTaskImpl in project ceylon by eclipse.
the class T6435291 method main.
public static void main(String... args) {
javax.tools.JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavacTaskImpl task = (JavacTaskImpl) tool.getTask(null, null, null, null, null, null);
JavaCompiler compiler = JavaCompiler.instance(task.getContext());
try {
compiler.resolveIdent("T").complete();
} catch (BadClassFile e) {
System.err.println("Passed: expected completion failure " + e.getClass().getName());
return;
} catch (Exception e) {
throw new RuntimeException("Failed: unexpected exception");
}
throw new RuntimeException("Failed: no error reported");
}
Aggregations