com.sun.source.tree.UnaryTree Java Examples

The following examples show how to use com.sun.source.tree.UnaryTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
private boolean ambiguousUnaryOperator(UnaryTree node, String operatorName) {
  switch (node.getKind()) {
    case UNARY_MINUS:
    case UNARY_PLUS:
      break;
    default:
      return false;
  }
  if (!(node.getExpression() instanceof UnaryTree)) {
    return false;
  }
  JCTree.Tag tag = ((JCTree) node.getExpression()).getTag();
  if (tag.isPostUnaryOp()) {
    return false;
  }
  if (!operatorName(node).startsWith(operatorName)) {
    return false;
  }
  return true;
}
 
Example #2
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static List<? extends TypeMirror> computeUnary(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    UnaryTree tree = (UnaryTree) parent.getLeaf();
    
    if (tree.getExpression() == error) {
        List<? extends TypeMirror> parentTypes = resolveType(types, info, parent.getParentPath(), tree, offset, null, null);
        
        if (parentTypes != null) {
            //may contain only "void", ignore:
            if (parentTypes.size() != 1) {
                return parentTypes;
            }
            if (parentTypes.get(0).getKind() != TypeKind.VOID) {
                return parentTypes;
            }
        }
        
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);

        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
    }
    
    return null;
}
 
Example #3
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Void visitUnary(UnaryTree node, Void unused) {
    sync(node);
    String operatorName = operatorName(node);
    if (OpUtil.isPostUnaryOp(((JCTree) node).getTag())) {
        scan(node.getExpression(), null);
        splitToken(operatorName);
    } else {
        splitToken(operatorName);
        if (ambiguousUnaryOperator(node, operatorName)) {
            builder.space();
        }
        scan(node.getExpression(), null);
    }
    return null;
}
 
Example #4
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private boolean ambiguousUnaryOperator(UnaryTree node, String operatorName) {
    switch (node.getKind()) {
        case UNARY_MINUS:
        case UNARY_PLUS:
            break;
        default:
            return false;
    }
    if (!(node.getExpression() instanceof UnaryTree)) {
        return false;
    }
    int tag = ((JCTree) node.getExpression()).getTag();
    if (OpUtil.isPostUnaryOp(tag)) {
        return false;
    }
    if (!operatorName(node).startsWith(operatorName)) {
        return false;
    }
    return true;
}
 
Example #5
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitUnary(UnaryTree node, Void unused) {
  sync(node);
  String operatorName = operatorName(node);
  if (((JCTree) node).getTag().isPostUnaryOp()) {
    scan(node.getExpression(), null);
    splitToken(operatorName);
  } else {
    splitToken(operatorName);
    if (ambiguousUnaryOperator(node, operatorName)) {
      builder.space();
    }
    scan(node.getExpression(), null);
  }
  return null;
}
 
Example #6
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static List<? extends TypeMirror> computeUnary(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    UnaryTree tree = (UnaryTree) parent.getLeaf();
    
    if (tree.getExpression() == error) {
        List<? extends TypeMirror> parentTypes = resolveType(types, info, parent.getParentPath(), tree, offset, null, null);
        
        if (parentTypes != null) {
            //may contain only "void", ignore:
            if (parentTypes.size() != 1) {
                return parentTypes;
            }
            if (parentTypes.get(0).getKind() != TypeKind.VOID) {
                return parentTypes;
            }
        }
        
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);

        return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
    }
    
    return null;
}
 
Example #7
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 6 votes vote down vote up
/** Returns true if {@code atLeastM} of the expressions in the given column are the same kind. */
private static boolean expressionsAreParallel(
    List<List<ExpressionTree>> rows, int column, int atLeastM) {
  Multiset<Tree.Kind> nodeTypes = HashMultiset.create();
  for (List<? extends ExpressionTree> row : rows) {
    if (column >= row.size()) {
      continue;
    }
    // Treat UnaryTree expressions as their underlying type for the comparison (so, for example
    // -ve and +ve numeric literals are considered the same).
    if (row.get(column) instanceof UnaryTree) {
      nodeTypes.add(((UnaryTree) row.get(column)).getExpression().getKind());
    } else {
      nodeTypes.add(row.get(column).getKind());
    }
  }
  for (Multiset.Entry<Tree.Kind> nodeType : nodeTypes.entrySet()) {
    if (nodeType.getCount() >= atLeastM) {
      return true;
    }
  }
  return false;
}
 
Example #8
Source File: ModelBuilder.java    From vertx-codetrans with Apache License 2.0 6 votes vote down vote up
@Override
public ExpressionModel visitUnary(UnaryTree node, VisitContext p) {
  ExpressionModel expression = scan(node.getExpression(), p);
  switch (node.getKind()) {
    case POSTFIX_INCREMENT:
      // Note we don't handle the case (3++) that is not legal in JavaScript
      return expression.onPostFixIncrement();
    case POSTFIX_DECREMENT:
      // Note we don't handle the case (3--) that is not legal in JavaScript
      return expression.onPostFixDecrement();
    case PREFIX_INCREMENT:
      // Note we don't handle the case (++3) that is not legal in JavaScript
      return expression.onPrefixIncrement();
    case PREFIX_DECREMENT:
      // Note we don't handle the case (--3) that is not legal in JavaScript
      return expression.onPrefixDecrement();
    case LOGICAL_COMPLEMENT:
      return expression.onLogicalComplement();
    case UNARY_MINUS:
      return expression.unaryMinus();
    case UNARY_PLUS:
      return expression.unaryPlus();
    default:
      throw new UnsupportedOperationException("Unary operator " + node.getKind().name() + " not yet implemented");
  }
}
 
Example #9
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitUnary(UnaryTree node, Void unused) {
    sync(node);
    String operatorName = operatorName(node);
    if (OpUtil.isPostUnaryOp(((JCTree) node).getTag())) {
        scan(node.getExpression(), null);
        splitToken(operatorName);
    } else {
        splitToken(operatorName);
        if (ambiguousUnaryOperator(node, operatorName)) {
            builder.space();
        }
        scan(node.getExpression(), null);
    }
    return null;
}
 
Example #10
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
private boolean ambiguousUnaryOperator(UnaryTree node, String operatorName) {
    switch (node.getKind()) {
        case UNARY_MINUS:
        case UNARY_PLUS:
            break;
        default:
            return false;
    }
    if (!(node.getExpression() instanceof UnaryTree)) {
        return false;
    }
    int tag = ((JCTree) node.getExpression()).getTag();
    if (OpUtil.isPostUnaryOp(tag)) {
        return false;
    }
    if (!operatorName(node).startsWith(operatorName)) {
        return false;
    }
    return true;
}
 
Example #11
Source File: TreeConverter.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private TreeNode convertPrefixExpr(UnaryTree node, TreePath parent) {
  TreePath path = getTreePath(parent, node);
  return new PrefixExpression()
      .setTypeMirror(getTypeMirror(path))
      .setOperator(PrefixExpression.Operator.from(node.getKind()))
      .setOperand((Expression) convert(node.getExpression(), path));
}
 
Example #12
Source File: ModelBuilder.java    From vertx-codetrans with Apache License 2.0 5 votes vote down vote up
@Override
public CodeModel visitForLoop(ForLoopTree node, VisitContext context) {
  if (node.getInitializer().size() != 1) {
    throw new UnsupportedOperationException();
  }
  if (node.getUpdate().size() != 1) {
    throw new UnsupportedOperationException();
  }
  StatementModel body = scan(node.getStatement(), context);
  if (node.getInitializer().size() == 1 &&
      node.getInitializer().get(0).getKind() == Tree.Kind.VARIABLE &&
      node.getCondition().getKind() == Tree.Kind.LESS_THAN &&
      node.getUpdate().size() == 1 &&
      node.getUpdate().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT &&
      node.getUpdate().get(0).getExpression().getKind() == Tree.Kind.POSTFIX_INCREMENT) {
    VariableTree init = (VariableTree) node.getInitializer().get(0);
    BinaryTree lessThan = (BinaryTree) node.getCondition();
    UnaryTree increment = (UnaryTree) node.getUpdate().get(0).getExpression();
    if (lessThan.getLeftOperand().getKind() == Tree.Kind.IDENTIFIER &&
        increment.getExpression().getKind() == Tree.Kind.IDENTIFIER) {
      String id1 = init.getName().toString();
      String id2 = ((IdentifierTree) lessThan.getLeftOperand()).getName().toString();
      String id3 = ((IdentifierTree) increment.getExpression()).getName().toString();
      if (id1.equals(id2) && id2.equals(id3)) {
        ExpressionModel from = scan(init.getInitializer(), context);
        ExpressionModel to = scan(lessThan.getRightOperand(), context);
        return context.builder.sequenceForLoop(id1, from, to, body);
      }
    }
  }
  StatementModel initializer = scan(node.getInitializer().get(0), context);
  ExpressionModel update = scan(node.getUpdate().get(0).getExpression(), context);
  ExpressionModel condition = scan(node.getCondition(), context);
  return context.builder.forLoop(initializer, condition, update, body);
}
 
Example #13
Source File: TreeDiffer.java    From compile-testing with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitUnary(UnaryTree expected, Tree actual) {
  Optional<UnaryTree> other = checkTypeAndCast(expected, actual);
  if (!other.isPresent()) {
    addTypeMismatch(expected, actual);
    return null;
  }

  scan(expected.getExpression(), other.get().getExpression());
  return null;
}
 
Example #14
Source File: TreePruner.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitUnary(UnaryTree node, Void p) {
  switch (node.getKind()) {
    case UNARY_PLUS:
    case UNARY_MINUS:
    case BITWISE_COMPLEMENT:
    case LOGICAL_COMPLEMENT:
      break;
    default:
      // non-constant unary expression
      return false;
  }
  return node.getExpression().accept(this, null);
}
 
Example #15
Source File: NullAway.java    From NullAway with MIT License 5 votes vote down vote up
@Override
public Description matchUnary(UnaryTree tree, VisitorState state) {
  if (!matchWithinClass) {
    return Description.NO_MATCH;
  }
  return doUnboxingCheck(state, tree.getExpression());
}
 
Example #16
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<? extends TypeMirror> visitUnary(UnaryTree node, Object p) {
    switch (node.getKind()) {
        case POSTFIX_DECREMENT: case POSTFIX_INCREMENT: case PREFIX_DECREMENT: case PREFIX_INCREMENT:
            // the incremented value is a l-value, it's type cannot be changed. We shouldn't be at this code path at all
            return null;
        case PLUS:
        case BITWISE_COMPLEMENT:
            scanParent();
            break;
        case LOGICAL_COMPLEMENT:
            return booleanType();
    }
    return null;
}
 
Example #17
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitUnary(UnaryTree node, List<TreePath> p) {
    switch (node.getKind()) {
        case PREFIX_INCREMENT:
        case PREFIX_DECREMENT:
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
            if (param == trees.getElement(TreePath.getPath(getCurrentPath(), node.getExpression()))) {
                p.add(getCurrentPath());
                return null;
            }
    }
    return super.visitUnary(node, p);
}
 
Example #18
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToMethodParam", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToMethodParam", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToMethodParameter", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.ASSIGNMENT, Kind.AND_ASSIGNMENT, Kind.DIVIDE_ASSIGNMENT,
    Kind.LEFT_SHIFT_ASSIGNMENT, Kind.MINUS_ASSIGNMENT, Kind.MULTIPLY_ASSIGNMENT,
    Kind.OR_ASSIGNMENT, Kind.PLUS_ASSIGNMENT, Kind.REMAINDER_ASSIGNMENT, Kind.RIGHT_SHIFT_ASSIGNMENT,
    Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT, Kind.XOR_ASSIGNMENT, Kind.PREFIX_INCREMENT,
    Kind.PREFIX_DECREMENT, Kind.POSTFIX_INCREMENT, Kind.POSTFIX_DECREMENT})
public static ErrorDescription assignmentToMethodParam(HintContext context) {
    final TreePath path = context.getPath();
    Element element = null;
    switch (path.getLeaf().getKind()) {
        case ASSIGNMENT:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((AssignmentTree) path.getLeaf()).getVariable()));
            break;
        case PREFIX_INCREMENT:
        case PREFIX_DECREMENT:
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((UnaryTree) path.getLeaf()).getExpression()));
            break;
        default:
            element = context.getInfo().getTrees().getElement(TreePath.getPath(path, ((CompoundAssignmentTree) path.getLeaf()).getVariable()));
    }
    if (element != null && element.getKind() == ElementKind.PARAMETER) {
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToMethodParam", element.getSimpleName())); //NOI18N
    }
    return null;
}
 
Example #19
Source File: SideEffectVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitUnary(UnaryTree node, Object p) {
    switch (node.getKind()) {
        case POSTFIX_DECREMENT:
        case POSTFIX_INCREMENT:
        case PREFIX_DECREMENT:
        case PREFIX_INCREMENT:
            break;
        default:
            return super.visitUnary(node, p);
    }
    checkVariableAccess(node.getExpression(), node);
    return super.visitUnary(node, p);
}
 
Example #20
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 5 votes vote down vote up
@Override
public Description matchUnary(UnaryTree tree, VisitorState state) {
  if (overLaps(tree, state)) {
    return Description.NO_MATCH;
  }
  Value x = evalExpr(tree, state);
  return updateCode(x, tree, tree, state);
}
 
Example #21
Source File: TreeDuplicator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Tree visitUnary(UnaryTree tree, Void p) {
    UnaryTree n = make.Unary(tree.getKind(), tree.getExpression());
    model.setType(n, model.getType(tree));
    comments.copyComments(tree, n);
    model.setPos(n, model.getPos(tree));
    return n;
}
 
Example #22
Source File: CopyFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Boolean visitUnary(UnaryTree node, TreePath p) {
    if (p == null)
        return super.visitUnary(node, p);

    UnaryTree t = (UnaryTree) p.getLeaf();

    return scan(node.getExpression(), t.getExpression(), p);
}
 
Example #23
Source File: JavaFixUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Number visitUnary(UnaryTree node, Void p) {
    Number op  = scan(node.getExpression(), p);

    if (op != null) {
        Number result = null;
        switch (node.getKind()) {
            case UNARY_MINUS:
                    if (op instanceof Double) {
                        result = -op.doubleValue();
                    } else if (op instanceof Float) {
                        result = -op.floatValue();
                    } else if (op instanceof Long) {
                        result = -op.longValue();
                    } else if (op instanceof Integer) {
                        result = -op.intValue();
                    } else {
                        throw new IllegalStateException("op=" + op.getClass());
                    }
                    break;
            case UNARY_PLUS:
                result = op;
                break;
        }

        if (result != null) {
            rewrite(node, make.Literal(result));

            return result;
        }
    }

    return super.visitUnary(node, p);
}
 
Example #24
Source File: MethodMetrics.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitUnary(UnaryTree node, Object p) {
    if (node.getKind() == Tree.Kind.LOGICAL_COMPLEMENT) {
        negationsCount++;
    }
    return super.visitUnary(node, p);
}
 
Example #25
Source File: ExpressionVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitUnary(UnaryTree node, Object p) {
    boolean b = isCorrectType(node);
    currentMatches = b;
    Object o = super.visitUnary(node, p);
    increment(node, b);
    return o;
}
 
Example #26
Source File: TreeNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitUnary(UnaryTree tree, List<Node> d) {
    List<Node> below = new ArrayList<Node>();
    
    addCorrespondingType(below);
    addCorrespondingComments(below);
    super.visitUnary(tree, below);
    
    d.add(new TreeNode(info, getCurrentPath(), below));
    return null;
}
 
Example #27
Source File: ProspectiveOperation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static ProspectiveOperation handlePreOrPostFixReducer(ExpressionTree expr, WorkingCopy workingCopy, TreeMaker tm, OperationType operationType, PreconditionsChecker precond, List<ProspectiveOperation> ls, ProspectiveOperation redOp) {
    ExpressionTree reducing = ((UnaryTree) expr).getExpression();
    ProspectiveOperation map;
    if (isInteger(reducing, workingCopy) || isLong(reducing, workingCopy) || isChar(reducing, workingCopy)) {
        map = new ProspectiveOperation(tm.Literal(1), operationType.MAP, precond.getInnerVariables(), workingCopy, precond.getVarToName());
    } else {
        map = new ProspectiveOperation(tm.Literal(1.), operationType.MAP, precond.getInnerVariables(), workingCopy, precond.getVarToName());
    }
    ls.add(map);
    redOp = new ProspectiveOperation(expr, operationType, precond.getInnerVariables(), workingCopy, precond.getVarToName());
    redOp.reducingVariable = reducing;
    return redOp;
}
 
Example #28
Source File: ReplaceBufferByString.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private ExpressionTree makeParenthesis(ExpressionTree arg) {
    Class c =  arg.getKind().asInterface();
    // if the original append argument was an expression, surround it in parenthesis, to get the same toString effect
    if (c == BinaryTree.class || c == UnaryTree.class || c == CompoundAssignmentTree.class || c == AssignmentTree.class ||
        c == ConditionalExpressionTree.class) {
        return mk.Parenthesized(arg);
    } else {
        return arg;
    }
}
 
Example #29
Source File: RemoveUnnecessary.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitUnary(UnaryTree node, Object p) {
    scan(node.getExpression(), p);
    return null;
}
 
Example #30
Source File: ArithmeticUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public Object visitUnary(UnaryTree node, Void p) {
    Object op  = scan(node.getExpression(), p);
    if (op != null) {
        Object result = null;
        if (op instanceof Character) {
            op = Integer.valueOf(((Character)op).charValue());
        }
        switch (node.getKind()) {
            case BITWISE_COMPLEMENT:
                if (op instanceof Long) {
                    result = ~((Long)op).longValue();
                } else if (op instanceof Number && integerLike((Number)op)) {
                    result = ~(((Number)op).intValue());
                }
                break;
            case LOGICAL_COMPLEMENT:
                if (op instanceof Boolean) {
                    result = !((Boolean)op).booleanValue();
                }
                break;
            case UNARY_MINUS:
                if (op instanceof Number) {
                    Number nop = (Number)op;
                    if (op instanceof Double) {
                        result = -nop.doubleValue();
                    } else if (op instanceof Float) {
                        result = -nop.floatValue();
                    } else if (op instanceof Long) {
                        result = -nop.longValue();
                    } else if (integerLike(nop)) {
                        result = -nop.intValue();
                    } else {
                        return null;
                    }
                }
                break;
            case UNARY_PLUS:
                if (op instanceof Number) {
                    result = op;
                }
                break;
        }
        return result;
    }

    return super.visitUnary(node, p);
}