Java Code Examples for com.sun.source.tree.BinaryTree#getLeftOperand()
The following examples show how to use
com.sun.source.tree.BinaryTree#getLeftOperand() .
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: UnnecessaryBoxing.java From netbeans with Apache License 2.0 | 6 votes |
private static boolean checkBinaryOp(CompilationInfo ci, TreePath expr, Tree prev) { BinaryTree bt = (BinaryTree)expr.getLeaf(); Tree other = prev == bt.getLeftOperand() ? bt.getRightOperand() : bt.getLeftOperand(); Boolean b = checkTwoArguments(ci, expr, other, prev); if (Boolean.TRUE == b) { return true; } if (b == null) { return false; } TypeMirror tm = ci.getTrees().getTypeMirror(new TreePath(expr, other)); if (tm != null && tm.getKind() == TypeKind.DECLARED) { Element el = ((DeclaredType)tm).asElement(); if (el != null && el.getKind() == ElementKind.CLASS) { return ((TypeElement)el).getQualifiedName().contentEquals("java.lang.String"); // NOI18N } } return false; }
Example 2
Source File: ConvertToTextBlock.java From netbeans with Apache License 2.0 | 6 votes |
private static String getTextOrNull(TreePath tp) { StringBuilder text = new StringBuilder(); Tree current = tp.getLeaf(); while (current.getKind() == Kind.PLUS) { BinaryTree bt = (BinaryTree) current; if (bt.getRightOperand().getKind() == Kind.STRING_LITERAL) { text.insert(0, ((LiteralTree) bt.getRightOperand()).getValue()); } else { return null; } current = bt.getLeftOperand(); } if (current.getKind() == Kind.STRING_LITERAL) { text.insert(0, ((LiteralTree) current).getValue()); } else { return null; } String textString = text.toString(); if (!textString.contains("\n")) { return null; } return textString; }
Example 3
Source File: NullAway.java From NullAway with MIT License | 6 votes |
@Override public Description matchBinary(BinaryTree tree, VisitorState state) { if (!matchWithinClass) { return Description.NO_MATCH; } ExpressionTree leftOperand = tree.getLeftOperand(); ExpressionTree rightOperand = tree.getRightOperand(); Type leftType = ASTHelpers.getType(leftOperand); Type rightType = ASTHelpers.getType(rightOperand); if (leftType == null || rightType == null) { throw new RuntimeException(); } if (leftType.isPrimitive() && !rightType.isPrimitive()) { return doUnboxingCheck(state, rightOperand); } if (rightType.isPrimitive() && !leftType.isPrimitive()) { return doUnboxingCheck(state, leftOperand); } return Description.NO_MATCH; }
Example 4
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
private static ExpressionTree negateBinaryOperator(TreeMaker make, Tree original, Kind newKind, boolean negateOperands) { BinaryTree bt = (BinaryTree) original; ExpressionTree left = bt.getLeftOperand(); ExpressionTree right = bt.getRightOperand(); if (negateOperands) { left = negate(make, left, original); right = negate(make, right, original); } return make.Binary(newKind, left, right); }
Example 5
Source File: NullAway.java From NullAway with MIT License | 5 votes |
/** * @param path tree path to read operation * @return true if it is permissible to perform this read before the field has been initialized, * false otherwise */ private boolean okToReadBeforeInitialized(TreePath path) { TreePath parentPath = path.getParentPath(); Tree leaf = path.getLeaf(); Tree parent = parentPath.getLeaf(); if (parent instanceof AssignmentTree) { // ok if it's actually a write AssignmentTree assignment = (AssignmentTree) parent; return assignment.getVariable().equals(leaf); } else if (parent instanceof BinaryTree) { // ok if we're comparing to null BinaryTree binaryTree = (BinaryTree) parent; Tree.Kind kind = binaryTree.getKind(); if (kind.equals(Tree.Kind.EQUAL_TO) || kind.equals(Tree.Kind.NOT_EQUAL_TO)) { ExpressionTree left = binaryTree.getLeftOperand(); ExpressionTree right = binaryTree.getRightOperand(); return (left.equals(leaf) && right.getKind().equals(Tree.Kind.NULL_LITERAL)) || (right.equals(leaf) && left.getKind().equals(Tree.Kind.NULL_LITERAL)); } } else if (parent instanceof MethodInvocationTree) { // ok if it's invoking castToNonNull and the read is the argument MethodInvocationTree methodInvoke = (MethodInvocationTree) parent; Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodInvoke); String qualifiedName = ASTHelpers.enclosingClass(methodSymbol) + "." + methodSymbol.getSimpleName().toString(); if (qualifiedName.equals(config.getCastToNonNullMethod())) { List<? extends ExpressionTree> arguments = methodInvoke.getArguments(); return arguments.size() == 1 && leaf.equals(arguments.get(0)); } } return false; }
Example 6
Source File: XPFlagCleaner.java From piranha with Apache License 2.0 | 4 votes |
@SuppressWarnings("TreeToString") @Override public Description matchBinary(BinaryTree tree, VisitorState state) { if (overLaps(tree, state)) { return Description.NO_MATCH; } Value x = evalExpr(tree, state); Description d = updateCode(x, tree, tree, state); if (!d.equals(Description.NO_MATCH)) { return d; } ExpressionTree deletedSubTree = null; ExpressionTree remainingSubTree = null; Value l = evalExpr(tree.getLeftOperand(), state); Value r = evalExpr(tree.getRightOperand(), state); if (tree.getKind().equals(Kind.CONDITIONAL_AND)) { if (l.equals(Value.TRUE)) { deletedSubTree = tree.getLeftOperand(); remainingSubTree = tree.getRightOperand(); } else if (r.equals(Value.TRUE)) { deletedSubTree = tree.getRightOperand(); remainingSubTree = tree.getLeftOperand(); } } else if (tree.getKind().equals(Kind.CONDITIONAL_OR)) { if (l.equals(Value.FALSE)) { deletedSubTree = tree.getLeftOperand(); remainingSubTree = tree.getRightOperand(); } else if (r.equals(Value.FALSE)) { deletedSubTree = tree.getRightOperand(); remainingSubTree = tree.getLeftOperand(); } } if (deletedSubTree != null) { Preconditions.checkNotNull( remainingSubTree, "deletedSubTree != null => remainingSubTree !=null here."); Description.Builder builder = buildDescription(tree); SuggestedFix.Builder fixBuilder = SuggestedFix.builder(); fixBuilder.replace(tree, remainingSubTree.toString()); decrementAllSymbolUsages(deletedSubTree, state, fixBuilder); builder.addFix(fixBuilder.build()); endPos = state.getEndPosition(tree); return builder.build(); } return Description.NO_MATCH; }
Example 7
Source File: RemoveUnnecessary.java From netbeans with Apache License 2.0 | 4 votes |
public Object transformLogAndOr(BinaryTree node, Object p) { List<StatementTree> saveStats = this.statements; boolean saveRemove = this.remove; this.remove = true; this.statements = new ArrayList<>(); scan(node.getRightOperand(), p); if (remove) { // the if statement would be empty; attempt to transform the // left operand if it has at least something. Omit the left operand // at all. this.statements = saveStats; scan(node.getLeftOperand(), p); if (remove) { this.remove &= saveRemove; return null; } this.remove &= saveRemove; } else { List<StatementTree> elseStats = this.statements; this.statements = saveStats; remove = false; if (mk != null) { ExpressionTree condition; if (node.getKind() == Tree.Kind.CONDITIONAL_AND) { condition = node.getLeftOperand(); } else { condition = Utilities.negate(mk, node.getLeftOperand(), node); } statements.add( mk.If(condition, elseStats.size() == 1 ? elseStats.get(0) : mk.Block(elseStats, false), null ) ); } } return null; }
Example 8
Source File: WrongStringComparison.java From netbeans with Apache License 2.0 | 4 votes |
@TriggerPatterns({ @TriggerPattern(value="$left == $right", constraints={@ConstraintVariableType(variable="$left", type=STRING_TYPE), @ConstraintVariableType(variable="$right", type=STRING_TYPE)}), @TriggerPattern(value="$left != $right", constraints={@ConstraintVariableType(variable="$left", type=STRING_TYPE), @ConstraintVariableType(variable="$right", type=STRING_TYPE)}) }) public static ErrorDescription run(HintContext ctx) { CompilationInfo info = ctx.getInfo(); TreePath treePath = ctx.getPath(); Tree t = treePath.getLeaf(); BinaryTree bt = (BinaryTree) t; TreePath left = new TreePath(treePath, bt.getLeftOperand() ); TreePath right = new TreePath(treePath, bt.getRightOperand() ); Trees trees = info.getTrees(); TypeMirror leftType = left == null ? null : trees.getTypeMirror(left); TypeMirror rightType = right == null ? null : trees.getTypeMirror(right); if ( leftType != null && rightType != null && STRING_TYPE.equals(leftType.toString()) && STRING_TYPE.equals(rightType.toString())) { if (checkInsideGeneratedEquals(ctx, treePath, left.getLeaf(), right.getLeaf())) { return null; } FileObject file = info.getFileObject(); TreePathHandle tph = TreePathHandle.create(treePath, info); ArrayList<Fix> fixes = new ArrayList<Fix>(); boolean reverseOperands = false; if (bt.getLeftOperand().getKind() != Tree.Kind.STRING_LITERAL) { if (bt.getRightOperand().getKind() == Tree.Kind.STRING_LITERAL) { if (getStringLiteralsFirst(ctx.getPreferences())) { reverseOperands = true; } else { fixes.add(new WrongStringComparisonFix(tph, WrongStringComparisonFix.Kind.NULL_CHECK).toEditorFix()); } } else { fixes.add(new WrongStringComparisonFix(tph, WrongStringComparisonFix.Kind.ternaryNullCheck(getTernaryNullCheck(ctx.getPreferences()))).toEditorFix()); } } fixes.add(new WrongStringComparisonFix(tph, WrongStringComparisonFix.Kind.reverseOperands(reverseOperands)).toEditorFix()); return ErrorDescriptionFactory.forTree( ctx, t, NbBundle.getMessage(WrongStringComparison.class, "LBL_WrongStringComparison"), fixes.toArray(new Fix[0])); } return null; }
Example 9
Source File: WrongStringComparison.java From netbeans with Apache License 2.0 | 4 votes |
@Override protected void performRewrite(TransformationContext ctx) { WorkingCopy copy = ctx.getWorkingCopy(); TreePath path = ctx.getPath(); if (path != null) { TreeMaker make = copy.getTreeMaker(); BinaryTree oldTree = (BinaryTree) path.getLeaf(); ExpressionTree left = oldTree.getLeftOperand(); ExpressionTree right = oldTree.getRightOperand(); ExpressionTree newTree; if (kind == Kind.REVERSE_OPERANDS) { // "str2".equals(str1) ExpressionTree rightEquals = make.MemberSelect(right, "equals"); // NOI18N ExpressionTree rightEqualsLeft = make.MethodInvocation(Collections.<ExpressionTree>emptyList(), rightEquals, Collections.singletonList(left)); rightEqualsLeft = matchSign(make, oldTree, rightEqualsLeft); newTree = rightEqualsLeft; } else { ExpressionTree leftEquals = make.MemberSelect(left, "equals"); // NOI18N ExpressionTree leftEqualsRight = make.MethodInvocation(Collections.<ExpressionTree>emptyList(), leftEquals, Collections.singletonList(right)); leftEqualsRight = matchSign(make, oldTree, leftEqualsRight); if (kind == Kind.NO_NULL_CHECK) { // str1.equals(str2) newTree = leftEqualsRight; } else { ExpressionTree leftEqNull = make.Binary(Tree.Kind.EQUAL_TO, left, make.Identifier("null")); // NOI18N ExpressionTree rightEqNull = make.Binary(oldTree.getKind(), right, make.Identifier("null")); // NOI18N if (kind == Kind.NULL_CHECK_TERNARY) { // str1 == null ? str2 == null : str1.equals(str2) newTree = make.ConditionalExpression(leftEqNull, rightEqNull, leftEqualsRight); } else { ExpressionTree leftNeNull = make.Binary(Tree.Kind.NOT_EQUAL_TO, left, make.Identifier("null")); // NOI18N ExpressionTree leftNeNullAndLeftEqualsRight = make.Binary(Tree.Kind.CONDITIONAL_AND, leftNeNull, leftEqualsRight); if (right.getKind() == Tree.Kind.STRING_LITERAL) { // str1 != null && str1.equals("str2") newTree = leftNeNullAndLeftEqualsRight; } else { // (str1 == null && str2 == null) || (str1 != null && str1.equals(str2)) ExpressionTree leftEqNullAndRightEqNull = make.Binary(Tree.Kind.CONDITIONAL_AND, leftEqNull, rightEqNull); newTree = make.Binary(Tree.Kind.CONDITIONAL_OR, make.Parenthesized(leftEqNullAndRightEqNull), make.Parenthesized(leftNeNullAndLeftEqualsRight)); } } if (path.getParentPath().getLeaf().getKind() != Tree.Kind.PARENTHESIZED) { newTree = make.Parenthesized(newTree); } } } copy.rewrite(oldTree, newTree); } }