@Override public boolean matches(UnaryTree tree, VisitorState state) { return exprMatcher.matches(tree.getExpression(), state); } };
@Override public Boolean visitUnary(UnaryTree node, Void aVoid) { return node.getExpression().accept(this, null); }
@Override public Description matchUnary(UnaryTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } return doUnboxingCheck(state, tree.getExpression()); }
@Override public Description matchMethodInvocation( MethodInvocationTree methodInvocationTree, VisitorState visitorState) { if (methodInvocationTree.getArguments().stream() .filter(arg -> UNARY_OPERATORS.contains(arg.getKind())) .map(arg -> ASTHelpers.getSymbol(((UnaryTree) arg).getExpression())) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet() .stream() .anyMatch(e -> e.getValue() > 1)) { return describeMatch(methodInvocationTree); } return Description.NO_MATCH; } }
private static Optional<Symbol> returnUnarySym(StatementTree s) { if (s instanceof ExpressionStatementTree) { if (((ExpressionStatementTree) s).getExpression() instanceof UnaryTree) { UnaryTree unaryTree = (UnaryTree) ((ExpressionStatementTree) s).getExpression(); return Optional.ofNullable(ASTHelpers.getSymbol(unaryTree.getExpression())); } } return Optional.empty(); } }
@Override public Number visitUnary(UnaryTree node, Void p) { Number value = node.getExpression().accept(this, null); if (value == null) { return value; } if (value instanceof Long) { return unop(node.getKind(), value.longValue()); } else { return unop(node.getKind(), value.intValue()); } }
@Override public JCExpression visitUnary(UnaryTree tree, Void v) { if (tree.getKind() == Kind.LOGICAL_COMPLEMENT) { return (JCExpression) tree.getExpression(); } else { return defaultNegation(tree); } }
@Override public Boolean visitUnary(UnaryTree node, Void aVoid) { Boolean r = node.getExpression().accept(this, null); if (r == null) { return null; } switch (node.getKind()) { case LOGICAL_COMPLEMENT: return !r; default: return null; } } };
@Override public Void visitUnary(UnaryTree node, InitializationContext init) { if (UNARY_ASSIGNMENT.contains(node.getKind())) { init = InitializationContext.NONE; writes.recordAssignment(node.getExpression(), init); } return super.visitUnary(node, init); } }
@Override public Void visitUnary(UnaryTree tree, Void unused) { switch (tree.getKind()) { case POSTFIX_INCREMENT: case PREFIX_INCREMENT: case POSTFIX_DECREMENT: case PREFIX_DECREMENT: check(tree.getExpression()); break; default: // fall out } return super.visitUnary(tree, unused); }
@Override public UUnary visitUnary(UnaryTree tree, Void v) { return UUnary.create(tree.getKind(), template(tree.getExpression())); }
@Override public Void visitUnary(UnaryTree node, Void unused) { switch (node.getKind()) { case PREFIX_DECREMENT: case PREFIX_INCREMENT: case POSTFIX_DECREMENT: case POSTFIX_INCREMENT: recordWrite(node.getExpression()); break; default: break; } return super.visitUnary(node, null); }
@Override public Description matchForLoop(ForLoopTree forLoopTree, VisitorState visitorState) { List<? extends ExpressionStatementTree> updates = forLoopTree.getUpdate(); // keep track of all the symbols that are updated in the for loop header final Set<Symbol> incrementedSymbols = updates.stream() .filter(expStateTree -> expStateTree.getExpression() instanceof UnaryTree) .map( expStateTree -> ASTHelpers.getSymbol( ((UnaryTree) expStateTree.getExpression()).getExpression())) .collect(Collectors.toCollection(HashSet::new)); // track if they are updated in the body without a conditional surrounding them StatementTree body = forLoopTree.getStatement(); List<? extends StatementTree> statementTrees = body instanceof BlockTree ? ((BlockTree) body).getStatements() : ImmutableList.of(body); for (StatementTree s : statementTrees) { if (!CONDITIONALS.contains(s.getKind())) { Optional<Symbol> opSymbol = returnUnarySym(s); if (opSymbol.isPresent() && incrementedSymbols.contains(opSymbol.get())) { // both ++ and -- return describeMatch(forLoopTree); } } } return Description.NO_MATCH; }
@Override @Nullable public Choice<Unifier> visitUnary(UnaryTree unary, @Nullable Unifier unifier) { return Choice.condition(getKind().equals(unary.getKind()), unifier) .thenChoose( unifications(getExpression(), ASTHelpers.stripParentheses(unary.getExpression()))); }
@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)); }
@Override public Description matchUnary(UnaryTree tree, VisitorState state) { switch (tree.getKind()) { case POSTFIX_INCREMENT: case POSTFIX_DECREMENT: break; default: return NO_MATCH; } Tree parent = state.getPath().getParentPath().getLeaf(); if (parent.getKind() != Kind.LAMBDA_EXPRESSION) { return NO_MATCH; } JCLambda lambda = (JCLambda) parent; Symbol sym = ASTHelpers.getSymbol(tree.getExpression()); if (lambda.getParameters().stream().noneMatch(p -> ASTHelpers.getSymbol(p) == sym)) { return NO_MATCH; } return describeMatch( tree, SuggestedFix.replace( tree, String.format( "%s %s 1", state.getSourceForNode(tree.getExpression()), tree.getKind() == Kind.POSTFIX_INCREMENT ? "+" : "-"))); } }
@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; }
@Override @Nullable public Choice<Unifier> visitUnary(UnaryTree unary, @Nullable Unifier unifier) { return Choice.condition(getKind().equals(unary.getKind()), unifier) .thenChoose( unifications(getExpression(), ASTHelpers.stripParentheses(unary.getExpression()))); }
@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; }
@Override public JExpression visitUnary(UnaryTree ut, Void __) { return $b.invoke(opName(ut.getKind())) .arg(loc(ut)) .arg(visit(ut.getExpression())); }