Java Code Examples for com.sun.source.tree.StatementTree#getKind()
The following examples show how to use
com.sun.source.tree.StatementTree#getKind() .
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 javaide with GNU General Public License v3.0 | 6 votes |
/** * Helper method for statements. */ private void visitStatement( StatementTree node, CollapseEmptyOrNot collapseEmptyOrNot, AllowLeadingBlankLine allowLeadingBlank, AllowTrailingBlankLine allowTrailingBlank) { sync(node); switch (node.getKind()) { case BLOCK: builder.space(); visitBlock((BlockTree) node, collapseEmptyOrNot, allowLeadingBlank, allowTrailingBlank); break; default: builder.open(plusTwo); builder.breakOp(" "); scan(node, null); builder.close(); } }
Example 2
Source File: Flow.java From netbeans with Apache License 2.0 | 6 votes |
public Boolean visitBlock(BlockTree node, ConstructorData p) { List<? extends StatementTree> statements = new ArrayList<StatementTree>(node.getStatements()); for (int i = 0; i < statements.size(); i++) { StatementTree st = statements.get(i); if (st.getKind() == Kind.IF) { IfTree it = (IfTree) st; if (it.getElseStatement() == null && Utilities.exitsFromAllBranchers(info, new TreePath(new TreePath(getCurrentPath(), it), it.getThenStatement()))) { generalizedIf(it.getCondition(), it.getThenStatement(), statements.subList(i + 1, statements.size()), false); break; } } scan(st, null); } return null; }
Example 3
Source File: ForLoopToFunctionalHint.java From netbeans with Apache License 2.0 | 6 votes |
@TriggerTreeKind(Tree.Kind.ENHANCED_FOR_LOOP) @Messages("ERR_ForLoopToFunctionalHint=Can use functional operations") public static ErrorDescription computeWarning(HintContext ctx) { if (ctx.getInfo().getElements().getTypeElement("java.util.stream.Streams") == null && !DISABLE_CHECK_FOR_STREAM) return null; PreconditionsChecker pc = new PreconditionsChecker(ctx.getPath().getLeaf(), ctx.getInfo()); if (pc.isSafeToRefactor()) { EnhancedForLoopTree eflt = (EnhancedForLoopTree)ctx.getPath().getLeaf(); StatementTree stmt = eflt.getStatement(); if (stmt == null) { return null; } if (stmt.getKind() == Tree.Kind.BLOCK) { BlockTree bt = (BlockTree)stmt; if (bt.getStatements() == null || bt.getStatements().isEmpty()) { return null; } } Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath(), null).toEditorFix(); return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_ForLoopToFunctionalHint(), fix); } return null; }
Example 4
Source File: AssignResultToVariable.java From netbeans with Apache License 2.0 | 6 votes |
private StatementTree findMatchingMethodInvocation(CompilationInfo info, BlockTree block, int offset) { for (StatementTree t : block.getStatements()) { if (t.getKind() != Kind.EXPRESSION_STATEMENT) continue; long statementStart = info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t); if (offset < statementStart) return null; ExpressionStatementTree est = (ExpressionStatementTree) t; long statementEnd = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t); long expressionEnd = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), est.getExpression()); if (expressionEnd <= offset && offset < statementEnd) { return t; } } return null; }
Example 5
Source File: Braces.java From netbeans with Apache License 2.0 | 6 votes |
private static ErrorDescription checkStatement(HintContext ctx, String dnKey, StatementTree statement, TreePath tp) { if ( statement != null && statement.getKind() != Tree.Kind.EMPTY_STATEMENT && statement.getKind() != Tree.Kind.BLOCK && statement.getKind() != Tree.Kind.TRY && statement.getKind() != Tree.Kind.SYNCHRONIZED && statement.getKind() != Tree.Kind.SWITCH && statement.getKind() != Tree.Kind.ERRONEOUS && !isErroneousExpression( statement )) { return ErrorDescriptionFactory.forTree( ctx, statement, NbBundle.getMessage(Braces.class, dnKey), new BracesFix(ctx.getInfo().getFileObject(), TreePathHandle.create(tp, ctx.getInfo())).toEditorFix()); } return null; }
Example 6
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * Helper method for statements. */ private void visitStatement( StatementTree node, CollapseEmptyOrNot collapseEmptyOrNot, AllowLeadingBlankLine allowLeadingBlank, AllowTrailingBlankLine allowTrailingBlank) { sync(node); switch (node.getKind()) { case BLOCK: builder.space(); visitBlock((BlockTree) node, collapseEmptyOrNot, allowLeadingBlank, allowTrailingBlank); break; default: builder.open(plusTwo); builder.breakOp(" "); scan(node, null); builder.close(); } }
Example 7
Source File: Refactorer.java From netbeans with Apache License 2.0 | 5 votes |
private List<ProspectiveOperation> getBlockListRepresentation( StatementTree tree, boolean last) { List<ProspectiveOperation> ls = new ArrayList<ProspectiveOperation>(); BlockTree blockTree = (BlockTree) tree; List<? extends StatementTree> statements = blockTree.getStatements(); for ( int i = 0; i < statements.size(); i++) { StatementTree statement = statements.get(i); boolean l = last && i == statements.size() - 1; if (statement.getKind() == Tree.Kind.IF) { IfTree ifTree = (IfTree) statement; if (isIfWithContinue(ifTree)) { ifTree = refactorContinuingIf(ifTree, statements.subList(i + 1, statements.size())); // the if was refactored, so that all the statements are nested in it, so it became // the last (and single) statement within the parent ls.addAll(this.getListRepresentation(ifTree, last)); break; } else if (l) { ls.addAll(this.getListRepresentation(ifTree, true)); } else { if (this.isReturningIf(ifTree)) { this.untrasformable = true; } ls.addAll(ProspectiveOperation.createOperator(ifTree, ProspectiveOperation.OperationType.MAP, preconditionsChecker, workingCopy)); } } else { ls.addAll(getListRepresentation(statement, l)); } } return ls; }
Example 8
Source File: AddParameterOrLocalFix.java From netbeans with Apache License 2.0 | 5 votes |
private void resolveLocalVariable55(final WorkingCopy wc, TreePath tp, TreeMaker make, TypeMirror proposedType) { final String name = ((IdentifierTree) tp.getLeaf()).getName().toString(); TreePath blockPath = findOutmostBlock(tp); if (blockPath == null) { return; } int index = 0; BlockTree block = ((BlockTree) blockPath.getLeaf()); TreePath method = findMethod(tp); if (method != null && ((MethodTree) method.getLeaf()).getReturnType() == null && !block.getStatements().isEmpty()) { StatementTree stat = block.getStatements().get(0); if (stat.getKind() == Kind.EXPRESSION_STATEMENT) { Element thisMethodEl = wc.getTrees().getElement(method); TreePath pathToFirst = new TreePath(new TreePath(new TreePath(method, block), stat), ((ExpressionStatementTree) stat).getExpression()); Element superCall = wc.getTrees().getElement(pathToFirst); if (thisMethodEl != null && superCall != null && thisMethodEl.getKind() == ElementKind.CONSTRUCTOR && superCall.getKind() == ElementKind.CONSTRUCTOR) { index = 1; } } } VariableTree vt = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(proposedType), null); wc.rewrite(block, wc.getTreeMaker().insertBlockStatement(block, index, vt)); }
Example 9
Source File: RemoveSurroundingCodeAction.java From netbeans with Apache License 2.0 | 5 votes |
private void addStat(StatementTree stat, List<StatementTree> to) { if (stat != null) { if (stat.getKind() == Tree.Kind.BLOCK) { to.addAll(((BlockTree) stat).getStatements()); } else { to.add(stat); } } }
Example 10
Source File: FinalizeDoesNotCallSuper.java From netbeans with Apache License 2.0 | 5 votes |
private TryTree soleTryWithoutFinally(BlockTree block) { if (block.getStatements().size() != 1) return null; StatementTree first = block.getStatements().get(0); if (first.getKind() != Kind.TRY) return null; TryTree tt = (TryTree) first; if (tt.getFinallyBlock() != null) return null; return tt; }
Example 11
Source File: ProspectiveOperation.java From netbeans with Apache License 2.0 | 5 votes |
private BlockTree addReturn(StatementTree statement, Name varName) { List<StatementTree> ls = new ArrayList<StatementTree>(); if (statement.getKind() == Tree.Kind.BLOCK) { ls.addAll(((BlockTree) statement).getStatements()); } else { ls.add(statement); } if (varName != null) { ls.add(this.treeMaker.Return(treeMaker.Identifier(varName.toString()))); } else { ls.add(this.treeMaker.Return(treeMaker.Identifier(UNKNOWN_NAME))); } return treeMaker.Block(ls, false); }
Example 12
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 13
Source File: Refactorer.java From netbeans with Apache License 2.0 | 5 votes |
private List<ProspectiveOperation> getListRepresentation( StatementTree tree, boolean last) { List<ProspectiveOperation> ls = new ArrayList<ProspectiveOperation>(); if (tree.getKind() == Tree.Kind.BLOCK) { ls.addAll(getBlockListRepresentation(tree, last)); } else if (tree.getKind() == Tree.Kind.IF) { ls.addAll(getIfListRepresentation(tree, last)); } else { ls.addAll(getSingleStatementListRepresentation(tree)); } return ls; }
Example 14
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 15
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 16
Source File: ConvertToARM.java From netbeans with Apache License 2.0 | 5 votes |
private static Collection<VariableTree> findVariableDecls( final List<? extends StatementTree> statements, final Tree parent) { final List<VariableTree> varDecls = new LinkedList<VariableTree>(); for (StatementTree st : statements) { if (st.getKind() == Tree.Kind.VARIABLE) { varDecls.add((VariableTree)st); } } return varDecls; }
Example 17
Source File: ConvertToARM.java From netbeans with Apache License 2.0 | 5 votes |
private static TryTree findNestedARM( final Collection<? extends StatementTree> stms, final StatementTree var) { int state = var != null ? 0 : 1; for (StatementTree stm : stms) { if (stm == var) { state = 1; } if (state == 1) { if (stm.getKind() == Kind.TRY) { final TryTree tryTree = (TryTree)stm; if (tryTree.getResources() != null && !tryTree.getResources().isEmpty()) { return tryTree; } else { final Iterator<? extends StatementTree> blkStms = tryTree.getBlock().getStatements().iterator(); if (blkStms.hasNext()) { StatementTree bstm = blkStms.next(); if (bstm.getKind() == Kind.TRY) { return (TryTree)bstm; } if (bstm.getKind() == Kind.EXPRESSION_STATEMENT && blkStms.hasNext()) { bstm = blkStms.next(); if (bstm.getKind() == Kind.TRY) { return (TryTree)bstm; } } } } } if (stm != var) { break; } } } return null; }
Example 18
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
private static Name getLeftTreeName(StatementTree statement) { if (statement.getKind() != Kind.EXPRESSION_STATEMENT) { return null; } JCTree.JCExpressionStatement jceTree = (JCTree.JCExpressionStatement) statement; if (jceTree.expr.getKind() != Kind.ASSIGNMENT) { return null; } JCTree.JCAssign assignTree = (JCTree.JCAssign) jceTree.expr; return ((JCTree.JCIdent) assignTree.lhs).name; }
Example 19
Source File: DoubleCheck.java From netbeans with Apache License 2.0 | 4 votes |
/** * Checks, that the two if statements test the same variable. Returns the tree that references * the guard variable if the two ifs are the same, or {@code null} if the ifs do not match. * @param info * @param statementTP * @param secondTP * @return */ private static TreePath sameIfAndValidate(CompilationInfo info, TreePath statementTP, TreePath secondTP, TreePath[] fieldRef) { StatementTree statement = (StatementTree) statementTP.getLeaf(); if (statement.getKind() != Kind.IF) { return null; } IfTree first = (IfTree)statement; IfTree second = (IfTree) secondTP.getLeaf(); if (first.getElseStatement() != null) { return null; } if (second.getElseStatement() != null) { return null; } TreePath varFirst = equalToNull(new TreePath(statementTP, first.getCondition())); TreePath varSecond = equalToNull(new TreePath(secondTP, second.getCondition())); if (varFirst == null || varSecond == null) { return null; } Element firstVariable = info.getTrees().getElement(varFirst); Element secondVariable = info.getTrees().getElement(varSecond); Element target = firstVariable; if (firstVariable != null && firstVariable.equals(secondVariable)) { TreePath var = info.getTrees().getPath(firstVariable); if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_5) < 0) { fieldRef[0] = var; return varFirst; } if (firstVariable.getKind() == ElementKind.LOCAL_VARIABLE) { // check how the variable was assigned: TreePath methodPath = Utilities.findTopLevelBlock(varFirst); FlowResult fr = Flow.assignmentsForUse(info, methodPath, new AtomicBoolean(false)); Iterable<? extends TreePath> itp = fr.getAssignmentsForUse().get(varFirst.getLeaf()); if (itp != null) { Iterator<? extends TreePath> i = itp.iterator(); if (i.hasNext()) { TreePath v = i.next(); if (!i.hasNext()) { // if the local variable has exactly one possible value, // use it as the field target = info.getTrees().getElement(v); if (target != null && target.getKind() == ElementKind.FIELD) { var = info.getTrees().getPath(target); if (!sameCompilationUnit(var, varFirst)) { // the variable is somewhere ... var = info.getTrees().getPath(firstVariable); } } } } } } fieldRef[0] = var; return varFirst; } return null; }
Example 20
Source File: IteratorToFor.java From netbeans with Apache License 2.0 | 4 votes |
private String assignedToVariable(TransformationContext ctx, TypeMirror variableType, TreePath forStatement, List<TreePath> toReplace) { if (forStatement.getLeaf().getKind() != Kind.BLOCK) return null; BlockTree block = (BlockTree) forStatement.getLeaf(); if (block.getStatements().isEmpty()) return null; StatementTree first = block.getStatements().get(0); if (first.getKind() != Kind.VARIABLE) return null; VariableTree var = (VariableTree) first; TypeMirror varType = ctx.getWorkingCopy().getTrees().getTypeMirror(new TreePath(forStatement, var.getType())); if (varType == null || !ctx.getWorkingCopy().getTypes().isSameType(variableType, varType)) return null; for (TreePath tp : toReplace) { if (tp.getLeaf() == var.getInitializer()) return var.getName().toString(); } return null; }