Esempio n. 1
0
 /** Print a block. */
 public void printEnumBody(List<JCTree> stats) throws IOException {
   print("{");
   println();
   indent();
   boolean first = true;
   for (List<JCTree> l = stats; l.nonEmpty(); l = l.tail) {
     if (isEnumerator(l.head)) {
       if (!first) {
         print(",");
         println();
       }
       align();
       printStat(l.head);
       first = false;
     }
   }
   print(";");
   println();
   int x = 0;
   for (List<JCTree> l = stats; l.nonEmpty(); l = l.tail) {
     x++;
     if (!isEnumerator(l.head)) {
       align();
       printStat(l.head);
       println();
     }
   }
   undent();
   align();
   print("}");
 }
Esempio n. 2
0
 /**
  * Derived visitor method: print list of expression trees, separated by given string.
  *
  * @param sep the separator string
  */
 public <T extends JCTree> void printExprs(List<T> trees, String sep) throws IOException {
   if (trees.nonEmpty()) {
     printExpr(trees.head);
     for (List<T> l = trees.tail; l.nonEmpty(); l = l.tail) {
       print(sep);
       printExpr(l.head);
     }
   }
 }
Esempio n. 3
0
 /**
  * Converts a class name into a (possibly localized) string. Anonymous inner classes gets
  * converted into a localized string.
  *
  * @param t the type of the class whose name is to be rendered
  * @param longform if set, the class' fullname is displayed - if unset the short name is chosen
  *     (w/o package)
  * @param locale the locale in which the string is to be rendered
  * @return localized string representation
  */
 protected String className(ClassType t, boolean longform, Locale locale) {
   Symbol sym = t.tsym;
   if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
     StringBuilder s = new StringBuilder(visit(t.supertype_field, locale));
     for (List<Type> is = t.interfaces_field; is.nonEmpty(); is = is.tail) {
       s.append("&");
       s.append(visit(is.head, locale));
     }
     return s.toString();
   } else if (sym.name.length() == 0) {
     String s;
     ClassType norm = (ClassType) t.tsym.type;
     if (norm == null) {
       s = localize(locale, "compiler.misc.anonymous.class", (Object) null);
     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
       s =
           localize(
               locale, "compiler.misc.anonymous.class", visit(norm.interfaces_field.head, locale));
     } else {
       s = localize(locale, "compiler.misc.anonymous.class", visit(norm.supertype_field, locale));
     }
     return s;
   } else if (longform) {
     return sym.getQualifiedName().toString();
   } else {
     return sym.name.toString();
   }
 }
Esempio n. 4
0
 /** Derived visitor method: print list of statements, each on a separate line. */
 public void printStats(List<? extends JCTree> trees) throws IOException {
   for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) {
     align();
     printStat(l.head);
     println();
   }
 }
Esempio n. 5
0
 /** If type parameter list is non-empty, print it enclosed in "<...>" brackets. */
 public void printTypeParameters(List<JCTypeParameter> trees) throws IOException {
   if (trees.nonEmpty()) {
     print("<");
     printExprs(trees);
     print(">");
   }
 }
Esempio n. 6
0
 /**
  * Print unit consisting of package clause and import statements in toplevel, followed by class
  * definition. if class definition == null, print all definitions in toplevel.
  *
  * @param tree The toplevel tree
  * @param cdef The class definition, which is assumed to be part of the toplevel tree.
  */
 public void printUnit(JCCompilationUnit tree, JCClassDecl cdef) throws IOException {
   docComments = tree.docComments;
   printDocComment(tree);
   if (tree.pid != null) {
     consumeComments(tree.pos);
     print("package ");
     printExpr(tree.pid);
     print(";");
     println();
   }
   boolean firstImport = true;
   for (List<JCTree> l = tree.defs;
       l.nonEmpty() && (cdef == null || getTag(l.head) == IMPORT);
       l = l.tail) {
     if (getTag(l.head) == IMPORT) {
       JCImport imp = (JCImport) l.head;
       Name name = TreeInfo.name(imp.qualid);
       if (name == name.table.fromChars(new char[] {'*'}, 0, 1)
           || cdef == null
           || isUsed(TreeInfo.symbol(imp.qualid), cdef)) {
         if (firstImport) {
           firstImport = false;
           println();
         }
         printStat(imp);
       }
     } else {
       printStat(l.head);
     }
   }
   if (cdef != null) {
     printStat(cdef);
     println();
   }
 }
Esempio n. 7
0
 public void visitForLoop(JCForLoop tree) {
   try {
     print("for (");
     if (tree.init.nonEmpty()) {
       if (getTag(tree.init.head) == VARDEF) {
         printExpr(tree.init.head);
         for (List<JCStatement> l = tree.init.tail; l.nonEmpty(); l = l.tail) {
           JCVariableDecl vdef = (JCVariableDecl) l.head;
           print(", " + vdef.name + " = ");
           printExpr(vdef.init);
         }
       } else {
         printExprs(tree.init);
       }
     }
     print("; ");
     if (tree.cond != null) printExpr(tree.cond);
     print("; ");
     printExprs(tree.step);
     print(") ");
     printStat(tree.body);
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
Esempio n. 8
0
 public void visitTry(JCTry tree) {
   try {
     print("try ");
     if (tree.resources.nonEmpty()) {
       print("(");
       boolean first = true;
       for (JCTree var : tree.resources) {
         if (!first) {
           println();
           indent();
         }
         printStat(var);
         first = false;
       }
       print(") ");
     }
     printStat(tree.body);
     for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
       printStat(l.head);
     }
     if (tree.finalizer != null) {
       print(" finally ");
       printStat(tree.finalizer);
     }
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
Esempio n. 9
0
 public void printAnnotations(List<JCAnnotation> trees) throws IOException {
   for (List<JCAnnotation> l = trees; l.nonEmpty(); l = l.tail) {
     printStat(l.head);
     println();
     align();
   }
 }
Esempio n. 10
0
 /** Visitor method: enter classes of a list of trees, returning a list of types. */
 <T extends JCTree> List<Type> classEnter(List<T> trees, Env<AttrContext> env) {
   ListBuffer<Type> ts = new ListBuffer<Type>();
   for (List<T> l = trees; l.nonEmpty(); l = l.tail) {
     Type t = classEnter(l.head, env);
     if (t != null) ts.append(t);
   }
   return ts.toList();
 }
Esempio n. 11
0
  /**
   * Reverse list. If the list is empty or a singleton, then the same list is returned. Otherwise a
   * new list is formed.
   */
  public List<A> reverse() {
    // if it is empty or a singleton, return itself
    if (isEmpty() || tail.isEmpty()) return this;

    List<A> rev = nil();
    for (List<A> l = this; l.nonEmpty(); l = l.tail) rev = new List<A>(l.head, rev);
    return rev;
  }
Esempio n. 12
0
 private void consumeComments(int until) {
   CommentInfo head = comments.head;
   while (comments.nonEmpty() && head.pos < until) {
     printComment(head);
     comments = comments.tail;
     head = comments.head;
   }
 }
 private String makeSignature(boolean full) {
   StringBuilder result = new StringBuilder();
   result.append("(");
   for (List<Type> types = sym.type.getParameterTypes(); types.nonEmpty(); ) {
     Type t = types.head;
     result.append(TypeMaker.getTypeString(env, t, full));
     types = types.tail;
     if (types.nonEmpty()) {
       result.append(", ");
     }
   }
   if (isVarArgs()) {
     int len = result.length();
     result.replace(len - 2, len, "...");
   }
   result.append(")");
   return result.toString();
 }
Esempio n. 14
0
 private void consumeComments(int till) throws IOException {
   boolean prevNewLine = onNewLine;
   boolean found = false;
   CommentInfo head = comments.head;
   while (comments.nonEmpty() && head.pos < till) {
     printComment(head);
     comments = comments.tail;
     head = comments.head;
   }
   if (!onNewLine && prevNewLine) {
     println();
   }
 }
Esempio n. 15
0
 /** Form a string listing all elements with given separator character. */
 public String toString(String sep) {
   if (isEmpty()) {
     return "";
   } else {
     StringBuilder buf = new StringBuilder();
     buf.append(head);
     for (List<A> l = tail; l.nonEmpty(); l = l.tail) {
       buf.append(sep);
       buf.append(l.head);
     }
     return buf.toString();
   }
 }
Esempio n. 16
0
 public void visitTry(JCTry tree) {
   try {
     print("try ");
     printStat(tree.body);
     for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
       printStat(l.head);
     }
     if (tree.finalizer != null) {
       print(" finally ");
       printStat(tree.finalizer);
     }
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
 /**
  * Process a single compound annotation, returning its Attribute. Used from MemberEnter for
  * attaching the attributes to the annotated symbol.
  */
 Attribute.Compound enterAnnotation(JCAnnotation a, Type expected, Env<AttrContext> env) {
   // The annotation might have had its type attributed (but not checked)
   // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
   // need to do it again.
   Type at =
       (a.annotationType.type != null
           ? a.annotationType.type
           : attr.attribType(a.annotationType, env));
   a.type = chk.checkType(a.annotationType.pos(), at, expected);
   if (a.type.isErroneous())
     return new Attribute.Compound(a.type, List.<Pair<MethodSymbol, Attribute>>nil());
   if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
     log.error(a.annotationType.pos(), "not.annotation.type", a.type.toString());
     return new Attribute.Compound(a.type, List.<Pair<MethodSymbol, Attribute>>nil());
   }
   List<JCExpression> args = a.args;
   if (args.length() == 1 && args.head.getTag() != JCTree.ASSIGN) {
     // special case: elided "value=" assumed
     args.head = make.at(args.head.pos).Assign(make.Ident(names.value), args.head);
   }
   ListBuffer<Pair<MethodSymbol, Attribute>> buf = new ListBuffer<Pair<MethodSymbol, Attribute>>();
   for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
     JCExpression t = tl.head;
     if (t.getTag() != JCTree.ASSIGN) {
       log.error(t.pos(), "annotation.value.must.be.name.value");
       continue;
     }
     JCAssign assign = (JCAssign) t;
     if (assign.lhs.getTag() != JCTree.IDENT) {
       log.error(t.pos(), "annotation.value.must.be.name.value");
       continue;
     }
     JCIdent left = (JCIdent) assign.lhs;
     Symbol method =
         rs.resolveQualifiedMethod(left.pos(), env, a.type, left.name, List.<Type>nil(), null);
     left.sym = method;
     left.type = method.type;
     if (method.owner != a.type.tsym)
       log.error(left.pos(), "no.annotation.member", left.name, a.type);
     Type result = method.type.getReturnType();
     Attribute value = enterAttributeValue(result, assign.rhs, env);
     if (!method.type.isErroneous())
       buf.append(new Pair<MethodSymbol, Attribute>((MethodSymbol) method, value));
     t.type = result;
   }
   return new Attribute.Compound(a.type, buf.toList());
 }
Esempio n. 18
0
 /** Prepend given list of elements to front of list, forming and returning a new list. */
 public List<A> prependList(List<A> xs) {
   if (this.isEmpty()) return xs;
   if (xs.isEmpty()) return this;
   if (xs.tail.isEmpty()) return prepend(xs.head);
   // return this.prependList(xs.tail).prepend(xs.head);
   List<A> result = this;
   List<A> rev = xs.reverse();
   Assert.check(rev != xs);
   // since xs.reverse() returned a new list, we can reuse the
   // individual List objects, instead of allocating new ones.
   while (rev.nonEmpty()) {
     List<A> h = rev;
     rev = rev.tail;
     h.setTail(result);
     result = h;
   }
   return result;
 }
Esempio n. 19
0
  /**
   * Copy successive elements of this list into given vector until list is exhausted or end of
   * vector is reached.
   */
  @Override
  @SuppressWarnings("unchecked")
  public <T> T[] toArray(T[] vec) {
    int i = 0;
    List<A> l = this;
    Object[] dest = vec;
    while (l.nonEmpty() && i < vec.length) {
      dest[i] = l.head;
      l = l.tail;
      i++;
    }
    if (l.isEmpty()) {
      if (i < vec.length) vec[i] = null;
      return vec;
    }

    vec = (T[]) Array.newInstance(vec.getClass().getComponentType(), size());
    return toArray(vec);
  }
Esempio n. 20
0
 private void consumeTrailingComments(int from) throws IOException {
   boolean prevNewLine = onNewLine;
   CommentInfo head = comments.head;
   boolean stop = false;
   while (comments.nonEmpty()
       && head.prevEndPos == from
       && !stop
       && !(head.start == StartConnection.ON_NEXT_LINE
           || head.start == StartConnection.START_OF_LINE)) {
     from = head.endPos;
     printComment(head);
     stop = (head.end == EndConnection.ON_NEXT_LINE);
     comments = comments.tail;
     head = comments.head;
   }
   if (!onNewLine && prevNewLine) {
     println();
   }
 }
Esempio n. 21
0
  public void visitNewArray(JCNewArray tree) {
    try {
      if (tree.elemtype != null) {
        print("new ");
        JCTree elem = tree.elemtype;
        printBaseElementType(elem);

        if (!tree.annotations.isEmpty()) {
          print(' ');
          printTypeAnnotations(tree.annotations);
        }
        if (tree.elems != null) {
          print("[]");
        }

        int i = 0;
        List<List<JCAnnotation>> da = tree.dimAnnotations;
        for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) {
          if (da.size() > i && !da.get(i).isEmpty()) {
            print(' ');
            printTypeAnnotations(da.get(i));
          }
          print("[");
          i++;
          printExpr(l.head);
          print("]");
        }
        printBrackets(elem);
      }
      if (tree.elems != null) {
        print("{");
        printExprs(tree.elems);
        print("}");
      }
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }
Esempio n. 22
0
 public void visitNewArray(JCNewArray tree) {
   try {
     if (tree.elemtype != null) {
       print("new ");
       JCTree elem = tree.elemtype;
       if (elem instanceof JCArrayTypeTree) printBaseElementType((JCArrayTypeTree) elem);
       else printExpr(elem);
       for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) {
         print("[");
         printExpr(l.head);
         print("]");
       }
       if (elem instanceof JCArrayTypeTree) printBrackets((JCArrayTypeTree) elem);
     }
     if (tree.elems != null) {
       if (tree.elemtype != null) print("[]");
       print("{");
       printExprs(tree.elems);
       print("}");
     }
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
Esempio n. 23
0
 public boolean hasAnnotation(JCModifiers mods, Class<?> annotationClass) {
   for (List<JCAnnotation> l = mods.annotations; l.nonEmpty(); l = l.tail) {
     if (isAnnotation(l.head, annotationClass)) return true;
   }
   return false;
 }
Esempio n. 24
0
 /** Visitor method: scan a list of nodes. */
 public void scan(List<? extends JCTree> trees) {
   if (trees != null)
     for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) scan(l.head);
 }
 Attribute enterAttributeValue(Type expected, JCExpression tree, Env<AttrContext> env) {
   // first, try completing the attribution value sym - if a completion
   // error is thrown, we should recover gracefully, and display an
   // ordinary resolution diagnostic.
   try {
     expected.tsym.complete();
   } catch (CompletionFailure e) {
     log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
     return new Attribute.Error(expected);
   }
   if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
     Type result = attr.attribExpr(tree, env, expected);
     if (result.isErroneous()) return new Attribute.Error(expected);
     if (result.constValue() == null) {
       log.error(tree.pos(), "attribute.value.must.be.constant");
       return new Attribute.Error(expected);
     }
     result = cfolder.coerce(result, expected);
     return new Attribute.Constant(expected, result.constValue());
   }
   if (expected.tsym == syms.classType.tsym) {
     Type result = attr.attribExpr(tree, env, expected);
     if (result.isErroneous()) return new Attribute.Error(expected);
     if (TreeInfo.name(tree) != names._class) {
       log.error(tree.pos(), "annotation.value.must.be.class.literal");
       return new Attribute.Error(expected);
     }
     return new Attribute.Class(types, (((JCFieldAccess) tree).selected).type);
   }
   if ((expected.tsym.flags() & Flags.ANNOTATION) != 0
       || types.isSameType(expected, syms.annotationType)) {
     if (tree.getTag() != JCTree.ANNOTATION) {
       log.error(tree.pos(), "annotation.value.must.be.annotation");
       expected = syms.errorType;
     }
     return enterAnnotation((JCAnnotation) tree, expected, env);
   }
   if (expected.tag == TypeTags.ARRAY) { // should really be isArray()
     if (tree.getTag() != JCTree.NEWARRAY) {
       tree = make.at(tree.pos).NewArray(null, List.<JCExpression>nil(), List.of(tree));
     }
     JCNewArray na = (JCNewArray) tree;
     if (na.elemtype != null) {
       log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
       return new Attribute.Error(expected);
     }
     ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
     for (List<JCExpression> l = na.elems; l.nonEmpty(); l = l.tail) {
       buf.append(enterAttributeValue(types.elemtype(expected), l.head, env));
     }
     na.type = expected;
     return new Attribute.Array(expected, buf.toArray(new Attribute[buf.length()]));
   }
   if (expected.tag == TypeTags.CLASS && (expected.tsym.flags() & Flags.ENUM) != 0) {
     attr.attribExpr(tree, env, expected);
     Symbol sym = TreeInfo.symbol(tree);
     if (sym == null
         || TreeInfo.nonstaticSelect(tree)
         || sym.kind != Kinds.VAR
         || (sym.flags() & Flags.ENUM) == 0) {
       log.error(tree.pos(), "enum.annotation.must.be.enum.constant");
       return new Attribute.Error(expected);
     }
     VarSymbol enumerator = (VarSymbol) sym;
     return new Attribute.Enum(expected, enumerator);
   }
   if (!expected.isErroneous()) log.error(tree.pos(), "annotation.value.not.allowable.type");
   return new Attribute.Error(attr.attribExpr(tree, env, expected));
 }
  /**
   * Main method: compile a list of files, return all compiled classes
   *
   * @param filenames The names of all files to be compiled.
   */
  public List<ClassSymbol> compile(
      List<String> filenames,
      Map<String, String> origOptions,
      ClassLoader aptCL,
      AnnotationProcessorFactory providedFactory,
      java.util.Set<Class<? extends AnnotationProcessorFactory>> productiveFactories,
      java.util.Set<java.io.File> aggregateGenFiles)
      throws Throwable {
    // as a JavaCompiler can only be used once, throw an exception if
    // it has been used before.
    assert !hasBeenUsed : "attempt to reuse JavaCompiler";
    hasBeenUsed = true;

    this.aggregateGenFiles = aggregateGenFiles;

    long msec = System.currentTimeMillis();

    ListBuffer<ClassSymbol> classes = new ListBuffer<ClassSymbol>();
    try {
      JavacFileManager fm = (JavacFileManager) fileManager;
      // parse all files
      ListBuffer<JCCompilationUnit> trees = new ListBuffer<JCCompilationUnit>();
      for (List<String> l = filenames; l.nonEmpty(); l = l.tail) {
        if (classesAsDecls) {
          if (!l.head.endsWith(".java")) { // process as class file
            ClassSymbol cs = reader.enterClass(names.fromString(l.head));
            try {
              cs.complete();
            } catch (Symbol.CompletionFailure cf) {
              bark.aptError("CantFindClass", l);
              continue;
            }

            classes.append(cs); // add to list of classes
            continue;
          }
        }
        JavaFileObject fo = fm.getJavaFileObjectsFromStrings(List.of(l.head)).iterator().next();
        trees.append(parse(fo));
      }

      // enter symbols for all files
      List<JCCompilationUnit> roots = trees.toList();

      if (errorCount() == 0) {
        boolean prev = bark.setDiagnosticsIgnored(true);
        try {
          enter.main(roots);
        } finally {
          bark.setDiagnosticsIgnored(prev);
        }
      }

      if (errorCount() == 0) {
        apt.main(roots, classes, origOptions, aptCL, providedFactory, productiveFactories);
        genSourceFileNames.addAll(apt.getSourceFileNames());
        genClassFileNames.addAll(apt.getClassFileNames());
      }

    } catch (Abort ex) {
    }

    if (verbose) log.printVerbose("total", Long.toString(System.currentTimeMillis() - msec));

    chk.reportDeferredDiagnostics();

    printCount("error", errorCount());
    printCount("warn", warningCount());

    return classes.toList();
  }
Esempio n. 27
0
 public void printTypeAnnotations(List<JCAnnotation> trees) throws IOException {
   for (List<JCAnnotation> l = trees; l.nonEmpty(); l = l.tail) {
     printExpr(l.head);
     print(" ");
   }
 }