Java Code Examples for com.sun.source.tree.IfTree#getThenStatement()
The following examples show how to use
com.sun.source.tree.IfTree#getThenStatement() .
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: ConvertToPatternInstanceOf.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected void performRewrite(JavaFix.TransformationContext ctx) { WorkingCopy wc = ctx.getWorkingCopy(); TreePath main = ctx.getPath(); IfTree it = (IfTree) main.getLeaf(); InstanceOfTree iot = (InstanceOfTree) ((ParenthesizedTree) it.getCondition()).getExpression(); StatementTree bt = it.getThenStatement(); // wc.rewrite(iot.getType(), wc.getTreeMaker().BindingPattern(var.getName(), iot.getType())); // wc.rewrite(bt, wc.getTreeMaker().removeBlockStatement(bt, 0)); InstanceOfTree cond = wc.getTreeMaker().InstanceOf(iot.getExpression(), wc.getTreeMaker().BindingPattern(varName, iot.getType())); StatementTree thenBlock = removeFirst ? wc.getTreeMaker().removeBlockStatement((BlockTree) bt, 0) : bt; wc.rewrite(it, wc.getTreeMaker().If(wc.getTreeMaker().Parenthesized(cond), thenBlock, it.getElseStatement())); replaceOccurrences.stream().map(tph -> tph.resolve(wc)).forEach(tp -> { if (!removeFirst && tp.getParentPath().getLeaf().getKind() == Kind.PARENTHESIZED) { tp = tp.getParentPath(); } wc.rewrite(tp.getLeaf(), wc.getTreeMaker().Identifier(varName)); }); }
Example 2
Source File: EmptyStatements.java From netbeans with Apache License 2.0 | 6 votes |
@Hint(displayName = "#LBL_Empty_IF", description = "#DSC_Empty_IF", category = "empty", hintKind = Hint.Kind.INSPECTION, severity = Severity.VERIFIER, suppressWarnings = SUPPRESS_WARNINGS_KEY, id = "EmptyStatements_IF", enabled = false) @TriggerTreeKind(Tree.Kind.EMPTY_STATEMENT) public static ErrorDescription forIF(HintContext ctx) { final TreePath treePath = ctx.getPath(); Tree parent = treePath.getParentPath().getLeaf(); if (!EnumSet.of(Kind.IF).contains(parent.getKind())) { return null; } TreePath treePathForWarning = treePath; IfTree it = (IfTree) parent; if (it.getThenStatement() != null && it.getThenStatement().getKind() == Tree.Kind.EMPTY_STATEMENT) { treePathForWarning = treePath.getParentPath(); } if (it.getElseStatement() != null && it.getElseStatement().getKind() == Tree.Kind.EMPTY_STATEMENT) { treePathForWarning = treePath; } final List<Fix> fixes = new ArrayList<>(); fixes.add(FixFactory.createSuppressWarningsFix(ctx.getInfo(), treePathForWarning, SUPPRESS_WARNINGS_KEY)); return createErrorDescription(ctx, parent, fixes, parent.getKind()); }
Example 3
Source File: MethodMetrics.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Object visitReturn(ReturnTree node, Object p) { TreePath path = getCurrentPath(); TreePath parentPath = path.getParentPath(); if (suppress) { return super.visitReturn(node, p); } if (ignoreGuards && parentPath != null) { Tree parentTree = parentPath.getLeaf(); TreePath branchPath = path; while (parentTree.getKind() == Tree.Kind.BLOCK) { branchPath = parentPath; parentPath = parentPath.getParentPath(); parentTree = parentPath.getLeaf(); } if (parentTree.getKind() == Tree.Kind.IF) { IfTree ifTree = (IfTree)parentTree; StatementTree trueTree = ifTree.getThenStatement() == branchPath.getLeaf() ? ifTree.getThenStatement() : ifTree.getElseStatement(); if (trueTree == node) { return super.visitReturn(node, p); } if (trueTree.getKind() == Tree.Kind.BLOCK) { BlockTree bt = (BlockTree)trueTree; if (bt.getStatements().size() == 1) { return super.visitReturn(node, p); } } } } returnCount++; return super.visitReturn(node, p); }
Example 4
Source File: Refactorer.java From netbeans with Apache License 2.0 | 5 votes |
private boolean isReturningIf( IfTree ifTree) { StatementTree then = ifTree.getThenStatement(); if (then.getKind() == Tree.Kind.RETURN) { return true; } else if (then.getKind() == Tree.Kind.BLOCK) { BlockTree block = (BlockTree) then; if (block.getStatements().size() == 1 && block.getStatements().get(0).getKind() == Tree.Kind.RETURN) { return true; } } return false; }
Example 5
Source File: Refactorer.java From netbeans with Apache License 2.0 | 5 votes |
private Boolean isIfWithContinue( IfTree ifTree) { StatementTree then = ifTree.getThenStatement(); if (then.getKind() == Tree.Kind.CONTINUE) { return true; } else if (then.getKind() == Tree.Kind.BLOCK) { List<? extends StatementTree> statements = ((BlockTree) then).getStatements(); if (statements.size() == 1 && statements.get(0).getKind() == Tree.Kind.CONTINUE) { return true; } } return false; }
Example 6
Source File: Refactorer.java From netbeans with Apache License 2.0 | 5 votes |
private List<ProspectiveOperation> getIfListRepresentation( StatementTree tree, boolean last) { IfTree ifTree = (IfTree) tree; List<ProspectiveOperation> ls = new ArrayList<ProspectiveOperation>(); if (ifTree.getElseStatement() == null) { StatementTree then = ifTree.getThenStatement(); if (isOneStatementBlock(then)) { then = ((BlockTree) then).getStatements().get(0); } if (then.getKind() == Tree.Kind.RETURN) { ReturnTree returnTree = (ReturnTree) then; ExpressionTree returnExpression = returnTree.getExpression(); if (returnExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL && ((LiteralTree) returnExpression).getValue().equals(true)) { ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.ANYMATCH, this.preconditionsChecker, this.workingCopy)); } else if (returnExpression.getKind() == Tree.Kind.BOOLEAN_LITERAL && ((LiteralTree) returnExpression).getValue().equals(false)) { ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.NONEMATCH, this.preconditionsChecker, this.workingCopy)); } } else { ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.FILTER, this.preconditionsChecker, this.workingCopy)); ls.addAll(getListRepresentation(ifTree.getThenStatement(), last)); } } else { ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.MAP, this.preconditionsChecker, this.workingCopy)); } return ls; }
Example 7
Source File: ExpressionScanner.java From netbeans with Apache License 2.0 | 5 votes |
@Override public List<Tree> visitIf(IfTree node, ExpressionScanner.ExpressionsInfo p) { List<Tree> cond = null; Tree lastCond = null; if (acceptsTree(node)) { cond = scan(node.getCondition(), p); if (cond != null) { lastCond = cond.get(cond.size() - 1); } } StatementTree thent = node.getThenStatement(); StatementTree elset = node.getElseStatement(); List<Tree> thenr = null; if (isCurrentTree(thent)) { thenr = scan(thent, p); if (lastCond != null && thenr != null) { p.addNextExpression(lastCond, thenr.get(0)); } } List<Tree> elser = null; if (isCurrentTree(elset)) { elser = scan(elset, p); if (lastCond != null && elser != null) { p.addNextExpression(lastCond, elser.get(0)); } } return reduce(reduce(cond, thenr), elser); }
Example 8
Source File: IfTest.java From netbeans with Apache License 2.0 | 4 votes |
public void test158463a() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package personal;\n" + "\n" + "public class Test {\n" + " void m1(int p, int q) {\n" + " if (p > 0)\n" + " if (q > 0) { p++; }\n" + " else { p--; }\n" + " }\n" + "}\n"); String golden = "package personal;\n" + "\n" + "public class Test {\n" + " void m1(int p, int q) {\n" + " if ((p > 0) && (q > 0)) {\n" + " p++;\n" + " } else {\n" + " p--;\n" + " }\n" + " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(Phase.RESOLVED); TreeMaker make = workingCopy.getTreeMaker(); ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(1); BlockTree block = method.getBody(); IfTree original = (IfTree) block.getStatements().get(0); IfTree original2 = (IfTree) original.getThenStatement(); IfTree modified = make.If( make.Parenthesized( make.Binary(Kind.CONDITIONAL_AND, original.getCondition(), original2.getCondition())), original2.getThenStatement(), original2.getElseStatement()); workingCopy.rewrite(original, modified); } public void cancel() { } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example 9
Source File: IfTest.java From netbeans with Apache License 2.0 | 4 votes |
public void test158463b() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package personal;\n" + "\n" + "public class Test {\n" + " void m1(int p, int q) {\n" + " if (p > 0)\n" + " if (q > 0) p++; \n" + " else p--;\n" + " }\n" + "}\n"); String golden = "package personal;\n" + "\n" + "public class Test {\n" + " void m1(int p, int q) {\n" + " if ((p > 0) && (q > 0))\n" + " p++;\n" + " else {\n" + //TODO: brackets (#158154) " p--;\n" + " }\n" + " }\n" + "}\n"; JavaSource src = getJavaSource(testFile); Task<WorkingCopy> task = new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(Phase.RESOLVED); TreeMaker make = workingCopy.getTreeMaker(); ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); MethodTree method = (MethodTree) clazz.getMembers().get(1); BlockTree block = method.getBody(); IfTree original = (IfTree) block.getStatements().get(0); IfTree original2 = (IfTree) original.getThenStatement(); IfTree modified = make.If( make.Parenthesized( make.Binary(Kind.CONDITIONAL_AND, original.getCondition(), original2.getCondition())), original2.getThenStatement(), original2.getElseStatement()); workingCopy.rewrite(original, modified); } public void cancel() { } }; src.runModificationTask(task).commit(); String res = TestUtilities.copyFileToString(testFile); //System.err.println(res); assertEquals(golden, res); }
Example 10
Source File: Braces.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(); Tree oldTree = path.getLeaf(); oldTree = GeneratorUtilities.get(copy).importComments(oldTree, copy.getCompilationUnit()); switch( oldTree.getKind() ) { case FOR_LOOP: ForLoopTree oldFor = (ForLoopTree)oldTree; StatementTree oldBlock = oldFor.getStatement(); BlockTree newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case ENHANCED_FOR_LOOP: EnhancedForLoopTree oldEnhancedFor = (EnhancedForLoopTree)oldTree; oldBlock = oldEnhancedFor.getStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case WHILE_LOOP: WhileLoopTree oldWhile = (WhileLoopTree)oldTree; oldBlock = oldWhile.getStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case DO_WHILE_LOOP: DoWhileLoopTree oldDoWhile = (DoWhileLoopTree)oldTree; oldBlock = oldDoWhile.getStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); break; case IF: IfTree oldIf = (IfTree)oldTree; if ( fixThen ) { oldBlock = oldIf.getThenStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); } if ( fixElse ) { oldBlock = oldIf.getElseStatement(); newBlock = make.Block(Collections.<StatementTree>singletonList(oldBlock), false); copy.rewrite(oldBlock, newBlock); } } } }
Example 11
Source File: Tiny.java From netbeans with Apache License 2.0 | 4 votes |
@Hint(displayName = "#DN_indentation", description = "#DESC_indentation", category="bugs", suppressWarnings="SuspiciousIndentAfterControlStatement", options=Options.QUERY) @TriggerTreeKind({Kind.IF, Kind.WHILE_LOOP, Kind.FOR_LOOP, Kind.ENHANCED_FOR_LOOP}) public static ErrorDescription indentation(HintContext ctx) { Tree firstStatement; Tree found = ctx.getPath().getLeaf(); switch (found.getKind()) { case IF: IfTree it = (IfTree) found; if (it.getElseStatement() != null) firstStatement = it.getElseStatement(); else firstStatement = it.getThenStatement(); break; case WHILE_LOOP: firstStatement = ((WhileLoopTree) found).getStatement(); break; case FOR_LOOP: firstStatement = ((ForLoopTree) found).getStatement(); break; case ENHANCED_FOR_LOOP: firstStatement = ((EnhancedForLoopTree) found).getStatement(); break; default: return null; } if (firstStatement != null && firstStatement.getKind() == Kind.BLOCK) { return null; } Tree parent = ctx.getPath().getParentPath().getLeaf(); List<? extends Tree> parentStatements; switch (parent.getKind()) { case BLOCK: parentStatements = ((BlockTree) parent).getStatements(); break; case CASE: parentStatements = ((CaseTree) parent).getStatements(); break; default: return null; } int index = parentStatements.indexOf(found); if (index < 0 || index + 1 >= parentStatements.size()) return null; Tree secondStatement = parentStatements.get(index + 1); int firstIndent = indent(ctx, firstStatement); int secondIndent = indent(ctx, secondStatement); if (firstIndent == (-1) || secondIndent == (-1) || firstIndent != secondIndent) return null; return ErrorDescriptionFactory.forTree(ctx, secondStatement, Bundle.ERR_indentation()); }