Java Code Examples for com.sun.source.tree.BinaryTree#getRightOperand()
The following examples show how to use
com.sun.source.tree.BinaryTree#getRightOperand() .
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: 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 3
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 4
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 5
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 6
Source File: IfTest.java From netbeans with Apache License 2.0 | 4 votes |
public void testReplaceCondition() throws Exception { testFile = new File(getWorkDir(), "IfTest.java"); TestUtilities.copyStringToFile(testFile, "package foo.bar;\n" + "\n" + "public class IfTest {\n" + " public void test(boolean b) {\n" + " if (prec == treeinfo.notExpression)\n" + " print(';');\n" + " }\n" + "}\n" ); String golden = "package foo.bar;\n" + "\n" + "public class IfTest {\n" + " public void test(boolean b) {\n" + " if (prec == TreeInfo.notExpression)\n" + " print(';');\n" + " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); TreeMaker make = workingCopy.getTreeMaker(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(1); IfTree oldIf = (IfTree) method.getBody().getStatements().get(0); BinaryTree zatvorka = (BinaryTree) ((ParenthesizedTree) oldIf.getCondition()).getExpression(); MemberSelectTree mst = (MemberSelectTree) zatvorka.getRightOperand(); workingCopy.rewrite(mst.getExpression(), make.Identifier("TreeInfo")); } public void cancel() { } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example 7
Source File: BoxedIdentityComparison.java From netbeans with Apache License 2.0 | 4 votes |
@TriggerPatterns({ @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Integer")), @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Byte")), @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Short")), @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Long")), @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Float")), @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Double")), @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Character")), @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Boolean")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Integer")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Byte")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Short")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Long")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Float")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Double")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Character")), @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Boolean")), }) public static ErrorDescription wrapperComparisonUsingIdentity(HintContext ctx) { TreePath logExprPath = ctx.getPath(); BinaryTree bt = (BinaryTree)logExprPath.getLeaf(); TreePath wrapperPath = ctx.getVariables().get("$x"); // NOI18N Tree otherOperand = bt.getRightOperand(); // JLS 15.21; if the other type is primitive, the comparison is correct TypeMirror t = ctx.getInfo().getTrees().getTypeMirror(new TreePath(logExprPath, otherOperand)); if (t == null || t.getKind() != TypeKind.DECLARED) { return null; } t = ctx.getInfo().getTrees().getTypeMirror(wrapperPath); // primitive type is assignable to the wrapper, so it will trigger the hint if (t == null || t.getKind() != TypeKind.DECLARED) { return null; } final Fix fix; if (ctx.getInfo().getSourceVersion().compareTo(SourceVersion.RELEASE_7) < 0) { fix = null; } else { fix = new NullSafeEqualsFix(TreePathHandle.create(logExprPath, ctx.getInfo())).toEditorFix(); } return ErrorDescriptionFactory.forTree(ctx, logExprPath, TEXT_BoxedValueIdentityComparison(ctx.getInfo().getTypeUtilities().getTypeName(t)), fix); }
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); } }