Exemplo n.º 1
0
  /**
   * Translate the given abstract syntax trees to elements.
   *
   * @param trees a list of abstract syntax trees.
   * @throws java.io.IOException TODO
   * @return a list of elements corresponding to the top level classes in the abstract syntax trees
   */
  public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees)
      throws IOException {
    prepareCompiler();

    ListBuffer<JCCompilationUnit> roots = null;

    if (trees == null) {
      // If there are still files which were specified to be compiled
      // (i.e. in fileObjects) but which have not yet been entered,
      // then we make sure they have been parsed and add them to the
      // list to be entered.
      if (notYetEntered.size() > 0) {
        if (!parsed) parse(); // TODO would be nice to specify files needed to be parsed
        for (JavaFileObject file : fileObjects) {
          JCCompilationUnit unit = notYetEntered.remove(file);
          if (unit != null) {
            if (roots == null) roots = new ListBuffer<JCCompilationUnit>();
            roots.append(unit);
          }
        }
        notYetEntered.clear();
      }
    } else {
      for (CompilationUnitTree cu : trees) {
        if (cu instanceof JCCompilationUnit) {
          if (roots == null) roots = new ListBuffer<JCCompilationUnit>();
          roots.append((JCCompilationUnit) cu);
          notYetEntered.remove(cu.getSourceFile());
        } else throw new IllegalArgumentException(cu.toString());
      }
    }

    if (roots == null) return List.nil();

    try {
      List<JCCompilationUnit> units = compiler.enterTrees(roots.toList());

      if (notYetEntered.isEmpty()) compiler = compiler.processAnnotations(units);

      ListBuffer<TypeElement> elements = new ListBuffer<TypeElement>();
      for (JCCompilationUnit unit : units) {
        for (JCTree node : unit.defs) {
          if (node.getTag() == JCTree.CLASSDEF) {
            JCClassDecl cdef = (JCClassDecl) node;
            if (cdef.sym != null) // maybe null if errors in anno processing
            elements.append(cdef.sym);
          }
        }
      }
      return elements.toList();
    } finally {
      compiler.log.flush();
    }
  }
Exemplo n.º 2
0
 /**
  * Visitor method: enter all classes in given tree, catching any completion failure exceptions.
  * Return the tree's type.
  *
  * @param tree The tree to be visited.
  * @param env The environment visitor argument.
  */
 Type classEnter(JCTree tree, Env<AttrContext> env) {
   Env<AttrContext> prevEnv = this.env;
   try {
     this.env = env;
     tree.accept(this);
     return result;
   } catch (CompletionFailure ex) {
     return chk.completionError(tree.pos(), ex);
   } finally {
     this.env = prevEnv;
   }
 }
 private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) {
   List<ClassSymbol> classes = List.nil();
   for (JCCompilationUnit unit : units) {
     for (JCTree node : unit.defs) {
       if (node.getTag() == JCTree.CLASSDEF) {
         ClassSymbol sym = ((JCClassDecl) node).sym;
         Assert.checkNonNull(sym);
         classes = classes.prepend(sym);
       }
     }
   }
   return classes.reverse();
 }
Exemplo n.º 4
0
 public JCTree visitOther(Tree node, P p) {
   JCTree tree = (JCTree) node;
   switch (tree.getTag()) {
     case JCTree.LETEXPR:
       {
         LetExpr t = (LetExpr) node;
         List<JCVariableDecl> defs = copy(t.defs, p);
         JCTree expr = copy(t.expr, p);
         return M.at(t.pos).LetExpr(defs, expr);
       }
     default:
       throw new AssertionError("unknown tree tag: " + tree.getTag());
   }
 }
Exemplo n.º 5
0
 // prints the brackets of a nested array in reverse order
 // tree is either JCArrayTypeTree or JCAnnotatedTypeTree
 private void printBrackets(JCTree tree) throws IOException {
   JCTree elem = tree;
   while (true) {
     if (elem.hasTag(ANNOTATED_TYPE)) {
       JCAnnotatedType atype = (JCAnnotatedType) elem;
       elem = atype.underlyingType;
       if (elem.hasTag(TYPEARRAY)) {
         print(' ');
         printTypeAnnotations(atype.annotations);
       }
     }
     if (elem.hasTag(TYPEARRAY)) {
       print("[]");
       elem = ((JCArrayTypeTree) elem).elemtype;
     } else {
       break;
     }
   }
 }
Exemplo n.º 6
0
 /**
  * Visitor method: print expression tree.
  *
  * @param prec The current precedence level.
  */
 public void printExpr(JCTree tree, int prec) throws IOException {
   int prevPrec = this.prec;
   try {
     this.prec = prec;
     if (tree == null) print("/*missing*/");
     else {
       tree.accept(this);
     }
   } catch (UncheckedIOException ex) {
     IOException e = new IOException(ex.getMessage());
     e.initCause(ex);
     throw e;
   } finally {
     this.prec = prevPrec;
   }
 }
Exemplo n.º 7
0
 public void scan(JCTree tree) {
   if (tree != null && !result) tree.accept(this);
 }
Exemplo n.º 8
0
 /** Is the given tree an enumerator definition? */
 boolean isEnumerator(JCTree t) {
   return t.getTag() == JCTree.VARDEF && (((JCVariableDecl) t).mods.flags & ENUM) != 0;
 }
 public void scan(JCTree node) {
   super.scan(node);
   if (node != null) node.type = null;
 }
Exemplo n.º 10
0
 /** Visitor method: Scan a single node. */
 public void scan(JCTree tree) {
   if (tree != null) tree.accept(this);
 }
Exemplo n.º 11
0
 /** Is the given tree an enumerator definition? */
 boolean isEnumerator(JCTree t) {
   return t.hasTag(VARDEF) && (((JCVariableDecl) t).mods.flags & ENUM) != 0;
 }