com.sun.tools.javac.tree.JCTree.JCModuleDecl Java Examples

The following examples show how to use com.sun.tools.javac.tree.JCTree.JCModuleDecl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavacTaskImpl.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) {
    for (Env<AttrContext> env: queue) {
        switch (env.tree.getTag()) {
            case CLASSDEF:
                JCClassDecl cdef = (JCClassDecl) env.tree;
                if (cdef.sym != null)
                    elems.append(cdef.sym);
                break;
            case MODULEDEF:
                JCModuleDecl mod = (JCModuleDecl) env.tree;
                if (mod.sym != null)
                    elems.append(mod.sym);
                break;
            case PACKAGEDEF:
                JCCompilationUnit unit = env.toplevel;
                if (unit.packge != null)
                    elems.append(unit.packge);
                break;
        }
    }
    genList.addAll(queue);
}
 
Example #2
Source File: ElementsTable.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private String getModuleName(Location location) throws ToolException {
    try {
        JavaFileObject jfo = fm.getJavaFileForInput(location,
                "module-info", JavaFileObject.Kind.SOURCE);
        if (jfo != null) {
            JCCompilationUnit jcu = compiler.parse(jfo);
            JCModuleDecl module = TreeInfo.getModule(jcu);
            if (module != null) {
                return module.getName().toString();
            }
        }
    } catch (IOException ioe) {
        String text = messager.getText("main.file.manager.list", location);
        throw new ToolException(SYSERR, text, ioe);
    }
    return null;
}
 
Example #3
Source File: Modules.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override @SuppressWarnings("unchecked")
public void visitModuleDef(JCModuleDecl tree) {
    msym.directives = List.nil();
    msym.provides = List.nil();
    msym.uses = List.nil();
    tree.directives.forEach(t -> t.accept(this));
    msym.directives = msym.directives.reverse();
    msym.provides = msym.provides.reverse();
    msym.uses = msym.uses.reverse();

    if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED))
        msym.directives = msym.directives.prepend(msym.requires.head);

    msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));

    checkForCorrectness();
}
 
Example #4
Source File: Modules.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public Completer getUsesProvidesCompleter() {
    return sym -> {
        ModuleSymbol msym = (ModuleSymbol) sym;

        msym.complete();

        Env<AttrContext> env = typeEnvs.get(msym);
        UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env);
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
        JCModuleDecl decl = env.toplevel.getModuleDecl();
        DiagnosticPosition prevLintPos = deferredLintHandler.setPos(decl.pos());

        try {
            decl.accept(v);
        } finally {
            log.useSource(prev);
            deferredLintHandler.setPos(prevLintPos);
        }
    };
}
 
Example #5
Source File: Modules.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visitModuleDef(JCModuleDecl tree) {
    sym = Assert.checkNonNull(tree.sym);

    if (tree.getModuleType() == ModuleKind.OPEN) {
        sym.flags.add(ModuleFlags.OPEN);
    }
    sym.flags_field |= (tree.mods.flags & Flags.DEPRECATED);

    sym.requires = List.nil();
    sym.exports = List.nil();
    sym.opens = List.nil();
    tree.directives.forEach(t -> t.accept(this));
    sym.requires = sym.requires.reverse();
    sym.exports = sym.exports.reverse();
    sym.opens = sym.opens.reverse();
    ensureJavaBase();
}
 
Example #6
Source File: JavacTaskImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) {
    for (Env<AttrContext> env: queue) {
        switch (env.tree.getTag()) {
            case CLASSDEF:
                JCClassDecl cdef = (JCClassDecl) env.tree;
                if (cdef.sym != null)
                    elems.append(cdef.sym);
                break;
            case MODULEDEF:
                JCModuleDecl mod = (JCModuleDecl) env.tree;
                if (mod.sym != null)
                    elems.append(mod.sym);
                break;
            case PACKAGEDEF:
                JCCompilationUnit unit = env.toplevel;
                if (unit.packge != null)
                    elems.append(unit.packge);
                break;
        }
    }
    genList.addAll(queue);
}
 
Example #7
Source File: Modules.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override @SuppressWarnings("unchecked")
public void visitModuleDef(JCModuleDecl tree) {
    msym.directives = List.nil();
    msym.provides = List.nil();
    msym.uses = List.nil();
    StreamSupport.stream(tree.directives).forEach(t -> t.accept(this));
    msym.directives = msym.directives.reverse();
    msym.provides = msym.provides.reverse();
    msym.uses = msym.uses.reverse();

    if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED))
        msym.directives = msym.directives.prepend(msym.requires.head);

    msym.directives = msym.directives.appendList(List.from(Maps.getOrDefault(addReads,msym, Collections.emptySet())));

    checkForCorrectness();
}
 
Example #8
Source File: Modules.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Completer getUsesProvidesCompleter() {
    return sym -> {
        ModuleSymbol msym = (ModuleSymbol) sym;

        msym.complete();

        Env<AttrContext> env = typeEnvs.get(msym);
        UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env);
        JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
        JCModuleDecl decl = env.toplevel.getModuleDecl();
        DiagnosticPosition prevLintPos = deferredLintHandler.setPos(decl.pos());

        try {
            decl.accept(v);
        } finally {
            log.useSource(prev);
            deferredLintHandler.setPos(prevLintPos);
        }
    };
}
 
Example #9
Source File: Modules.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void visitModuleDef(JCModuleDecl tree) {
    sym = Assert.checkNonNull(tree.sym);

    if (tree.getModuleType() == ModuleKind.OPEN) {
        sym.flags.add(ModuleFlags.OPEN);
    }
    sym.flags_field |= (tree.mods.flags & Flags.DEPRECATED);

    sym.requires = List.nil();
    sym.exports = List.nil();
    sym.opens = List.nil();
    StreamSupport.stream(tree.directives).forEach(t -> t.accept(this));
    sym.requires = sym.requires.reverse();
    sym.exports = sym.exports.reverse();
    sym.opens = sym.opens.reverse();
    ensureJavaBase();
}
 
Example #10
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void checkModuleName (JCModuleDecl tree) {
    Name moduleName = tree.sym.name;
    Assert.checkNonNull(moduleName);
    if (lint.isEnabled(LintCategory.MODULE)) {
        JCExpression qualId = tree.qualId;
        while (qualId != null) {
            Name componentName;
            DiagnosticPosition pos;
            switch (qualId.getTag()) {
                case SELECT:
                    JCFieldAccess selectNode = ((JCFieldAccess) qualId);
                    componentName = selectNode.name;
                    pos = selectNode.pos();
                    qualId = selectNode.selected;
                    break;
                case IDENT:
                    componentName = ((JCIdent) qualId).name;
                    pos = qualId.pos();
                    qualId = null;
                    break;
                default:
                    throw new AssertionError("Unexpected qualified identifier: " + qualId.toString());
            }
            if (componentName != null) {
                String moduleNameComponentString = componentName.toString();
                int nameLength = moduleNameComponentString.length();
                if (nameLength > 0 && Character.isDigit(moduleNameComponentString.charAt(nameLength - 1))) {
                    log.warning(Lint.LintCategory.MODULE, pos, Warnings.PoorChoiceForModuleName(componentName));
                }
            }
        }
    }
}
 
Example #11
Source File: SourceTreeScannerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** record all tree nodes found by reflection. */
public void reflectiveScan(Object o) {
    if (o == null)
        return;
    if (o instanceof JCTree) {
        JCTree tree = (JCTree) o;
        //System.err.println("EXPECT: " + tree.getKind() + " " + trim(tree, 64));
        expect.add(tree);
        for (Field f: getFields(tree)) {
            if (TypeBoundKind.class.isAssignableFrom(f.getType())) {
                // not part of public API
                continue;
            }
            try {
                //System.err.println("FIELD: " + f.getName());
                if (tree instanceof JCModuleDecl && f.getName().equals("mods")) {
                    // The modifiers will not found by TreeScanner,
                    // but the embedded annotations will be.
                    reflectiveScan(((JCModuleDecl) tree).mods.annotations);
                } else {
                    reflectiveScan(f.get(tree));
                }
            } catch (IllegalAccessException e) {
                error(e.toString());
            }
        }
    } else if (o instanceof List) {
        List<?> list = (List<?>) o;
        for (Object item: list)
            reflectiveScan(item);
    } else
        error("unexpected item: " + o);
}
 
Example #12
Source File: JavaCompiler.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Name readModuleName(JavaFileObject fo) {
    return parseAndGetName(fo, t -> {
        JCModuleDecl md = t.getModuleDecl();

        return md != null ? TreeInfo.fullName(md.getName()) : null;
    });
}
 
Example #13
Source File: Modules.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void checkCyclicDependencies(JCModuleDecl mod) {
    for (JCDirective d : mod.directives) {
        JCRequires rd;
        if (!d.hasTag(Tag.REQUIRES) || (rd = (JCRequires) d).directive == null)
            continue;
        Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>();
        List<ModuleSymbol> queue = List.of(rd.directive.module);
        while (queue.nonEmpty()) {
            ModuleSymbol current = queue.head;
            queue = queue.tail;
            if (!nonSyntheticDeps.add(current))
                continue;
            current.complete();
            if ((current.flags() & Flags.ACYCLIC) != 0)
                continue;
            Assert.checkNonNull(current.requires, current::toString);
            for (RequiresDirective dep : current.requires) {
                if (!dep.flags.contains(RequiresFlag.EXTRA))
                    queue = queue.prepend(dep.module);
            }
        }
        if (nonSyntheticDeps.contains(mod.sym)) {
            log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module));
        }
        mod.sym.flags_field |= Flags.ACYCLIC;
    }
}
 
Example #14
Source File: JavacTaskImpl.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void run(Queue<Env<AttrContext>> list, Iterable<? extends Element> elements) {
    Set<Element> set = new HashSet<>();
    for (Element item: elements) {
        set.add(item);
    }

    ListBuffer<Env<AttrContext>> defer = new ListBuffer<>();
    while (list.peek() != null) {
        Env<AttrContext> env = list.remove();
        Symbol test = null;

        if (env.tree.hasTag(Tag.MODULEDEF)) {
            test = ((JCModuleDecl) env.tree).sym;
        } else if (env.tree.hasTag(Tag.PACKAGEDEF)) {
            test = env.toplevel.packge;
        } else {
            ClassSymbol csym = env.enclClass.sym;
            if (csym != null)
                test = csym.outermostClass();
        }
        if (test != null && set.contains(test))
            process(env);
        else
            defer = defer.append(env);
    }

    list.addAll(defer);
}
 
Example #15
Source File: Enter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Create a fresh environment for modules.
 *
 *  @param tree     The module definition.
 *  @param env      The environment current outside of the module definition.
 */
public Env<AttrContext> moduleEnv(JCModuleDecl tree, Env<AttrContext> env) {
    Assert.checkNonNull(tree.sym);
    Env<AttrContext> localEnv =
        env.dup(tree, env.info.dup(WriteableScope.create(tree.sym)));
    localEnv.enclClass = predefClassDef;
    localEnv.outer = env;
    localEnv.info.isSelfCall = false;
    localEnv.info.lint = null; // leave this to be filled in by Attr,
                               // when annotations have been processed
    return localEnv;
}
 
Example #16
Source File: Enter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void visitModuleDef(JCModuleDecl tree) {
    Env<AttrContext> moduleEnv = moduleEnv(tree, env);
    typeEnvs.put(tree.sym, moduleEnv);
    if (modules.isInModuleGraph(tree.sym)) {
        todo.append(moduleEnv);
    }
}
 
Example #17
Source File: Modules.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Completer getSourceCompleter(JCCompilationUnit tree) {
    return new Completer() {
        @Override
        public void complete(Symbol sym) throws CompletionFailure {
            ModuleSymbol msym = (ModuleSymbol) sym;
            msym.flags_field |= UNATTRIBUTED;
            ModuleVisitor v = new ModuleVisitor();
            JavaFileObject prev = log.useSource(tree.sourcefile);
            JCModuleDecl moduleDecl = tree.getModuleDecl();
            DiagnosticPosition prevLintPos = deferredLintHandler.setPos(moduleDecl.pos());

            try {
                moduleDecl.accept(v);
                completeModule(msym);
                checkCyclicDependencies(moduleDecl);
            } finally {
                log.useSource(prev);
                deferredLintHandler.setPos(prevLintPos);
                msym.flags_field &= ~UNATTRIBUTED;
            }
        }

        @Override
        public String toString() {
            return "SourceCompleter: " + tree.sourcefile.getName();
        }

    };
}
 
Example #18
Source File: JavacTaskImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void run(Queue<Env<AttrContext>> list, Iterable<? extends Element> elements) {
    Set<Element> set = new HashSet<>();
    for (Element item: elements) {
        set.add(item);
    }

    ListBuffer<Env<AttrContext>> defer = new ListBuffer<>();
    while (list.peek() != null) {
        Env<AttrContext> env = list.remove();
        Symbol test = null;

        if (env.tree.hasTag(Tag.MODULEDEF)) {
            test = ((JCModuleDecl) env.tree).sym;
        } else if (env.tree.hasTag(Tag.PACKAGEDEF)) {
            test = env.toplevel.packge;
        } else {
            ClassSymbol csym = env.enclClass.sym;
            if (csym != null)
                test = csym.outermostClass();
        }
        if (test != null && set.contains(test))
            process(env);
        else
            defer = defer.append(env);
    }

    list.addAll(defer);
}
 
Example #19
Source File: JavaCompiler.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Name readModuleName(JavaFileObject fo) {
    return parseAndGetName(fo, t -> {
        JCModuleDecl md = t.getModuleDecl();

        return md != null ? TreeInfo.fullName(md.getName()) : null;
    });
}
 
Example #20
Source File: Modules.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void checkCyclicDependencies(JCModuleDecl mod) {
    for (JCDirective d : mod.directives) {
        JCRequires rd;
        if (!d.hasTag(Tag.REQUIRES) || (rd = (JCRequires) d).directive == null)
            continue;
        Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>();
        List<ModuleSymbol> queue = List.of(rd.directive.module);
        while (queue.nonEmpty()) {
            ModuleSymbol current = queue.head;
            queue = queue.tail;
            if (!nonSyntheticDeps.add(current))
                continue;
            current.complete();
            if ((current.flags() & Flags.ACYCLIC) != 0)
                continue;
            Assert.checkNonNull(current.requires, current::toString);
            for (RequiresDirective dep : current.requires) {
                if (!dep.flags.contains(RequiresFlag.EXTRA))
                    queue = queue.prepend(dep.module);
            }
        }
        if (nonSyntheticDeps.contains(mod.sym)) {
            log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module));
        }
        mod.sym.flags_field |= Flags.ACYCLIC;
    }
}
 
Example #21
Source File: Modules.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Completer getSourceCompleter(JCCompilationUnit tree) {
    return new Completer() {
        @Override
        public void complete(Symbol sym) throws CompletionFailure {
            ModuleSymbol msym = (ModuleSymbol) sym;
            msym.flags_field |= UNATTRIBUTED;
            ModuleVisitor v = new ModuleVisitor();
            JavaFileObject prev = log.useSource(tree.sourcefile);
            JCModuleDecl moduleDecl = tree.getModuleDecl();
            DiagnosticPosition prevLintPos = deferredLintHandler.setPos(moduleDecl.pos());

            try {
                moduleDecl.accept(v);
                completeModule(msym);
                checkCyclicDependencies(moduleDecl);
            } finally {
                log.useSource(prev);
                deferredLintHandler.setPos(prevLintPos);
                msym.flags_field &= ~UNATTRIBUTED;
            }
        }

        @Override
        public String toString() {
            return "SourceCompleter: " + tree.sourcefile.getName();
        }

    };
}
 
Example #22
Source File: Lower.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitModuleDef(JCModuleDecl tree) {
    ModuleSymbol msym = tree.sym;
    ClassSymbol c = msym.module_info;
    c.setAttributes(msym);
    c.flags_field |= Flags.MODULE;
    createInfoClass(List.nil(), tree.sym.module_info);
}
 
Example #23
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitModuleDef(JCModuleDecl tree) {
    // Do nothing for modules
}
 
Example #24
Source File: TypeEnter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void resolveImports(JCCompilationUnit tree, Env<AttrContext> env) {
    if (tree.starImportScope.isFilled()) {
        // we must have already processed this toplevel
        return;
    }

    ImportFilter prevStaticImportFilter = staticImportFilter;
    ImportFilter prevTypeImportFilter = typeImportFilter;
    DiagnosticPosition prevLintPos = deferredLintHandler.immediate();
    Lint prevLint = chk.setLint(lint);
    Env<AttrContext> prevEnv = this.env;
    try {
        this.env = env;
        final PackageSymbol packge = env.toplevel.packge;
        this.staticImportFilter =
                (origin, sym) -> sym.isStatic() &&
                                 chk.importAccessible(sym, packge) &&
                                 sym.isMemberOf((TypeSymbol) origin.owner, types);
        this.typeImportFilter =
                (origin, sym) -> sym.kind == TYP &&
                                 chk.importAccessible(sym, packge);

        // Import-on-demand java.lang.
        PackageSymbol javaLang = syms.enterPackage(syms.java_base, names.java_lang);
        if (javaLang.members().isEmpty() && !javaLang.exists())
            throw new FatalError(diags.fragment(Fragments.FatalErrNoJavaLang));
        importAll(make.at(tree.pos()).Import(make.QualIdent(javaLang), false), javaLang, env);

        JCModuleDecl decl = tree.getModuleDecl();

        // Process the package def and all import clauses.
        if (tree.getPackage() != null && decl == null)
            checkClassPackageClash(tree.getPackage());

        for (JCImport imp : tree.getImports()) {
            doImport(imp);
        }

        if (decl != null) {
            //check @Deprecated:
            markDeprecated(decl.sym, decl.mods.annotations, env);
            // process module annotations
            annotate.annotateLater(decl.mods.annotations, env, env.toplevel.modle, null);
        }
    } finally {
        this.env = prevEnv;
        chk.setLint(prevLint);
        deferredLintHandler.setPos(prevLintPos);
        this.staticImportFilter = prevStaticImportFilter;
        this.typeImportFilter = prevTypeImportFilter;
    }
}
 
Example #25
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitModuleDef(JCModuleDecl tree) {
    // Do nothing for modules
}
 
Example #26
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitModuleDef(JCModuleDecl tree) {
    // Do nothing for modules
}
 
Example #27
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitModuleDef(JCModuleDecl tree) {
    // Do nothing for modules
}