Пример #1
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) {
     print("package ");
     printExpr(tree.pid);
     print(";");
     println();
   }
   boolean firstImport = true;
   for (List<JCTree> l = tree.defs;
       l.nonEmpty() && (cdef == null || l.head.getTag() == JCTree.IMPORT);
       l = l.tail) {
     if (l.head.getTag() == JCTree.IMPORT) {
       JCImport imp = (JCImport) l.head;
       Name name = TreeInfo.name(imp.qualid);
       if (name == name.table.asterisk
           || 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();
   }
 }
Пример #2
0
 /** Even empty <code>Tree</code> should have two items. */
 public void test_empty_this() throws Exception {
   TreeInfo tree =
       parse(
           "// filler filler filler filler filler",
           "<ui:UiBinder>",
           "  <g:Tree/>",
           "</ui:UiBinder>");
   refresh();
   // bounds
   assertEquals(new Rectangle(0, 0, 450, 300), tree.getBounds());
   // has items
   assertEquals(2, ScriptUtils.evaluate("getItemCount()", tree.getObject()));
 }
Пример #3
0
  /**
   * @param mixinClassName
   * @param runTransformers
   * @return
   * @throws ClassNotFoundException
   */
  private byte[] loadMixinClass(String mixinClassName, boolean runTransformers)
      throws ClassNotFoundException {
    byte[] mixinBytes = null;

    try {
      mixinBytes = TreeInfo.loadClass(mixinClassName, runTransformers);
    } catch (ClassNotFoundException ex) {
      throw new ClassNotFoundException(
          String.format("The specified mixin '%s' was not found", mixinClassName));
    } catch (IOException ex) {
      this.logger.warn(
          "Failed to load mixin %s, the specified mixin will not be applied", mixinClassName);
      throw new InvalidMixinException(
          this, "An error was encountered whilst loading the mixin class", ex);
    }

    // Inject the mixin class name into the LaunchClassLoader's invalid
    // classes set so that any classes referencing the mixin directly will
    // cause the game to crash
    if (MixinInfo.invalidClasses != null) {
      MixinInfo.invalidClasses.add(mixinClassName);
    }

    return mixinBytes;
  }
Пример #4
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;
  }
 /** {@inheritDoc} */
 public String getDocComment() {
   // Our doc comment is contained in a map in our toplevel,
   // indexed by our tree.  Find our enter environment, which gives
   // us our toplevel.  It also gives us a tree that contains our
   // tree:  walk it to find our tree.  This is painful.
   Env<AttrContext> enterEnv = getEnterEnv();
   if (enterEnv == null) return null;
   JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
   return enterEnv.toplevel.docComments.get(tree);
 }
  /** {@inheritDoc} */
  public SourcePosition getPosition() {
    // Find the toplevel.  From there use a tree-walking utility
    // that finds the tree for our symbol, and with it the position.
    Env<AttrContext> enterEnv = getEnterEnv();
    if (enterEnv == null) return null;
    JCTree.JCCompilationUnit toplevel = enterEnv.toplevel;
    JavaFileObject sourcefile = toplevel.sourcefile;
    if (sourcefile == null) return null;
    int pos = TreeInfo.positionFor(sym, toplevel);

    return new SourcePositionImpl(sourcefile, pos, toplevel.lineMap);
  }
Пример #7
0
 /** Even empty <code>Tree</code> should have two items. */
 public void test_empty() throws Exception {
   parse(
       "// filler filler filler filler filler",
       "<ui:UiBinder>",
       "  <g:FlowPanel>",
       "    <g:Tree wbp:name='tree'/>",
       "  </g:FlowPanel>",
       "</ui:UiBinder>");
   refresh();
   TreeInfo tree = getObjectByName("tree");
   // bounds
   {
     Rectangle bounds = tree.getBounds();
     assertThat(bounds.x).isEqualTo(0);
     assertThat(bounds.y).isEqualTo(0);
     assertThat(bounds.width).isEqualTo(450);
     assertThat(bounds.height).isGreaterThan(50);
   }
   // has items
   assertEquals(2, ScriptUtils.evaluate("getItemCount()", tree.getObject()));
 }
Пример #8
0
 public void visitBinary(JCBinary tree) {
   try {
     int ownprec = TreeInfo.opPrec(tree.getTag());
     String opname = operatorName(tree.getTag());
     open(prec, ownprec);
     printExpr(tree.lhs, ownprec);
     print(" " + opname + " ");
     printExpr(tree.rhs, ownprec + 1);
     close(prec, ownprec);
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
Пример #9
0
  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()) {
        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> env = 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, env);
      } 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, env);
          }
        }
      }
    }
    classEnter(tree.defs, env);
    if (addEnv) {
      todo.append(env);
    }
    log.useSource(prev);
    result = null;
  }
Пример #10
0
 public void visitUnary(JCUnary tree) {
   try {
     int ownprec = TreeInfo.opPrec(tree.getTag());
     String opname = operatorName(tree.getTag());
     open(prec, ownprec);
     if (tree.getTag() <= JCTree.PREDEC) {
       print(opname);
       printExpr(tree.arg, ownprec);
     } else {
       printExpr(tree.arg, ownprec);
       print(opname);
     }
     close(prec, ownprec);
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
Пример #11
0
 public Iterable<? extends Tree> pathFor(CompilationUnitTree unit, Tree node) {
   return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse();
 }
Пример #12
0
 /** Print a set of modifiers. */
 public void printFlags(long flags) throws IOException {
   if ((flags & SYNTHETIC) != 0) print("/*synthetic*/ ");
   print(TreeInfo.flagNames(flags));
   if ((flags & StandardFlags) != 0) print(" ");
   if ((flags & ANNOTATION) != 0) print("@");
 }
 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));
 }
Пример #14
0
 // Prints the inner element type of a nested array
 private void printBaseElementType(JCTree tree) throws IOException {
   printExpr(TreeInfo.innermostType(tree));
 }