Java 类com.sun.tools.javac.tree.JCTree.JCUnary 实例源码

项目:incubator-netbeans    文件:CasualDiff.java   
protected int diffUnary(JCUnary oldT, JCUnary newT, int[] bounds) {
    int[] argBounds = getBounds(oldT.arg);
    boolean newOpOnLeft = newT.getKind() != Kind.POSTFIX_DECREMENT && newT.getKind() != Kind.POSTFIX_INCREMENT;
    if (newOpOnLeft) {
        if (oldT.getTag() != newT.getTag()) {
            printer.print(operatorName(newT.getTag()));
        } else {
            copyTo(bounds[0], argBounds[0]);
        }
    }
    int localPointer = diffTree(oldT.arg, newT.arg, argBounds);
    localPointer = copyUpTo(localPointer, argBounds[1]);
    if (!newOpOnLeft) {
        if (oldT.getTag() != newT.getTag()) {
            printer.print(operatorName(newT.getTag()));
        } else {
            copyUpTo(localPointer, bounds[1]);
        }
    }
    return bounds[1];
}
项目:EasyMPermission    文件:PrettyCommentsPrinter.java   
public void visitUnary(JCUnary tree) {
    try {
        int ownprec = isOwnPrec(tree);
        String opname = operatorName(treeTag(tree));
        open(prec, ownprec);
        if (isPrefixUnary(tree)) {
            print(opname);
            printExpr(tree.arg, ownprec);
        } else {
            printExpr(tree.arg, ownprec);
            print(opname);
        }
        close(prec, ownprec);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
项目:s4j    文件:Pretty.java   
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);
    }
}
项目:lombok    文件:PrettyCommentsPrinter.java   
public void visitUnary(JCUnary tree) {
    try {
        int ownprec = TreeInfo.opPrec(getTag(tree));
        String opname = operatorName(getTag(tree));
        open(prec, ownprec);
        if (getTag(tree) <= Javac.getCtcInt(JCTree.class, "PREDEC")) {
            print(opname);
            printExpr(tree.arg, ownprec);
        } else {
            printExpr(tree.arg, ownprec);
            print(opname);
        }
        close(prec, ownprec);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
项目:openjdk-jdk10    文件:Analyzer.java   
@Override @DefinedBy(Api.COMPILER_TREE)
public JCTree visitNewClass(NewClassTree node, Void aVoid) {
    JCNewClass oldNewClazz = (JCNewClass)node;
    JCNewClass newNewClazz = (JCNewClass)super.visitNewClass(node, aVoid);
    if (!oldNewClazz.args.isEmpty() && oldNewClazz.args.head.hasTag(NULLCHK)) {
        //workaround to Attr generating trees
        newNewClazz.encl = ((JCUnary)newNewClazz.args.head).arg;
        newNewClazz.args = newNewClazz.args.tail;
    }
    return newNewClazz;
}
项目:lombok-ianchiu    文件:PrettyPrinter.java   
@Override public void visitUnary(JCUnary tree) {
    String op = operator(treeTag(tree));
    if (treeTag(tree).getOperatorPrecedenceLevel() == PREFIX) {
        print(op);
        print(tree.arg);
    } else {
        print(tree.arg);
        print(op);
    }
}
项目:javaparser2jctree    文件:PrintAstVisitor.java   
public void visitUnary(JCUnary that) {
    try {
        print("JCUnary:");
    } catch (Exception e) {
    }
    super.visitUnary(that);
}
项目:android-retrolambda-lombok    文件:JcTreeConverter.java   
private <N extends Node> N setPos(JCTree node, N astNode) {
    if (astNode != null && node != null) {
        int start = node.pos;
        Integer end_ = endPosTable.get(node);
        if (node instanceof JCUnary) end_ = node.getEndPosition(endPosTable);
        int end = end_ == null ? node.getEndPosition(endPosTable) : end_;
        if (start != com.sun.tools.javac.util.Position.NOPOS && end != com.sun.tools.javac.util.Position.NOPOS) {
            astNode.setPosition(new Position(start, end));
        }
    }
    return astNode;
}
项目:error-prone    文件:PlaceholderUnificationVisitor.java   
@Override
public Choice<State<JCUnary>> visitUnary(UnaryTree node, State<?> state) {
  final Tag tag = ((JCUnary) node).getTag();
  return chooseSubtrees(
          state, s -> unifyExpression(node.getExpression(), s), expr -> maker().Unary(tag, expr))
      .condition(
          s ->
              !MUTATING_UNARY_TAGS.contains(tag)
                  || !(s.result().getExpression() instanceof PlaceholderParamIdent));
}
项目:error-prone    文件:UseCorrectAssertInTests.java   
private static void replaceAssert(
    SuggestedFix.Builder fix, AssertTree foundAssert, VisitorState state) {

  ExpressionTree expr = foundAssert.getCondition();
  expr = (ExpressionTree) TreeInfo.skipParens((JCTree) expr);

  // case: "assert !expr"
  if (expr.getKind().equals(LOGICAL_COMPLEMENT)) {
    addFix(fix, ((JCUnary) expr).getExpression(), foundAssert, state, IS_FALSE);
    return;
  }

  // case: "assert expr1.equals(expr2)"
  if (instanceMethod().onClass(Any.INSTANCE).named("equals").matches(expr, state)) {
    JCMethodInvocation equalsCall = ((JCMethodInvocation) expr);
    JCExpression expr1 = ((JCFieldAccess) ((JCMethodInvocation) expr).meth).selected;
    JCExpression expr2 = equalsCall.getArguments().get(0);

    addFix(
        fix,
        expr1,
        foundAssert,
        state,
        String.format(IS_EQUAL_TO, normalizedSourceForExpression(expr2, state)));
    return;
  }

  // case: "assert expr1 == expr2" or "assert expr1 != expr2"
  if (expr.getKind().equals(Kind.EQUAL_TO) || expr.getKind().equals(Kind.NOT_EQUAL_TO)) {
    suggestFixForSameReference(fix, foundAssert, state, expr.getKind().equals(Kind.EQUAL_TO));
    return;
  }

  // case: "assert expr", which didn't match any of the previous cases.
  addFix(fix, (JCExpression) expr, foundAssert, state, IS_TRUE);
}
项目:s4j    文件:Attr.java   
/** Make an attributed null check tree.
 */
public JCExpression makeNullCheck(JCExpression arg) {
    // optimization: X.this is never null; skip null check
    Name name = TreeInfo.name(arg);
    if (name == names._this || name == names._super) return arg;

    int optag = JCTree.NULLCHK;
    JCUnary tree = make.at(arg.pos).Unary(optag, arg);
    tree.operator = syms.nullcheck;
    tree.type = arg.type;
    return tree;
}
项目:s4j    文件:Attr.java   
public void visitUnary(JCUnary tree) {
    // Attribute arguments.
    Type argtype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC)
        ? attribTree(tree.arg, env, VAR, Type.noType)
        : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env));

    // Find operator.
    Symbol operator = tree.operator =
        rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype);

    Type owntype = types.createErrorType(tree.type);
    if (operator.kind == MTH &&
            !argtype.isErroneous()) {
        owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC)
            ? tree.arg.type
            : operator.type.getReturnType();
        int opc = ((OperatorSymbol)operator).opcode;

        // If the argument is constant, fold it.
        if (argtype.constValue() != null) {
            Type ctype = cfolder.fold1(opc, argtype);
            if (ctype != null) {
                owntype = cfolder.coerce(ctype, owntype);

                // Remove constant types from arguments to
                // conserve space. The parser will fold concatenations
                // of string literals; the code here also
                // gets rid of intermediate results when some of the
                // operands are constant identifiers.
                if (tree.arg.type.tsym == syms.stringType.tsym) {
                    tree.arg.type = syms.stringType;
                }
            }
        }
    }
    result = check(tree, owntype, VAL, pkind, pt);
}
项目:incubator-netbeans    文件:CasualDiff.java   
private boolean matchUnary(JCUnary t1, JCUnary t2) {
    return t1.operator == t2.operator && treesMatch(t1.arg, t2.arg);
}
项目:lombok-ianchiu    文件:JavacTreeMaker.java   
public JCUnary Unary(TreeTag opcode, JCExpression arg) {
    return invoke(Unary, opcode.value, arg);
}
项目:javaparser2jctree    文件:AJCUnary.java   
public AJCUnary(JCUnary ltree) {
    super(ltree.getTag(), ltree.arg);
}
项目:javaparser2jctree    文件:AJCUnary.java   
public AJCUnary(JCUnary ltree, String lcomment) {
    this(ltree);
    setComment(lcomment);
}
项目:EasyMPermission    文件:JavacTreeMaker.java   
public JCUnary Unary(TreeTag opcode, JCExpression arg) {
    return invoke(Unary, opcode.value, arg);
}
项目:EasyMPermission    文件:PrettyCommentsPrinter.java   
private boolean isPrefixUnary(JCUnary tree) {
    return treeTag(tree).isPrefixUnaryOp();
}
项目:refactor-faster    文件:ExpressionTemplate.java   
/**
 * Returns the precedence level appropriate for unambiguously printing
 * leaf as a subexpression of its parent.
 */
private static int getPrecedence(JCTree leaf, Context context) {
  JCCompilationUnit comp = context.get(JCCompilationUnit.class);
  JCTree parent = TreeInfo.pathFor(leaf, comp).get(1);

  // In general, this should match the logic in com.sun.tools.javac.tree.Pretty.
  //
  // TODO(mdempsky): There are probably cases where we could omit parentheses
  // by tweaking the returned precedence, but they need careful review.
  // For example, consider a template to replace "add(a, b)" with "a + b",
  // which applied to "x + add(y, z)" would result in "x + (y + z)".
  // In most cases, we'd likely prefer "x + y + z" instead, but those aren't
  // always equivalent: "0L + (Integer.MIN_VALUE + Integer.MIN_VALUE)" yields
  // a different value than "0L + Integer.MIN_VALUE + Integer.MIN_VALUE" due
  // to integer promotion rules.

  if (parent instanceof JCConditional) {
    // This intentionally differs from Pretty, because Pretty appears buggy:
    // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-September/007303.html
    JCConditional conditional = (JCConditional) parent;
    return TreeInfo.condPrec + ((conditional.cond == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssign) {
    JCAssign assign = (JCAssign) parent;
    return TreeInfo.assignPrec + ((assign.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssignOp) {
    JCAssignOp assignOp = (JCAssignOp) parent;
    return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCUnary) {
    return TreeInfo.opPrec(parent.getTag());
  } else if (parent instanceof JCBinary) {
    JCBinary binary = (JCBinary) parent;
    return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCTypeCast) {
    JCTypeCast typeCast = (JCTypeCast) parent;
    return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCInstanceOf) {
    JCInstanceOf instanceOf = (JCInstanceOf) parent;
    return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0);
  } else if (parent instanceof JCArrayAccess) {
    JCArrayAccess arrayAccess = (JCArrayAccess) parent;
    return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCFieldAccess) {
    JCFieldAccess fieldAccess = (JCFieldAccess) parent;
    return (fieldAccess.selected == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else {
    return TreeInfo.noPrec;
  }
}
项目:android-retrolambda-lombok    文件:JcTreePrinter.java   
@Override public void visitUnary(JCUnary tree) {
    printNode(tree);
    child("arg", tree.arg);
    property("(operator)", operatorName(getTag(tree)));
    indent--;
}
项目:android-retrolambda-lombok    文件:JcTreeConverter.java   
@Override public void visitUnary(JCUnary node) {
    UnaryExpression expr = new UnaryExpression();
    expr.rawOperand(toTree(node.getExpression()));
    expr.astOperator(JcTreeBuilder.UNARY_OPERATORS.inverse().get(getTag(node)));
    set(node, expr);
}
项目:error-prone    文件:ExpressionTemplate.java   
/**
 * Returns the precedence level appropriate for unambiguously printing leaf as a subexpression of
 * its parent.
 */
private static int getPrecedence(JCTree leaf, Context context) {
  JCCompilationUnit comp = context.get(JCCompilationUnit.class);
  JCTree parent = TreeInfo.pathFor(leaf, comp).get(1);

  // In general, this should match the logic in com.sun.tools.javac.tree.Pretty.
  //
  // TODO(mdempsky): There are probably cases where we could omit parentheses
  // by tweaking the returned precedence, but they need careful review.
  // For example, consider a template to replace "add(a, b)" with "a + b",
  // which applied to "x + add(y, z)" would result in "x + (y + z)".
  // In most cases, we'd likely prefer "x + y + z" instead, but those aren't
  // always equivalent: "0L + (Integer.MIN_VALUE + Integer.MIN_VALUE)" yields
  // a different value than "0L + Integer.MIN_VALUE + Integer.MIN_VALUE" due
  // to integer promotion rules.

  if (parent instanceof JCConditional) {
    // This intentionally differs from Pretty, because Pretty appears buggy:
    // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-September/007303.html
    JCConditional conditional = (JCConditional) parent;
    return TreeInfo.condPrec + ((conditional.cond == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssign) {
    JCAssign assign = (JCAssign) parent;
    return TreeInfo.assignPrec + ((assign.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssignOp) {
    JCAssignOp assignOp = (JCAssignOp) parent;
    return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCUnary) {
    return TreeInfo.opPrec(parent.getTag());
  } else if (parent instanceof JCBinary) {
    JCBinary binary = (JCBinary) parent;
    return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCTypeCast) {
    JCTypeCast typeCast = (JCTypeCast) parent;
    return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCInstanceOf) {
    JCInstanceOf instanceOf = (JCInstanceOf) parent;
    return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0);
  } else if (parent instanceof JCArrayAccess) {
    JCArrayAccess arrayAccess = (JCArrayAccess) parent;
    return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCFieldAccess) {
    JCFieldAccess fieldAccess = (JCFieldAccess) parent;
    return (fieldAccess.selected == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else {
    return TreeInfo.noPrec;
  }
}
项目:s4j    文件:Attr.java   
@Override
public void visitUnary(JCUnary that) {
    if (that.operator == null)
        that.operator = new OperatorSymbol(names.empty, syms.unknownType, -1, syms.noSymbol);
    super.visitUnary(that);
}
项目:error-prone-aspirator    文件:ElementsCountedInLoop.java   
/**
 * @return identifier which is being incremented by constant one. Returns null if no such
 *         identifier is found.
 */
private IdentifierTree getIncrementedIdentifer(JCStatement statement) {
  if (statement == null) {
    return null;
  }
  if (statement.getKind() == Kind.EXPRESSION_STATEMENT) {
    Tree.Kind kind = ((JCExpressionStatement) statement).getExpression().getKind();
    if (kind == Kind.PREFIX_INCREMENT || kind == Kind.POSTFIX_INCREMENT) {
      JCUnary unary = (JCUnary) ((JCExpressionStatement) statement).getExpression();
      if (unary.arg.getKind() == Kind.IDENTIFIER) {
        return (IdentifierTree) unary.arg;
      }
      return null;
    } else if (kind == Kind.PLUS_ASSIGNMENT) {
      JCAssignOp assignOp = (JCAssignOp) ((JCExpressionStatement) statement).getExpression();
      if (assignOp.lhs.getKind() == Kind.IDENTIFIER && (isConstantOne(assignOp.rhs))) {
        return (IdentifierTree) assignOp.lhs;
      }
    } else if (kind == Kind.ASSIGNMENT) {
      JCAssign assign = (JCAssign) ((JCExpressionStatement) statement).getExpression();
      if (assign.lhs.getKind() == Kind.IDENTIFIER && assign.rhs.getKind() == Kind.PLUS) {
        JCBinary binary = (JCBinary) assign.rhs;
        if (binary.lhs.getKind() == Kind.IDENTIFIER) {
          if (((JCIdent) assign.lhs).sym == ((JCIdent) binary.lhs).sym) {
            if (isConstantOne(binary.rhs)) {
              return (IdentifierTree) binary.lhs;
            }
          }
        }
        if (binary.rhs.getKind() == Kind.IDENTIFIER) {
          if (((JCIdent) assign.lhs).sym == ((JCIdent) binary.rhs).sym) {
            if (isConstantOne(binary.lhs)) {
              return (IdentifierTree) binary.rhs;
            }
          }
        }
      }
    }
  }
  return null;
}
项目:Refaster    文件:ExpressionTemplate.java   
/**
 * Returns the precedence level appropriate for unambiguously printing
 * leaf as a subexpression of its parent.
 */
private static int getPrecedence(JCTree leaf, Context context) {
  JCCompilationUnit comp = context.get(JCCompilationUnit.class);
  JCTree parent = TreeInfo.pathFor(leaf, comp).get(1);

  // In general, this should match the logic in com.sun.tools.javac.tree.Pretty.
  //
  // TODO(mdempsky): There are probably cases where we could omit parentheses
  // by tweaking the returned precedence, but they need careful review.
  // For example, consider a template to replace "add(a, b)" with "a + b",
  // which applied to "x + add(y, z)" would result in "x + (y + z)".
  // In most cases, we'd likely prefer "x + y + z" instead, but those aren't
  // always equivalent: "0L + (Integer.MIN_VALUE + Integer.MIN_VALUE)" yields
  // a different value than "0L + Integer.MIN_VALUE + Integer.MIN_VALUE" due
  // to integer promotion rules.

  if (parent instanceof JCConditional) {
    // This intentionally differs from Pretty, because Pretty appears buggy:
    // http://mail.openjdk.java.net/pipermail/compiler-dev/2013-September/007303.html
    JCConditional conditional = (JCConditional) parent;
    return TreeInfo.condPrec + ((conditional.cond == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssign) {
    JCAssign assign = (JCAssign) parent;
    return TreeInfo.assignPrec + ((assign.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCAssignOp) {
    JCAssignOp assignOp = (JCAssignOp) parent;
    return TreeInfo.assignopPrec + ((assignOp.lhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCUnary) {
    return TreeInfo.opPrec(parent.getTag());
  } else if (parent instanceof JCBinary) {
    JCBinary binary = (JCBinary) parent;
    return TreeInfo.opPrec(parent.getTag()) + ((binary.rhs == leaf) ? 1 : 0);
  } else if (parent instanceof JCTypeCast) {
    JCTypeCast typeCast = (JCTypeCast) parent;
    return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCInstanceOf) {
    JCInstanceOf instanceOf = (JCInstanceOf) parent;
    return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0);
  } else if (parent instanceof JCArrayAccess) {
    JCArrayAccess arrayAccess = (JCArrayAccess) parent;
    return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else if (parent instanceof JCFieldAccess) {
    JCFieldAccess fieldAccess = (JCFieldAccess) parent;
    return (fieldAccess.selected == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec;
  } else {
    return TreeInfo.noPrec;
  }
}