Ejemplo n.º 1
0
  @Override
  public void visitTopLevel(JCCompilationUnit tree) {
    JavaFileObject prev = log.useSource(tree.sourcefile);
    boolean addEnv = false;
    boolean isPkgInfo =
        tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
    if (tree.pid != null) {
      tree.packge = reader.enterPackage(TreeInfo.fullName(tree.pid));
      if (tree.packageAnnotations.nonEmpty() || pkginfoOpt == PkgInfo.ALWAYS) {
        if (isPkgInfo) {
          addEnv = true;
        } else {
          log.error(tree.packageAnnotations.head.pos(), "pkg.annotations.sb.in.package-info.java");
        }
      }
    } else {
      tree.packge = syms.unnamedPackage;
    }
    tree.packge.complete(); // Find all classes in package.
    Env<AttrContext> topEnv = topLevelEnv(tree);

    // Save environment of package-info.java file.
    if (isPkgInfo) {
      Env<AttrContext> env0 = typeEnvs.get(tree.packge);
      if (env0 == null) {
        typeEnvs.put(tree.packge, topEnv);
      } else {
        JCCompilationUnit tree0 = env0.toplevel;
        if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) {
          log.warning(
              tree.pid != null ? tree.pid.pos() : null, "pkg-info.already.seen", tree.packge);
          if (addEnv
              || (tree0.packageAnnotations.isEmpty()
                  && tree.docComments != null
                  && tree.docComments.get(tree) != null)) {
            typeEnvs.put(tree.packge, topEnv);
          }
        }
      }

      for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner) q.flags_field |= EXISTS;

      Name name = names.package_info;
      ClassSymbol c = reader.enterClass(name, tree.packge);
      c.flatname = names.fromString(tree.packge + "." + name);
      c.sourcefile = tree.sourcefile;
      c.completer = null;
      c.members_field = new Scope(c);
      tree.packge.package_info = c;
    }
    classEnter(tree.defs, topEnv);
    if (addEnv) {
      todo.append(topEnv);
    }
    log.useSource(prev);
    result = null;
  }
Ejemplo n.º 2
0
 /**
  * Create a fresh environment for toplevels.
  *
  * @param tree The toplevel tree.
  */
 Env<AttrContext> topLevelEnv(JCCompilationUnit tree) {
   Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext());
   localEnv.toplevel = tree;
   localEnv.enclClass = predefClassDef;
   tree.namedImportScope = new ImportScope(tree.packge);
   tree.starImportScope = new StarImportScope(tree.packge);
   localEnv.info.scope = tree.namedImportScope;
   localEnv.info.lint = lint;
   return localEnv;
 }
Ejemplo n.º 3
0
 /**
  * Parse the specified files returning a list of abstract syntax trees.
  *
  * @throws java.io.IOException TODO
  * @return a list of abstract syntax trees
  */
 public Iterable<? extends CompilationUnitTree> parse() throws IOException {
   try {
     prepareCompiler();
     List<JCCompilationUnit> units = compiler.parseFiles(fileObjects);
     for (JCCompilationUnit unit : units) {
       JavaFileObject file = unit.getSourceFile();
       if (notYetEntered.containsKey(file)) notYetEntered.put(file, unit);
     }
     return units;
   } finally {
     parsed = true;
     if (compiler != null && compiler.log != null) compiler.log.flush();
   }
 }