本文整理汇总了Java中com.sun.source.tree.TryTree.getFinallyBlock方法的典型用法代码示例。如果您正苦于以下问题:Java TryTree.getFinallyBlock方法的具体用法?Java TryTree.getFinallyBlock怎么用?Java TryTree.getFinallyBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.TryTree
的用法示例。
在下文中一共展示了TryTree.getFinallyBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: performRewrite
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
Tree t = ctx.getPath().getLeaf();
if (t.getKind() != Tree.Kind.CATCH) {
// remove a clause from the multi-catch
removeAlternativeFromMultiCatch(ctx);
return;
}
CatchTree toRemove = (CatchTree)t;
TryTree parent = (TryTree) ctx.getPath().getParentPath().getLeaf();
TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
if (parent.getResources().isEmpty() && parent.getCatches().size() == 1) {
List<StatementTree> repl = new ArrayList<>();
repl.addAll(parent.getBlock().getStatements());
if (parent.getFinallyBlock() != null) {
repl.addAll(parent.getFinallyBlock().getStatements());
}
Utilities.replaceStatement(ctx.getWorkingCopy(), ctx.getPath().getParentPath(), repl);
} else {
ctx.getWorkingCopy().rewrite(parent, make.removeTryCatch(parent, toRemove));
}
}
示例2: enclosingTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
static TreePath enclosingTry(TreePath from) {
TreePath tryPath = from;
while (tryPath != null
&& tryPath.getLeaf().getKind() != Kind.TRY
&& !TreeUtilities.CLASS_TREE_KINDS.contains(tryPath.getLeaf().getKind())
&& tryPath.getLeaf().getKind() != Kind.CATCH
&& tryPath.getLeaf().getKind() != Kind.LAMBDA_EXPRESSION)
tryPath = tryPath.getParentPath();
if (tryPath.getLeaf().getKind() == Kind.TRY) {
TryTree tt = (TryTree) tryPath.getLeaf();
//#104085: if the statement to be wrapped is inside a finally block of the try-catch,
//do not attempt to extend existing catches...:
for (Tree t : from) {
if (tt.getFinallyBlock() == t) {
return null;
}
}
return tryPath;
}
return null;
}
示例3: visitTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@Override
public Void visitTry(TryTree tryTree, List<ReformatOption> optionsToReformat) {
addLeftBraceToList(optionsToReformat, tryTree.getBlock(), PreferencesFormatOptions.BRACES_IN_OTHER_DECLARATION);
BlockTree finalBlock = tryTree.getFinallyBlock();
List<? extends CatchTree> catches = tryTree.getCatches();
if (finalBlock instanceof CompoundTree) {
addLeftBraceToList(optionsToReformat, finalBlock, PreferencesFormatOptions.BRACES_IN_OTHER_DECLARATION);
addRightBraceToList(optionsToReformat, (CompoundTree) finalBlock, PreferencesFormatOptions.AFTER_OTHER_DECLARATION);
} else if (!catches.isEmpty()) {
BlockTree catchBlock = catches.get(catches.size() - 1).getBlock();
addRightBraceToList(optionsToReformat, (CompoundTree) catchBlock, PreferencesFormatOptions.AFTER_OTHER_DECLARATION);
} else {
addRightBraceToList(optionsToReformat, (CompoundTree) tryTree.getBlock(), PreferencesFormatOptions.AFTER_OTHER_DECLARATION);
}
return null;
}
示例4: matchTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@Override
public Description matchTry (TryTree tree, VisitorState state) {
if (badEmptyCatchBlock(tree, state)) {
// If it has finally block, assume it's OK
BlockTree bt = tree.getFinallyBlock();
if (bt == null || bt.getStatements().size() == 0) {
CatchTree lastCatch = tree.getCatches().get(tree.getCatches().size() - 1);
LineMap lineMap = state.getPath().getCompilationUnit().getLineMap();
/* System.out.println("****** warning starts **************");
System.out.println("WARNING: empty catch: "
+ state.getPath().getCompilationUnit().getSourceFile().getName()
// + ":" + state.getEndPosition((JCTree) tree)
+ ":" + lineMap.getLineNumber(TreeInfo.getStartPos((JCTree) lastCatch)));
System.out.println(state.getPath().getLeaf());
System.out.println();
System.out.println("****** warning ends **************"); */
return describeMatch(lastCatch, NO_FIX);
}
}
return Description.NO_MATCH;
}
示例5: soleTryWithoutFinally
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
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;
}
示例6: visitTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@Override
public Boolean visitTry(TryTree that, Void unused) {
boolean completes = scan(that.getBlock());
// assume all catch blocks are reachable; javac has already rejected unreachable
// checked exception handlers
for (CatchTree catchTree : that.getCatches()) {
completes |= scan(catchTree.getBlock());
}
if (that.getFinallyBlock() != null && !scan(that.getFinallyBlock())) {
completes = false;
}
return completes;
}
示例7: visitTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@Override
public UTry visitTry(TryTree tree, Void v) {
@SuppressWarnings({"unchecked", "rawtypes"})
List<UTree<?>> resources =
cast(templateTrees(tree.getResources()), (Class<UTree<?>>) (Class) UTree.class);
UBlock block = visitBlock(tree.getBlock(), null);
ImmutableList.Builder<UCatch> catchesBuilder = ImmutableList.builder();
for (CatchTree catchTree : tree.getCatches()) {
catchesBuilder.add(visitCatch(catchTree, null));
}
UBlock finallyBlock =
(tree.getFinallyBlock() == null) ? null : visitBlock(tree.getFinallyBlock(), null);
return UTry.create(resources, block, catchesBuilder.build(), finallyBlock);
}
示例8: visitTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@Override
public Result visitTry(TryTree node, BreakContext cxt) {
Result result = node.getBlock().accept(this, cxt);
for (CatchTree catchTree : node.getCatches()) {
result = result.or(catchTree.accept(this, cxt));
}
if (node.getFinallyBlock() != null) {
result = result.then(node.getFinallyBlock().accept(this, cxt));
}
return result;
}
示例9: tryFinallyClose
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
private boolean tryFinallyClose(VarSymbol var, TreePath path, VisitorState state) {
if ((var.flags() & (Flags.FINAL | Flags.EFFECTIVELY_FINAL)) == 0) {
return false;
}
Tree parent = path.getParentPath().getLeaf();
if (parent.getKind() != Tree.Kind.BLOCK) {
return false;
}
BlockTree block = (BlockTree) parent;
int idx = block.getStatements().indexOf(path.getLeaf());
if (idx == -1 || idx == block.getStatements().size() - 1) {
return false;
}
StatementTree next = block.getStatements().get(idx + 1);
if (!(next instanceof TryTree)) {
return false;
}
TryTree tryTree = (TryTree) next;
if (tryTree.getFinallyBlock() == null) {
return false;
}
boolean[] closed = {false};
tryTree
.getFinallyBlock()
.accept(
new TreeScanner<Void, Void>() {
@Override
public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) {
if (CLOSE_METHOD.matches(tree, state)
&& Objects.equals(getSymbol(getReceiver(tree)), var)) {
closed[0] = true;
}
return null;
}
},
null);
return closed[0];
}
示例10: matchAncestor
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
/** Match a tree in the ancestor chain given the ancestor's immediate descendant. */
protected MatchResult matchAncestor(Tree leaf, Tree prevTree) {
if (leaf instanceof TryTree) {
TryTree tryTree = (TryTree) leaf;
if (tryTree.getFinallyBlock() != null && tryTree.getFinallyBlock().equals(prevTree)) {
return MatchResult.FOUND_ERROR;
}
}
return MatchResult.KEEP_LOOKING;
}
示例11: matchAncestor
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
/**
* Match a tree in the ancestor chain given the ancestor's immediate descendant.
*/
protected MatchResult matchAncestor(Tree leaf, Tree prevTree) {
if (leaf instanceof TryTree) {
TryTree tryTree = (TryTree) leaf;
if (tryTree.getFinallyBlock() != null && tryTree.getFinallyBlock().equals(prevTree)) {
return MatchResult.FOUND_ERROR;
}
}
return MatchResult.KEEP_LOOKING;
}
示例12: visitTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
public @Override Void visitTry(TryTree tt, Void p) {
List<CatchTree> catches = new ArrayList<CatchTree>();
catches.addAll(tt.getCatches());
catches.addAll(createCatches(info, make, thandles, statement));
if (!streamAlike) {
info.rewrite(tt, make.Try(tt.getResources(), tt.getBlock(), catches, tt.getFinallyBlock()));
} else {
VariableTree originalDeclaration = (VariableTree) statement.getLeaf();
VariableTree declaration = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), originalDeclaration.getName(), originalDeclaration.getType(), make.Literal(null));
StatementTree assignment = make.ExpressionStatement(make.Assignment(make.Identifier(originalDeclaration.getName()), originalDeclaration.getInitializer()));
List<StatementTree> finallyStatements = new ArrayList<StatementTree>(tt.getFinallyBlock() != null ? tt.getFinallyBlock().getStatements() : Collections.<StatementTree>emptyList());
finallyStatements.add(createFinallyCloseBlockStatement(originalDeclaration));
BlockTree finallyTree = make.Block(finallyStatements, false);
info.rewrite(originalDeclaration, assignment);
TryTree nueTry = make.Try(tt.getResources(), tt.getBlock(), catches, finallyTree);
TreePath currentBlockCandidate = statement;
while (currentBlockCandidate.getLeaf() != tt) {
currentBlockCandidate = currentBlockCandidate.getParentPath();
}
currentBlockCandidate = currentBlockCandidate.getParentPath();
if (currentBlockCandidate.getLeaf().getKind() == Kind.BLOCK) {
BlockTree originalTree = (BlockTree) currentBlockCandidate.getLeaf();
List<StatementTree> statements = new ArrayList<StatementTree>(originalTree.getStatements());
int index = statements.indexOf(tt);
statements.remove(index);
statements.add(index, nueTry);
statements.add(index, declaration);
info.rewrite(originalTree, make.Block(statements, originalTree.isStatic()));
} else {
BlockTree nueBlock = make.Block(Arrays.asList(declaration, nueTry), false);
info.rewrite(tt, nueBlock);
}
}
return null;
}
示例13: catch
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@TriggerPatterns({
@TriggerPattern(value="$lock.lock(); $otherStats$; try { $statements$; $lock.unlock(); $rest$; } catch $catches$ finally { $finstats$; } ",
[email protected](variable="$lock", type="java.util.concurrent.locks.Lock")),
@TriggerPattern(value="$lock.lock(); $otherStats$; try { $statements$; $lock.unlock(); $rest$; } catch $catches$",
[email protected](variable="$lock", type="java.util.concurrent.locks.Lock")),
@TriggerPattern(value="$lock.lock(); $otherStats$; try { $statements$; } catch $catches$ catch($excType $var) { $catchStats1$; $lock.unlock(); $catchStats2$; } catch $catches2$ finally { $finstmts$; }",
[email protected](variable="$lock", type="java.util.concurrent.locks.Lock")),
})
@NbBundle.Messages({
"ERR_UnlockOutsideTryFinally=Lock.lock() not unlocked in finally",
"FIX_UnlockOutsideTryFinally=Wrap by try-finally",
"MSG_ExtraUnlock=Extra unlock() call; lock is already released in finally"
})
public static ErrorDescription unlockInsideTry(HintContext ctx) {
TreePath fin = ctx.getVariables().get("$lock$1");
if (fin == null) {
return null;
}
TreePath parent = fin.getParentPath();
if (parent.getLeaf().getKind() != Tree.Kind.MEMBER_SELECT) {
return null;
}
parent = parent.getParentPath();
if (parent == null || parent.getLeaf().getKind() != Tree.Kind.METHOD_INVOCATION) {
return null;
}
TreePath tPath = parent.getParentPath();
while (tPath != null && tPath.getLeaf().getKind() != Tree.Kind.TRY) {
if (tPath.getLeaf().getKind() == Tree.Kind.METHOD ||
tPath.getLeaf().getKind() == Tree.Kind.CLASS) {
return null;
}
tPath = tPath.getParentPath();
}
if (tPath == null) {
return null;
}
TryTree tt = (TryTree)tPath.getLeaf();
Fix f = null;
String displayName = null;
if (tt.getFinallyBlock() != null) {
TreePath finBlockPath = new TreePath(tPath, tt.getFinallyBlock());
Collection<? extends Occurrence> occ = Matcher.create(ctx.getInfo()).
setSearchRoot(finBlockPath).
match(
Pattern.createSimplePattern(parent)
);
if (!occ.isEmpty()) {
f = new MoveUnlockFix(
TreePathHandle.create(parent, ctx.getInfo()),
null).toEditorFix();
displayName = Bundle.MSG_ExtraUnlock();
}
}
if (f == null) {
displayName = Bundle.ERR_UnlockOutsideTryFinally();
f = new MoveUnlockFix(
TreePathHandle.create(parent, ctx.getInfo()),
TreePathHandle.create(tPath, ctx.getInfo())).toEditorFix();
}
return ErrorDescriptionFactory.forName(ctx, parent, displayName, f);
}
示例14: visitTry
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
@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;
}
示例15: hasFinally
import com.sun.source.tree.TryTree; //导入方法依赖的package包/类
private boolean hasFinally(TryTree tree) {
return tree.getFinallyBlock() != null;
}