com.sun.tools.javac.code.Symbol.PackageSymbol Java Examples

The following examples show how to use com.sun.tools.javac.code.Symbol.PackageSymbol. 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: ClassFinder.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/** Load a toplevel class with given fully qualified name
 *  The class is entered into `classes' only if load was successful.
 */
public ClassSymbol loadClass(ModuleSymbol msym, Name flatname) throws CompletionFailure {
    Assert.checkNonNull(msym);
    Name packageName = Convert.packagePart(flatname);
    PackageSymbol ps = syms.lookupPackage(msym, packageName);

    Assert.checkNonNull(ps.modle, () -> "msym=" + msym + "; flatName=" + flatname);

    boolean absent = syms.getClass(ps.modle, flatname) == null;
    ClassSymbol c = syms.enterClass(ps.modle, flatname);

    if (c.members_field == null) {
        try {
            c.complete();
        } catch (CompletionFailure ex) {
            if (absent) syms.removeClass(ps.modle, flatname);
            throw ex;
        }
    }
    return c;
}
 
Example #2
Source File: ElementsTable.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private Set<PackageElement> getAllModulePackages(ModuleElement mdle) throws ToolException {
    Set<PackageElement> result = new HashSet<>();
    ModuleSymbol msym = (ModuleSymbol) mdle;
    List<Location> msymlocs = getModuleLocation(locations, msym.name.toString());
    for (Location msymloc : msymlocs) {
        for (JavaFileObject fo : fmList(msymloc, "", sourceKinds, true)) {
            if (fo.getName().endsWith("module-info.java")) {
                continue;
            }
            String binaryName = fm.inferBinaryName(msymloc, fo);
            String pn = getPackageName(binaryName);
            PackageSymbol psym = syms.enterPackage(msym, names.fromString(pn));
            result.add((PackageElement) psym);
        }
    }
    return result;
}
 
Example #3
Source File: JavacFiler.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private ModuleSymbol inferModule(String pkg) {
    if (modules.getDefaultModule() == syms.noModule)
        return modules.getDefaultModule();

    Set<ModuleSymbol> rootModules = modules.getRootModules();

    if (rootModules.size() == 1) {
        return rootModules.iterator().next();
    }

    PackageSymbol pack = elementUtils.getPackageElement(pkg);

    if (pack != null && pack.modle != syms.unnamedModule) {
        return pack.modle;
    }

    return null;
}
 
Example #4
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean checkTypeContainsImportableElement(TypeSymbol tsym, TypeSymbol origin, PackageSymbol packge, Name name, Set<Symbol> processed) {
    if (tsym == null || !processed.add(tsym))
        return false;

        // also search through inherited names
    if (checkTypeContainsImportableElement(types.supertype(tsym.type).tsym, origin, packge, name, processed))
        return true;

    for (Type t : types.interfaces(tsym.type))
        if (checkTypeContainsImportableElement(t.tsym, origin, packge, name, processed))
            return true;

    for (Symbol sym : tsym.members().getSymbolsByName(name)) {
        if (sym.isStatic() &&
            importAccessible(sym, packge) &&
            sym.isMemberOf(origin, types)) {
            return true;
        }
    }

    return false;
}
 
Example #5
Source File: ClassFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Completion for classes to be loaded. Before a class is loaded
 *  we make sure its enclosing class (if any) is loaded.
 */
private void complete(Symbol sym) throws CompletionFailure {
    if (sym.kind == TYP) {
        try {
            ClassSymbol c = (ClassSymbol) sym;
            dependencies.push(c, CompletionCause.CLASS_READER);
            annotate.blockAnnotations();
            c.members_field = new Scope.ErrorScope(c); // make sure it's always defined
            completeOwners(c.owner);
            completeEnclosing(c);
            fillIn(c);
        } finally {
            annotate.unblockAnnotationsNoFlush();
            dependencies.pop();
        }
    } else if (sym.kind == PCK) {
        PackageSymbol p = (PackageSymbol)sym;
        try {
            fillIn(p);
        } catch (IOException ex) {
            throw new CompletionFailure(sym, ex.getLocalizedMessage()).initCause(ex);
        }
    }
    if (!reader.filling)
        annotate.flush(); // finish attaching annotations
}
 
Example #6
Source File: ClassFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Load a toplevel class with given fully qualified name
 *  The class is entered into `classes' only if load was successful.
 */
public ClassSymbol loadClass(ModuleSymbol msym, Name flatname) throws CompletionFailure {
    Assert.checkNonNull(msym);
    Name packageName = Convert.packagePart(flatname);
    PackageSymbol ps = syms.lookupPackage(msym, packageName);

    Assert.checkNonNull(ps.modle, () -> "msym=" + msym + "; flatName=" + flatname);

    boolean absent = syms.getClass(ps.modle, flatname) == null;
    ClassSymbol c = syms.enterClass(ps.modle, flatname);

    if (c.members_field == null) {
        try {
            c.complete();
        } catch (CompletionFailure ex) {
            if (absent) syms.removeClass(ps.modle, flatname);
            throw ex;
        }
    }
    return c;
}
 
Example #7
Source File: Symtab.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public ModuleSymbol inferModule(Name packageName) {
    if (packageName.isEmpty())
        return java_base == noModule ? noModule : unnamedModule;//!

    ModuleSymbol msym = null;
    Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
    if (map == null)
        return null;
    for (Map.Entry<ModuleSymbol,PackageSymbol> e: map.entrySet()) {
        if (!e.getValue().members().isEmpty()) {
            if (msym == null) {
                msym = e.getKey();
            } else {
                return null;
            }
        }
    }
    return msym;
}
 
Example #8
Source File: UClassIdentTest.java    From Refaster with Apache License 2.0 6 votes vote down vote up
@Test
public void importConflicts() {
  ImportPolicy.bind(context, ImportPolicy.IMPORT_TOP_LEVEL);
  context.put(PackageSymbol.class, Symtab.instance(context).rootPackage);
  // Test fully qualified class names
  inliner.addImport("package.Exception");
  assertInlines("Exception", UClassIdent.create("package.Exception"));
  // Will import "anotherPackage.Exception" due to conflicts
  assertInlines("anotherPackage.Exception", UClassIdent.create("anotherPackage.Exception"));
  new EqualsTester()
      .addEqualityGroup(inliner.getImportsToAdd(),
          ImmutableSet.of("package.Exception"))
      .testEquals();
  // Test nested class names
  inliner.addImport("package.subpackage.Foo.Bar");
  // Will import "package.Foo"
  assertInlines("Foo.Bar", UClassIdent.create("package.Foo.Bar"));
  assertInlines("Bar", UClassIdent.create("package.subpackage.Foo.Bar"));
  // Will not import "anotherPackage.Foo" due to conflicts
  assertInlines("anotherPackage.Foo.Bar", UClassIdent.create("anotherPackage.Foo.Bar"));
  new EqualsTester()
      .addEqualityGroup(inliner.getImportsToAdd(),
          ImmutableSet.of("package.Exception", "package.subpackage.Foo.Bar", "package.Foo"))
      .testEquals();
}
 
Example #9
Source File: Symtab.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Create a new member or toplevel class symbol with given flat name
 *  and enter in `classes' unless already there.
 */
public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) {
    Assert.checkNonNull(msym);
    PackageSymbol ps = lookupPackage(msym, Convert.packagePart(flatname));
    Assert.checkNonNull(ps);
    Assert.checkNonNull(ps.modle);
    ClassSymbol c = getClass(ps.modle, flatname);
    if (c == null) {
        c = defineClass(Convert.shortName(flatname), ps);
        doEnterClass(ps.modle, c);
        return c;
    } else
        return c;
}
 
Example #10
Source File: ClassFinder.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Completion for classes to be loaded. Before a class is loaded
 *  we make sure its enclosing class (if any) is loaded.
 */
private void complete(Symbol sym) throws CompletionFailure {
    if (sym.kind == TYP) {
        try {
            ClassSymbol c = (ClassSymbol) sym;
            dependencies.push(c, CompletionCause.CLASS_READER);
            annotate.blockAnnotations();
            c.members_field = new Scope.ErrorScope(c); // make sure it's always defined
            completeOwners(c.owner);
            completeEnclosing(c);
            fillIn(c);
        } finally {
            annotate.unblockAnnotationsNoFlush();
            dependencies.pop();
        }
    } else if (sym.kind == PCK) {
        PackageSymbol p = (PackageSymbol)sym;
        try {
            fillIn(p);
        } catch (IOException ex) {
            JCDiagnostic msg =
                    diagFactory.fragment(Fragments.ExceptionMessage(ex.getLocalizedMessage()));
            throw new CompletionFailure(sym, msg).initCause(ex);
        }
    }
    if (!reader.filling)
        annotate.flush(); // finish attaching annotations
}
 
Example #11
Source File: Symtab.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addRootPackageFor(ModuleSymbol module) {
    doEnterPackage(module, rootPackage);
    PackageSymbol unnamedPackage = new PackageSymbol(names.empty, rootPackage) {
            @Override
            public String toString() {
                return messages.getLocalizedString("compiler.misc.unnamed.package");
            }
        };
    unnamedPackage.modle = module;
    //we cannot use a method reference below, as initialCompleter might be null now
    unnamedPackage.completer = s -> initialCompleter.complete(s);
    module.unnamedPackage = unnamedPackage;
}
 
Example #12
Source File: ElementsTable.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a list of all classes contained in this package, including
 * member classes of those classes, and their member classes, etc.
 */
private void addAllClasses(Collection<TypeElement> list, PackageElement pkg) {
    boolean filtered = true;
    PackageSymbol sym = (PackageSymbol)pkg;
    for (Symbol isym : sym.members().getSymbols(NON_RECURSIVE)) {
        addAllClasses(list, (TypeElement)isym, filtered);
    }
}
 
Example #13
Source File: JCTree.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
protected JCCompilationUnit(List<JCAnnotation> packageAnnotations,
                            JCExpression pid,
                            List<JCTree> defs,
                            JavaFileObject sourcefile,
                            PackageSymbol packge,
                            ImportScope namedImportScope,
                            StarImportScope starImportScope) {
    this.packageAnnotations = packageAnnotations;
    this.pid = pid;
    this.defs = defs;
    this.sourcefile = sourcefile;
    this.packge = packge;
    this.namedImportScope = namedImportScope;
    this.starImportScope = starImportScope;
}
 
Example #14
Source File: DocEnv.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the PackageDoc of this package symbol.
 */
public PackageDocImpl getPackageDoc(PackageSymbol pack) {
    PackageDocImpl result = packageMap.get(pack);
    if (result != null) return result;
    result = new PackageDocImpl(this, pack);
    packageMap.put(pack, result);
    return result;
}
 
Example #15
Source File: JavacProcessingEnvironment.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<PackageSymbol> getPackageInfoFiles(List<? extends JCCompilationUnit> units) {
    List<PackageSymbol> packages = List.nil();
    for (JCCompilationUnit unit : units) {
        if (isPkgInfo(unit.sourcefile, JavaFileObject.Kind.SOURCE)) {
            packages = packages.prepend(unit.packge);
        }
    }
    return packages.reverse();
}
 
Example #16
Source File: JavacProcessingEnvironment.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Enter a set of generated class files. */
private List<ClassSymbol> enterClassFiles(Map<ModuleSymbol, Map<String, JavaFileObject>> modulesAndClassFiles) {
    List<ClassSymbol> list = List.nil();

    for (Entry<ModuleSymbol, Map<String, JavaFileObject>> moduleAndClassFiles : modulesAndClassFiles.entrySet()) {
        for (Map.Entry<String,JavaFileObject> entry : moduleAndClassFiles.getValue().entrySet()) {
            Name name = names.fromString(entry.getKey());
            JavaFileObject file = entry.getValue();
            if (file.getKind() != JavaFileObject.Kind.CLASS)
                throw new AssertionError(file);
            ClassSymbol cs;
            if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) {
                Name packageName = Convert.packagePart(name);
                PackageSymbol p = symtab.enterPackage(moduleAndClassFiles.getKey(), packageName);
                if (p.package_info == null)
                    p.package_info = symtab.enterClass(moduleAndClassFiles.getKey(), Convert.shortName(name), p);
                cs = p.package_info;
                cs.reset();
                if (cs.classfile == null)
                    cs.classfile = file;
                cs.completer = initialCompleter;
            } else {
                cs = symtab.enterClass(moduleAndClassFiles.getKey(), name);
                cs.reset();
                cs.classfile = file;
                cs.completer = initialCompleter;
                cs.owner.members().enter(cs); //XXX - OverwriteBetweenCompilations; syms.getClass is not sufficient anymore
            }
            list = list.prepend(cs);
        }
    }
    return list.reverse();
}
 
Example #17
Source File: PackageDocImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
    foundDoc = (documentation != null);
}
 
Example #18
Source File: PackageDocImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
    foundDoc = (documentation != null);
}
 
Example #19
Source File: ClassFinder.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Load directory of package into members scope.
 */
private void fillIn(PackageSymbol p) throws IOException {
    if (p.members_field == null)
        p.members_field = WriteableScope.create(p);

    ModuleSymbol msym = p.modle;

    Assert.checkNonNull(msym, p::toString);

    msym.complete();

    if (msym == syms.noModule) {
        preferCurrent = false;
        if (userPathsFirst) {
            scanUserPaths(p, true);
            preferCurrent = true;
            scanPlatformPath(p);
        } else {
            scanPlatformPath(p);
            scanUserPaths(p, true);
        }
    } else if (msym.classLocation == StandardLocation.CLASS_PATH) {
        scanUserPaths(p, msym.sourceLocation == StandardLocation.SOURCE_PATH);
    } else {
        scanModulePaths(p, msym);
    }
}
 
Example #20
Source File: ClassFinder.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("fallthrough")
private void fillIn(PackageSymbol p,
                    Location location,
                    Iterable<JavaFileObject> files)
{
    currentLoc = location;
    for (JavaFileObject fo : files) {
        switch (fo.getKind()) {
        case OTHER:
            if (!isSigFile(location, fo)) {
                extraFileActions(p, fo);
                break;
            }
            //intentional fall-through:
        case CLASS:
        case DEX_CALSS:
        case SOURCE: {
            // TODO pass binaryName to includeClassFile
            String binaryName = fileManager.inferBinaryName(currentLoc, fo);
            String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
            if (SourceVersion.isIdentifier(simpleName) ||
                simpleName.equals("package-info"))
                includeClassFile(p, fo);
            break;
        }
        default:
            extraFileActions(p, fo);
            break;
        }
    }
}
 
Example #21
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private ExportsDirective findExport(PackageSymbol pack) {
    for (ExportsDirective d : pack.modle.exports) {
        if (d.packge == pack)
            return d;
    }

    return null;
}
 
Example #22
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void checkVisible(DiagnosticPosition pos, Symbol what, PackageSymbol inPackage, boolean inSuperType) {
    if (!isAPISymbol(what) && !inSuperType) { //package private/private element
        log.warning(LintCategory.EXPORTS, pos, Warnings.LeaksNotAccessible(kindName(what), what, what.packge().modle));
        return ;
    }

    PackageSymbol whatPackage = what.packge();
    ExportsDirective whatExport = findExport(whatPackage);
    ExportsDirective inExport = findExport(inPackage);

    if (whatExport == null) { //package not exported:
        log.warning(LintCategory.EXPORTS, pos, Warnings.LeaksNotAccessibleUnexported(kindName(what), what, what.packge().modle));
        return ;
    }

    if (whatExport.modules != null) {
        if (inExport.modules == null || !whatExport.modules.containsAll(inExport.modules)) {
            log.warning(LintCategory.EXPORTS, pos, Warnings.LeaksNotAccessibleUnexportedQualified(kindName(what), what, what.packge().modle));
        }
    }

    if (whatPackage.modle != inPackage.modle && whatPackage.modle != syms.java_base) {
        //check that relativeTo.modle requires transitive what.modle, somehow:
        List<ModuleSymbol> todo = List.of(inPackage.modle);

        while (todo.nonEmpty()) {
            ModuleSymbol current = todo.head;
            todo = todo.tail;
            if (current == whatPackage.modle)
                return ; //OK
            for (RequiresDirective req : current.requires) {
                if (req.isTransitive()) {
                    todo = todo.prepend(req.module);
                }
            }
        }

        log.warning(LintCategory.EXPORTS, pos, Warnings.LeaksNotAccessibleNotRequiredTransitive(kindName(what), what, what.packge().modle));
    }
}
 
Example #23
Source File: DocEnv.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create the PackageDoc (or a subtype) for a package symbol.
 */
void makePackageDoc(PackageSymbol pack, TreePath treePath) {
    PackageDocImpl result = packageMap.get(pack);
    if (result != null) {
        if (treePath != null) result.setTreePath(treePath);
    } else {
        result = new PackageDocImpl(this, pack, treePath);
        packageMap.put(pack, result);
    }
}
 
Example #24
Source File: JavadocClassFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Override extraFileActions to check for package documentation
 */
@Override
protected void extraFileActions(PackageSymbol pack, JavaFileObject fo) {
    if (fo.isNameCompatible("package", JavaFileObject.Kind.HTML)) {
        pack.sourcefile = fo;
    }
}
 
Example #25
Source File: RefasterRule.java    From Refaster with Apache License 2.0 5 votes vote down vote up
private Context prepareContext(Context baseContext, JCCompilationUnit compilationUnit) {
  Context context = new SubContext(baseContext);
  if (context.get(JavaFileManager.class) == null) {
    JavacFileManager.preRegister(context);
  }
  ImportPolicy.bind(context, importPolicy());
  context.put(JCCompilationUnit.class, compilationUnit);
  context.put(PackageSymbol.class, compilationUnit.packge);
  return context;
}
 
Example #26
Source File: ClassFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Load directory of package into members scope.
 */
private void fillIn(PackageSymbol p) throws IOException {
    if (p.members_field == null)
        p.members_field = WriteableScope.create(p);

    ModuleSymbol msym = p.modle;

    Assert.checkNonNull(msym, p::toString);

    msym.complete();

    if (msym == syms.noModule) {
        preferCurrent = false;
        if (userPathsFirst) {
            scanUserPaths(p, true);
            preferCurrent = true;
            scanPlatformPath(p);
        } else {
            scanPlatformPath(p);
            scanUserPaths(p, true);
        }
    } else if (msym.classLocation == StandardLocation.CLASS_PATH) {
        scanUserPaths(p, msym.sourceLocation == StandardLocation.SOURCE_PATH);
    } else {
        scanModulePaths(p, msym);
    }
}
 
Example #27
Source File: JCTree.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
protected JCCompilationUnit(List<JCAnnotation> packageAnnotations,
                            JCExpression pid,
                            List<JCTree> defs,
                            JavaFileObject sourcefile,
                            PackageSymbol packge,
                            ImportScope namedImportScope,
                            StarImportScope starImportScope) {
    this.packageAnnotations = packageAnnotations;
    this.pid = pid;
    this.defs = defs;
    this.sourcefile = sourcefile;
    this.packge = packge;
    this.namedImportScope = namedImportScope;
    this.starImportScope = starImportScope;
}
 
Example #28
Source File: ElementsTable.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private ModuleSymbol findModuleOfPackageName(String packageName) {
        Name pack = names.fromString(packageName);
        for (ModuleSymbol msym : modules.allModules()) {
            PackageSymbol p = syms.getPackage(msym, pack);
            if (p != null && !p.members().isEmpty()) {
                return msym;
            }
        }
        return null;
}
 
Example #29
Source File: PackageDocImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 */
public PackageDocImpl(DocEnv env, PackageSymbol sym, TreePath treePath) {
    super(env, treePath);
    this.sym = sym;
    this.tree = (treePath == null) ? null : (JCCompilationUnit) treePath.getCompilationUnit();
    foundDoc = (documentation != null);
}
 
Example #30
Source File: Symtab.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public List<ModuleSymbol> listPackageModules(Name packageName) {
    if (packageName.isEmpty())
        return List.nil();

    List<ModuleSymbol> result = List.nil();
    Map<ModuleSymbol,PackageSymbol> map = packages.get(packageName);
    if (map != null) {
        for (Map.Entry<ModuleSymbol, PackageSymbol> e: map.entrySet()) {
            if (!e.getValue().members().isEmpty()) {
                result = result.prepend(e.getKey());
            }
        }
    }
    return result;
}