本文整理汇总了Java中com.sun.source.tree.CatchTree类的典型用法代码示例。如果您正苦于以下问题:Java CatchTree类的具体用法?Java CatchTree怎么用?Java CatchTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CatchTree类属于com.sun.source.tree包,在下文中一共展示了CatchTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitCatch
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
@Override
public Boolean visitCatch(CatchTree tree, Stack<Tree> d) {
TypeMirror type1 = info.getTrees().getTypeMirror(new TreePath(new TreePath(getCurrentPath(), tree.getParameter()), tree.getParameter().getType()));
Types t = info.getTypes();
if (type1 != null) {
Set<TypeMirror> toRemove = new HashSet<TypeMirror>();
Map<TypeMirror, List<Tree>> exceptions2Highlights = exceptions2HighlightsStack.peek();
if (exceptions2Highlights != null) {
for (TypeMirror type2 : exceptions2Highlights.keySet()) {
if (t.isAssignable(type2, type1)) {
toRemove.add(type2);
}
}
for (TypeMirror type : toRemove) {
exceptions2Highlights.remove(type);
}
}
}
scan(tree.getParameter(), d);
return scan(tree.getBlock(), d);
}
示例2: performRewrite
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
@Override
protected void performRewrite(TransformationContext ctx) {
WorkingCopy wc = ctx.getWorkingCopy();
TreePath tp = ctx.getPath();
List<Tree> exceptions = new LinkedList<Tree>();
for (TypeMirrorHandle<TypeMirror> h : exceptionHandles) {
TypeMirror tm = h.resolve(wc);
if (tm == null) return ; //XXX: log
exceptions.add(wc.getTreeMaker().Type(tm));
}
VariableTree excVar = ((CatchTree) tp.getLeaf()).getParameter();
wc.rewrite(excVar.getType(), wc.getTreeMaker().UnionType(exceptions));
}
示例3: visitTry
import com.sun.source.tree.CatchTree; //导入依赖的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;
}
示例4: performRewrite
import com.sun.source.tree.CatchTree; //导入依赖的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));
}
}
示例5: createCatch
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
private static CatchTree createCatch(WorkingCopy info, TreeMaker make, TreePath statement, String name, TypeMirror type) {
StatementTree logStatement = createExceptionsStatement(info, make, name);
if (logStatement == null) {
logStatement = createLogStatement(info, make, statement, name);
}
if (logStatement == null) {
logStatement = createRethrowAsRuntimeExceptionStatement(info, make, name);
}
if (logStatement == null) {
logStatement = createRethrow(info, make, name);
}
if (logStatement == null) {
logStatement = createPrintStackTraceStatement(info, make, name);
}
return make.Catch(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), name, make.Type(type), null), make.Block(Collections.singletonList(logStatement), false));
}
示例6: visitTry
import com.sun.source.tree.CatchTree; //导入依赖的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();
}
}
示例7: assignmentToCatchBlockParameter
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToCatchBlockParameter", options=Options.QUERY) //NOI18N
@TriggerTreeKind(Kind.CATCH)
public static List<ErrorDescription> assignmentToCatchBlockParameter(HintContext context) {
final Trees trees = context.getInfo().getTrees();
final TreePath catchPath = context.getPath();
final Element param = trees.getElement(TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getParameter()));
if (param == null || param.getKind() != ElementKind.EXCEPTION_PARAMETER) {
return null;
}
final TreePath block = TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getBlock());
final List<TreePath> paths = new LinkedList<TreePath>();
new AssignmentFinder(trees, param).scan(block, paths);
final List<ErrorDescription> ret = new ArrayList<ErrorDescription>(paths.size());
for (TreePath path : paths) {
ret.add(ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToCatchBlockParameter", param.getSimpleName()))); //NOI18N
}
return ret;
}
示例8: visitCatchClause
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
/**
* Helper method for {@link CatchTree}s.
*/
private void visitCatchClause(CatchTree node, AllowTrailingBlankLine allowTrailingBlankLine) {
sync(node);
builder.space();
token("catch");
builder.space();
token("(");
builder.open(plusFour);
VariableTree ex = node.getParameter();
if (ex.getType().getKind() == UNION_TYPE) {
builder.open(ZERO);
visitUnionType(ex);
builder.close();
} else {
// TODO(cushon): don't break after here for consistency with for, while, etc.
builder.breakToFill();
builder.open(ZERO);
scan(ex, null);
builder.close();
}
builder.close();
token(")");
builder.space();
visitBlock(
node.getBlock(), CollapseEmptyOrNot.NO, AllowLeadingBlankLine.YES, allowTrailingBlankLine);
}
示例9: visitCatch
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
@Override
public Void visitCatch(CatchTree node, Void p) {
TreePath param = new TreePath(getCurrentPath(), node.getParameter());
Element ex = trees.getElement(param);
validateUnionTypeInfo(ex);
if (ex.getSimpleName().contentEquals("ex")) {
assertTrue(ex.getKind() == ElementKind.EXCEPTION_PARAMETER, "Expected EXCEPTION_PARAMETER - found " + ex.getKind());
for (Element e : types.asElement(trees.getLub(node)).getEnclosedElements()) {
Member m = e.getAnnotation(Member.class);
if (m != null) {
assertTrue(e.getKind() == m.value(), "Expected " + m.value() + " - found " + e.getKind());
}
}
assertTrue(assertionCount == 9, "Expected 9 assertions - found " + assertionCount);
}
return super.visitCatch(node, p);
}
示例10: visitTry
import com.sun.source.tree.CatchTree; //导入依赖的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;
}
示例11: testVisitCatch
import com.sun.source.tree.CatchTree; //导入依赖的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());
}
示例12: testVisitCatchNotReformat
import com.sun.source.tree.CatchTree; //导入依赖的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());
}
示例13: matchTry
import com.sun.source.tree.CatchTree; //导入依赖的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();
}
示例14: rethrowFix
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
private Optional<Fix> rethrowFix(ImmutableList<CatchTree> catchBlocks, VisitorState state) {
SuggestedFix.Builder fix = SuggestedFix.builder();
catchBlocks.forEach(
c -> {
// e.g.
// fail("message") -> throw new AssertionError("message", cause);
// assertWithMessage("message format %s", 42) ->
// throw new AssertionError(String.format("message format %s", 42), cause);
StatementTree statementTree = getOnlyElement(c.getBlock().getStatements());
MethodInvocationTree methodInvocationTree =
(MethodInvocationTree) ((ExpressionStatementTree) statementTree).getExpression();
String message = null;
if (message == null && !methodInvocationTree.getArguments().isEmpty()) {
message = getMessageOrFormat(methodInvocationTree, state);
}
if (message != null) {
// only catch and rethrow to add additional context, not for raw `fail()` calls
fix.replace(
statementTree,
String.format(
"throw new AssertionError(%s, %s);", message, c.getParameter().getName()));
}
});
return fix.isEmpty() ? Optional.empty() : Optional.of(fix.build());
}
示例15: catchVariableIsUsed
import com.sun.source.tree.CatchTree; //导入依赖的package包/类
private boolean catchVariableIsUsed(CatchTree c) {
VarSymbol sym = ASTHelpers.getSymbol(c.getParameter());
boolean[] found = {false};
c.getBlock()
.accept(
new TreeScanner<Void, Void>() {
@Override
public Void visitIdentifier(IdentifierTree node, Void aVoid) {
if (Objects.equals(sym, ASTHelpers.getSymbol(node))) {
found[0] = true;
}
return super.visitIdentifier(node, aVoid);
}
},
null);
return found[0];
}