下面列出了com.sun.source.tree.TryTree#getResources ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
@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;
}
@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;
}
@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;
}