use of com.sun.source.tree.CompilationUnitTree in project error-prone by google.
the class ClassName method matchCompilationUnit.
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
if (tree.getTypeDecls().isEmpty() || tree.getPackageName() == null) {
return Description.NO_MATCH;
}
String filename = Files.getNameWithoutExtension(tree.getSourceFile().getName());
List<String> names = new ArrayList<>();
for (Tree member : tree.getTypeDecls()) {
if (member instanceof ClassTree) {
ClassTree classMember = (ClassTree) member;
if (isSuppressed(classMember)) {
// and @SuppressWarnings can't be applied to packages.
return Description.NO_MATCH;
}
if (classMember.getSimpleName().contentEquals(filename)) {
return Description.NO_MATCH;
}
if (classMember.getModifiers().getFlags().contains(Modifier.PUBLIC)) {
// the error.
return Description.NO_MATCH;
}
names.add(classMember.getSimpleName().toString());
}
}
String message = String.format("Expected a class declaration named %s inside %s.java, instead found: %s", filename, filename, Joiner.on(", ").join(names));
return buildDescription(tree.getPackageName()).setMessage(message).build();
}
use of com.sun.source.tree.CompilationUnitTree 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(System.err, 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.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class T6598108 method main.
public static void main(String[] args) throws Exception {
//NOI18N
final String bootPath = System.getProperty("sun.boot.class.path");
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
assert tool != null;
final JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath), null, Arrays.asList(new MyFileObject()));
CompilationUnitTree cut = ct.parse().iterator().next();
TreePath tp = new TreePath(new TreePath(cut), cut.getTypeDecls().get(0));
Scope s = Trees.instance(ct).getScope(tp);
TypeElement type = ct.getElements().getTypeElement("com.sun.java.util.jar.pack.Package.File");
if (Trees.instance(ct).isAccessible(s, type)) {
//"false" would be expected here.
throw new IllegalStateException("");
}
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class T6557752 method main.
public static void main(String[] args) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
Iterable<? extends CompilationUnitTree> asts = task.parse();
task.analyze();
trees = Trees.instance(task);
MyVisitor myVisitor = new MyVisitor();
for (CompilationUnitTree ast : asts) {
myVisitor.compilationUnit = ast;
myVisitor.scan(ast, null);
}
if (!myVisitor.foundError) {
throw new AssertionError("Expected error not found!");
}
}
use of com.sun.source.tree.CompilationUnitTree in project ceylon-compiler by ceylon.
the class GenStubs method run.
boolean run(String sourcepath, File outdir, List<String> classes) {
//System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
if (sourcepath == null)
throw new IllegalArgumentException("sourcepath not set");
if (outdir == null)
throw new IllegalArgumentException("source output dir not set");
JavacTool tool = JavacTool.create();
StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
try {
fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
List<JavaFileObject> files = new ArrayList<JavaFileObject>();
for (String c : classes) {
JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
if (fo == null)
error("class not found: " + c);
else
files.add(fo);
}
JavacTask t = tool.getTask(null, fm, null, null, null, files);
Iterable<? extends CompilationUnitTree> trees = t.parse();
for (CompilationUnitTree tree : trees) {
makeStub(fm, tree);
}
} catch (IOException e) {
error("IO error " + e, e);
}
return (errors == 0);
}
Aggregations