Search in sources :

Example 46 with Context

use of com.sun.tools.javac.util.Context in project error-prone by google.

the class BugCheckerRefactoringTestHelper method doCompile.

private JCCompilationUnit doCompile(final JavaFileObject input, Iterable<JavaFileObject> files, Context context) throws IOException {
    JavacTool tool = JavacTool.create();
    DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
    context.put(ErrorProneOptions.class, ErrorProneOptions.empty());
    JavacTaskImpl task = (JavacTaskImpl) tool.getTask(CharStreams.nullWriter(), fileManager, diagnosticsCollector, options, /*classes=*/
    null, files, context);
    Iterable<? extends CompilationUnitTree> trees = task.parse();
    task.analyze();
    JCCompilationUnit tree = Iterables.getOnlyElement(Iterables.filter(Iterables.filter(trees, JCCompilationUnit.class), compilationUnit -> compilationUnit.getSourceFile() == input));
    Iterable<Diagnostic<? extends JavaFileObject>> errorDiagnostics = Iterables.filter(diagnosticsCollector.getDiagnostics(), d -> d.getKind() == Diagnostic.Kind.ERROR);
    if (!Iterables.isEmpty(errorDiagnostics)) {
        fail("compilation failed unexpectedly: " + errorDiagnostics);
    }
    return tree;
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) Iterables(com.google.common.collect.Iterables) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) HashMap(java.util.HashMap) DescriptionBasedDiff(com.google.errorprone.apply.DescriptionBasedDiff) ErrorProneScannerTransformer(com.google.errorprone.scanner.ErrorProneScannerTransformer) ErrorProneScanner(com.google.errorprone.scanner.ErrorProneScanner) ImmutableList(com.google.common.collect.ImmutableList) CharStreams(com.google.common.io.CharStreams) JavacTool(com.sun.tools.javac.api.JavacTool) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) Fix(com.google.errorprone.fixes.Fix) DiagnosticCollector(javax.tools.DiagnosticCollector) Truth.assertAbout(com.google.common.truth.Truth.assertAbout) JavaFileObjects(com.google.testing.compile.JavaFileObjects) TreePath(com.sun.source.util.TreePath) BugChecker(com.google.errorprone.bugpatterns.BugChecker) IOException(java.io.IOException) Truth.assertThat(com.google.common.truth.Truth.assertThat) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JavaSourceSubjectFactory.javaSource(com.google.testing.compile.JavaSourceSubjectFactory.javaSource) JavaFileObject(javax.tools.JavaFileObject) SourceFile(com.google.errorprone.apply.SourceFile) List(java.util.List) Description(com.google.errorprone.matchers.Description) Context(com.sun.tools.javac.util.Context) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JavaFileObject(javax.tools.JavaFileObject) JavacTaskImpl(com.sun.tools.javac.api.JavacTaskImpl) JavacTool(com.sun.tools.javac.api.JavacTool) Diagnostic(javax.tools.Diagnostic) DiagnosticCollector(javax.tools.DiagnosticCollector)

Example 47 with Context

use of com.sun.tools.javac.util.Context in project error-prone by google.

the class AbstractUTreeTest method createContext.

@Before
public void createContext() {
    context = new Context();
    JavacFileManager.preRegister(context);
    unifier = new Unifier(context);
    inliner = unifier.createInliner();
}
Also used : Context(com.sun.tools.javac.util.Context) Before(org.junit.Before)

Example 48 with Context

use of com.sun.tools.javac.util.Context in project lombok by rzwitserloot.

the class JavacHandlerUtil method copyTypeParams.

public static List<JCTypeParameter> copyTypeParams(JavacNode source, List<JCTypeParameter> params) {
    if (params == null || params.isEmpty())
        return params;
    ListBuffer<JCTypeParameter> out = new ListBuffer<JCTypeParameter>();
    JavacTreeMaker maker = source.getTreeMaker();
    Context context = source.getContext();
    for (JCTypeParameter tp : params) {
        List<JCExpression> bounds = tp.bounds;
        if (bounds != null && !bounds.isEmpty()) {
            ListBuffer<JCExpression> boundsCopy = new ListBuffer<JCExpression>();
            for (JCExpression expr : tp.bounds) {
                boundsCopy.append(cloneType(maker, expr, source.get(), context));
            }
            bounds = boundsCopy.toList();
        }
        out.append(maker.TypeParameter(tp.name, bounds));
    }
    return out.toList();
}
Also used : JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Context(com.sun.tools.javac.util.Context) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer)

Example 49 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class T6889255 method test.

void test(String testName, boolean expectNames, String... opts) throws Exception {
    System.err.println("Test " + testName + ": expectNames:" + expectNames + " javacOpts:" + Arrays.asList(opts));
    File outDir = new File(testName);
    outDir.mkdirs();
    compile(outDir, opts);
    Context ctx = new Context();
    JavacFileManager fm = new JavacFileManager(ctx, true, null);
    fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(outDir));
    ClassReader cr = ClassReader.instance(ctx);
    cr.saveParameterNames = true;
    Names names = Names.instance(ctx);
    Set<String> classes = getTopLevelClasses(outDir);
    Deque<String> work = new LinkedList<String>(classes);
    String classname;
    while ((classname = work.poll()) != null) {
        System.err.println("Checking class " + classname);
        ClassSymbol sym = cr.enterClass(names.table.fromString(classname));
        sym.complete();
        if ((sym.flags() & Flags.INTERFACE) != 0 && !testInterfaces)
            continue;
        for (Scope.Entry e = sym.members_field.elems; e != null; e = e.sibling) {
            System.err.println("Checking member " + e.sym);
            switch(e.sym.kind) {
                case Kinds.TYP:
                    {
                        String name = e.sym.flatName().toString();
                        if (!classes.contains(name)) {
                            classes.add(name);
                            work.add(name);
                        }
                        break;
                    }
                case Kinds.MTH:
                    verify((MethodSymbol) e.sym, expectNames);
                    break;
            }
        }
    }
}
Also used : Context(com.sun.tools.javac.util.Context) JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Names(com.sun.tools.javac.util.Names) Scope(com.sun.tools.javac.code.Scope) ClassReader(com.sun.tools.javac.jvm.ClassReader)

Example 50 with Context

use of com.sun.tools.javac.util.Context in project ceylon-compiler by ceylon.

the class GetDeps method run.

void run(PrintWriter out, String... args) throws IOException, ClassFileNotFoundException {
    decodeArgs(args);
    final StandardJavaFileManager fm = new JavacFileManager(new Context(), false, null);
    if (classpath != null)
        fm.setLocation(StandardLocation.CLASS_PATH, classpath);
    ClassFileReader reader = new ClassFileReader(fm);
    Dependencies d = new Dependencies();
    if (regex != null)
        d.setFilter(Dependencies.getRegexFilter(Pattern.compile(regex)));
    if (packageNames.size() > 0)
        d.setFilter(Dependencies.getPackageFilter(packageNames, false));
    SortedRecorder r = new SortedRecorder(reverse);
    d.findAllDependencies(reader, rootClassNames, transitiveClosure, r);
    SortedMap<Location, SortedSet<Dependency>> deps = r.getMap();
    for (Map.Entry<Location, SortedSet<Dependency>> e : deps.entrySet()) {
        out.println(e.getKey());
        for (Dependency dep : e.getValue()) {
            out.println("    " + dep.getTarget());
        }
    }
}
Also used : JavacFileManager(com.sun.tools.javac.file.JavacFileManager) Context(com.sun.tools.javac.util.Context) Dependencies(com.sun.tools.classfile.Dependencies) Location(com.sun.tools.classfile.Dependency.Location)

Aggregations

Context (com.sun.tools.javac.util.Context)65 JavacFileManager (com.sun.tools.javac.file.JavacFileManager)19 Test (org.junit.Test)9 Options (com.sun.tools.javac.util.Options)8 IOException (java.io.IOException)8 PrintWriter (java.io.PrintWriter)8 JavaFileObject (javax.tools.JavaFileObject)7 ListBuffer (com.sun.tools.javac.util.ListBuffer)6 JavacTask (com.sun.source.util.JavacTask)5 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)5 JCCompilationUnit (com.sun.tools.javac.tree.JCTree.JCCompilationUnit)5 JavaFileManager (javax.tools.JavaFileManager)5 OutputStreamWriter (java.io.OutputStreamWriter)4 Path (java.nio.file.Path)4 DiagnosticListener (javax.tools.DiagnosticListener)4 ImmutableList (com.google.common.collect.ImmutableList)3 JavacTaskImpl (com.sun.tools.javac.api.JavacTaskImpl)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)3 StringWriter (java.io.StringWriter)3