本文整理汇总了Java中com.sun.source.tree.TryTree类的典型用法代码示例。如果您正苦于以下问题:Java TryTree类的具体用法?Java TryTree怎么用?Java TryTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TryTree类属于com.sun.source.tree包,在下文中一共展示了TryTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitTry
import com.sun.source.tree.TryTree; //导入依赖的package包/类
public Boolean visitTry(TryTree node, TreePath p) {
if (p == null) {
super.visitTry(node, p);
return false;
}
TryTree at = (TryTree) p.getLeaf();
if (!checkLists(node.getResources(), at.getResources(), p)) {
return false;
}
if (!scan(node.getBlock(), at.getBlock(), p)) {
return false;
}
if (!checkLists(node.getCatches(), at.getCatches(), p)) {
return false;
}
return scan(node.getFinallyBlock(), at.getFinallyBlock(), p);
}
示例2: visitTry
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Override
public Void visitTry(TryTree node, Collection<TreePath> trees) {
Set<TypeMirror> caught = new HashSet<TypeMirror>();
for (CatchTree ct : node.getCatches()) {
TypeMirror t = info.getTrees().getTypeMirror(new TreePath(new TreePath(getCurrentPath(), ct), ct.getParameter()));
if (t != null) {
caught.add(t);
}
}
caughtExceptions.push(caught);
try {
scan(node.getBlock(), trees);
} finally {
caughtExceptions.pop();
}
scan(node.getFinallyBlock(), trees);
return null;
}
示例3: 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));
}
}
示例4: 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;
}
示例5: visitTry
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Override
public Boolean visitTry(TryTree node, Void p) {
Set<TypeMirror> caught = new HashSet<TypeMirror>();
for (CatchTree ct : node.getCatches()) {
TypeMirror t = info.getTrees().getTypeMirror(new TreePath(new TreePath(getCurrentPath(), ct), ct.getParameter()));
if (t != null) {
caught.add(t);
}
}
caughtExceptions.push(Pair.of(caught, node));
try {
return scan(node.getBlock(), p) == Boolean.TRUE || scan(node.getFinallyBlock(), p) == Boolean.TRUE;
} finally {
caughtExceptions.pop();
}
}
示例6: 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;
}
示例7: testVisitTry
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Test
public void testVisitTry() throws ParseException, BadLocationException, IOException {
List<ReformatOption> optionsToReformat = new ArrayList<>();
CompilationUnitTree unit = getCompilationUnitTree(INPUT_FILE);
MethodTree methodTree = TreeNavigationUtils.findMethodTreeByName("doSomething", unit).get(0);
TryTree tryTree = (TryTree) getStatementTreeByClassName(TryTree.class, methodTree);
if (tryTree == null) {
fail(ERROR_MESSAGE);
}
ReformatTreeVisitor reformatTreeVisitor = getReformatTreeVisitor(INPUT_FILE);
reformatTreeVisitor.visitTry(tryTree, optionsToReformat);
assertEquals(optionsToReformat.size(), 3);
}
示例8: testVisitTryNotReformat
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Test
public void testVisitTryNotReformat() throws ParseException, BadLocationException, IOException {
List<ReformatOption> optionsToReformat = new ArrayList<>();
CompilationUnitTree unit = getCompilationUnitTree(INPUT_FILE);
MethodTree methodTree = TreeNavigationUtils.findMethodTreeByName("doSomething", unit).get(0);
TryTree tryTree = (TryTree) getStatementTreeByClassName(TryTree.class, methodTree);
if (tryTree == null) {
fail(ERROR_MESSAGE);
}
ReformatTreeVisitor reformatTreeVisitor = getReformatTreeVisitor(INPUT_FILE, 5, 10);
reformatTreeVisitor.visitTry(tryTree, optionsToReformat);
assertTrue(optionsToReformat.isEmpty());
}
示例9: testVisitCatch
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Test
public void testVisitCatch() throws ParseException, BadLocationException, IOException {
List<ReformatOption> optionsToReformat = new ArrayList<>();
CompilationUnitTree unit = getCompilationUnitTree(INPUT_FILE);
MethodTree methodTree = TreeNavigationUtils.findMethodTreeByName("doSomething", unit).get(0);
TryTree tryTree = (TryTree) getStatementTreeByClassName(TryTree.class, methodTree);
if (tryTree == null) {
fail(ERROR_MESSAGE);
}
CatchTree catchTree = tryTree.getCatches().get(0);
ReformatTreeVisitor reformatTreeVisitor = getReformatTreeVisitor(INPUT_FILE);
reformatTreeVisitor.visitCatch(catchTree, optionsToReformat);
assertFalse(optionsToReformat.isEmpty());
}
示例10: testVisitCatchNotReformat
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Test
public void testVisitCatchNotReformat() throws ParseException, BadLocationException, IOException {
List<ReformatOption> optionsToReformat = new ArrayList<>();
CompilationUnitTree unit = getCompilationUnitTree(INPUT_FILE);
MethodTree methodTree = TreeNavigationUtils.findMethodTreeByName("doSomething", unit).get(0);
TryTree tryTree = (TryTree) getStatementTreeByClassName(TryTree.class, methodTree);
if (tryTree == null) {
fail(ERROR_MESSAGE);
}
CatchTree catchTree = tryTree.getCatches().get(0);
ReformatTreeVisitor reformatTreeVisitor = getReformatTreeVisitor(INPUT_FILE, 10, 20);
reformatTreeVisitor.visitCatch(catchTree, optionsToReformat);
assertTrue(optionsToReformat.isEmpty());
}
示例11: visitTry
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Override
public Choice<State<JCTry>> visitTry(final TryTree node, State<?> state) {
return chooseSubtrees(
state,
s -> unify(node.getResources(), s),
s -> unifyStatement(node.getBlock(), s),
s -> unify(node.getCatches(), s),
s -> unifyStatement(node.getFinallyBlock(), s),
(resources, block, catches, finallyBlock) ->
maker()
.Try(
resources,
(JCBlock) block,
List.convert(JCCatch.class, catches),
(JCBlock) finallyBlock));
}
示例12: matchTry
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Override
public Description matchTry(TryTree tree, VisitorState state) {
if (tryTreeMatches(tree, state)) {
List<? extends StatementTree> tryStatements = tree.getBlock().getStatements();
StatementTree lastTryStatement = tryStatements.get(tryStatements.size() - 1);
String failCall = String.format("\nfail(\"Expected %s\");", exceptionToString(tree));
SuggestedFix.Builder fixBuilder =
SuggestedFix.builder().postfixWith(lastTryStatement, failCall);
// Make sure that when the fail import is added it doesn't conflict with existing ones.
fixBuilder.removeStaticImport("junit.framework.Assert.fail");
fixBuilder.removeStaticImport("junit.framework.TestCase.fail");
fixBuilder.addStaticImport("org.junit.Assert.fail");
return describeMatch(lastTryStatement, fixBuilder.build());
} else {
return Description.NO_MATCH;
}
}
示例13: matchMethodInvocation
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!FENCE_MATCHER.matches(tree, state)) {
return NO_MATCH;
}
Tree previous = null;
OUTER:
for (Tree enclosing : state.getPath().getParentPath()) {
switch (enclosing.getKind()) {
case TRY:
if (((TryTree) enclosing).getFinallyBlock().equals(previous)) {
return NO_MATCH;
}
break;
case CLASS:
case METHOD:
case LAMBDA_EXPRESSION:
break OUTER;
default: // fall out
}
previous = enclosing;
}
return describeMatch(tree);
}
示例14: matchTry
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Override
public Description matchTry(TryTree tree, VisitorState state) {
if (tree.getCatches().isEmpty()) {
return NO_MATCH;
}
// Find catch blocks that contain only a call to fail, and that ignore the caught exception.
ImmutableList<CatchTree> catchBlocks =
tree.getCatches()
.stream()
.filter(
c ->
c.getBlock().getStatements().size() == 1
&& FAIL_METHOD.matches(getOnlyElement(c.getBlock().getStatements()), state))
.filter(c -> !catchVariableIsUsed(c))
.collect(toImmutableList());
if (catchBlocks.isEmpty()) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
rethrowFix(catchBlocks, state).ifPresent(description::addFix);
deleteFix(tree, catchBlocks, state).ifPresent(description::addFix);
return description.build();
}
示例15: matchMethodInvocation
import com.sun.source.tree.TryTree; //导入依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
// Match on calls to any override of WakeLock.release().
if (!RELEASE.matches(tree, state)) {
return NO_MATCH;
}
// Ok if surrounded in try/catch block that catches RuntimeException.
TryTree enclosingTry = findEnclosingNode(state.getPath(), TryTree.class);
if (enclosingTry != null
&& tryCatchesException(enclosingTry, state.getSymtab().runtimeExceptionType, state)) {
return NO_MATCH;
}
// Ok if WakeLock not in danger of unexpected exception.
// Also, can't perform analysis if WakeLock symbol not found.
Symbol wakelockSymbol = getSymbol(getReceiver(tree));
if (wakelockSymbol == null || !wakelockMayThrow(wakelockSymbol, state)) {
return NO_MATCH;
}
Tree releaseStatement = state.getPath().getParentPath().getLeaf();
return describeMatch(releaseStatement, getFix(releaseStatement, wakelockSymbol, state));
}