Java Code Examples for com.sun.source.tree.TryTree#getResources()
The following examples show how to use
com.sun.source.tree.TryTree#getResources() .
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: 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 2
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
@Override public Void visitTry(TryTree node, Void unused) { sync(node); builder.open(ZERO); token("try"); builder.space(); if (!node.getResources().isEmpty()) { token("("); builder.open(node.getResources().size() > 1 ? plusFour : ZERO); boolean first = true; for (Tree resource : node.getResources()) { if (!first) { builder.forcedBreak(); } VariableTree variableTree = (VariableTree) resource; declareOne( DeclarationKind.PARAMETER, fieldAnnotationDirection(variableTree.getModifiers()), Optional.of(variableTree.getModifiers()), variableTree.getType(), VarArgsOrNot.NO, ImmutableList.<AnnotationTree>of(), variableTree.getName(), "", "=", Optional.fromNullable(variableTree.getInitializer()), Optional.<String>absent(), Optional.<ExpressionTree>absent(), Optional.<TypeWithDims>absent()); if (builder.peekToken().equals(Optional.of(";"))) { token(";"); builder.space(); } first = false; } if (builder.peekToken().equals(Optional.of(";"))) { token(";"); builder.space(); } token(")"); builder.close(); builder.space(); } // An empty try-with-resources body can collapse to "{}" if there are no trailing catch or // finally blocks. boolean trailingClauses = !node.getCatches().isEmpty() || node.getFinallyBlock() != null; visitBlock( node.getBlock(), CollapseEmptyOrNot.valueOf(!trailingClauses), AllowLeadingBlankLine.YES, AllowTrailingBlankLine.valueOf(trailingClauses)); for (int i = 0; i < node.getCatches().size(); i++) { CatchTree catchClause = node.getCatches().get(i); trailingClauses = i < node.getCatches().size() - 1 || node.getFinallyBlock() != null; visitCatchClause(catchClause, AllowTrailingBlankLine.valueOf(trailingClauses)); } if (node.getFinallyBlock() != null) { builder.space(); token("finally"); builder.space(); visitBlock( node.getFinallyBlock(), CollapseEmptyOrNot.NO, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.NO); } builder.close(); return null; }
Example 3
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 4 votes |
@Override public Void visitTry(TryTree node, Void unused) { sync(node); builder.open(ZERO); token("try"); builder.space(); if (!node.getResources().isEmpty()) { token("("); builder.open(node.getResources().size() > 1 ? plusFour : ZERO); boolean first = true; for (Tree resource : node.getResources()) { if (!first) { builder.forcedBreak(); } VariableTree variableTree = (VariableTree) resource; declareOne( DeclarationKind.PARAMETER, fieldAnnotationDirection(variableTree.getModifiers()), Optional.of(variableTree.getModifiers()), variableTree.getType(), VarArgsOrNot.NO, ImmutableList.<AnnotationTree>of(), variableTree.getName(), "", "=", Optional.fromNullable(variableTree.getInitializer()), Optional.<String>absent(), Optional.<ExpressionTree>absent(), Optional.<TypeWithDims>absent()); if (builder.peekToken().equals(Optional.of(";"))) { token(";"); builder.space(); } first = false; } if (builder.peekToken().equals(Optional.of(";"))) { token(";"); builder.space(); } token(")"); builder.close(); builder.space(); } // An empty try-with-resources body can collapse to "{}" if there are no trailing catch or // finally blocks. boolean trailingClauses = !node.getCatches().isEmpty() || node.getFinallyBlock() != null; visitBlock( node.getBlock(), CollapseEmptyOrNot.valueOf(!trailingClauses), AllowLeadingBlankLine.YES, AllowTrailingBlankLine.valueOf(trailingClauses)); for (int i = 0; i < node.getCatches().size(); i++) { CatchTree catchClause = node.getCatches().get(i); trailingClauses = i < node.getCatches().size() - 1 || node.getFinallyBlock() != null; visitCatchClause(catchClause, AllowTrailingBlankLine.valueOf(trailingClauses)); } if (node.getFinallyBlock() != null) { builder.space(); token("finally"); builder.space(); visitBlock( node.getFinallyBlock(), CollapseEmptyOrNot.NO, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.NO); } builder.close(); return null; }
Example 4
Source File: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 4 votes |
@Override public Void visitTry(TryTree node, Void unused) { sync(node); builder.open(ZERO); token("try"); builder.space(); if (!node.getResources().isEmpty()) { token("("); builder.open(node.getResources().size() > 1 ? plusFour : ZERO); boolean first = true; for (Tree resource : node.getResources()) { if (!first) { builder.forcedBreak(); } if (resource instanceof VariableTree) { VariableTree variableTree = (VariableTree) resource; declareOne( DeclarationKind.PARAMETER, fieldAnnotationDirection(variableTree.getModifiers()), Optional.of(variableTree.getModifiers()), variableTree.getType(), /* name= */ variableTree.getName(), "", "=", Optional.ofNullable(variableTree.getInitializer()), /* trailing= */ Optional.empty(), /* receiverExpression= */ Optional.empty(), /* typeWithDims= */ Optional.empty()); } else { // TODO(cushon): think harder about what to do with `try (resource1; resource2) {}` scan(resource, null); } if (builder.peekToken().equals(Optional.of(";"))) { token(";"); builder.space(); } first = false; } if (builder.peekToken().equals(Optional.of(";"))) { token(";"); builder.space(); } token(")"); builder.close(); builder.space(); } // An empty try-with-resources body can collapse to "{}" if there are no trailing catch or // finally blocks. boolean trailingClauses = !node.getCatches().isEmpty() || node.getFinallyBlock() != null; visitBlock( node.getBlock(), CollapseEmptyOrNot.valueOf(!trailingClauses), AllowLeadingBlankLine.YES, AllowTrailingBlankLine.valueOf(trailingClauses)); for (int i = 0; i < node.getCatches().size(); i++) { CatchTree catchClause = node.getCatches().get(i); trailingClauses = i < node.getCatches().size() - 1 || node.getFinallyBlock() != null; visitCatchClause(catchClause, AllowTrailingBlankLine.valueOf(trailingClauses)); } if (node.getFinallyBlock() != null) { builder.space(); token("finally"); builder.space(); visitBlock( node.getFinallyBlock(), CollapseEmptyOrNot.NO, AllowLeadingBlankLine.YES, AllowTrailingBlankLine.NO); } builder.close(); return null; }