com.sun.source.tree.CatchTree Java Examples
The following examples show how to use
com.sun.source.tree.CatchTree.
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: ModelChecker.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
@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); }
Example #2
Source File: UseSpecificCatch.java From netbeans with Apache License 2.0 | 6 votes |
@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)); }
Example #3
Source File: TryCatchFinally.java From netbeans with Apache License 2.0 | 6 votes |
@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; }
Example #4
Source File: ModelChecker.java From hottub with GNU General Public License v2.0 | 6 votes |
@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); }
Example #5
Source File: ModelChecker.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
@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); }
Example #6
Source File: ExtraCatch.java From netbeans with Apache License 2.0 | 6 votes |
@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)); } }
Example #7
Source File: MethodExitDetector.java From netbeans with Apache License 2.0 | 6 votes |
@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); }
Example #8
Source File: ModelChecker.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@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); }
Example #9
Source File: MagicSurroundWithTryCatchFix.java From netbeans with Apache License 2.0 | 6 votes |
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)); }
Example #10
Source File: ModelChecker.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@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); }
Example #11
Source File: ModelChecker.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
@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); }
Example #12
Source File: JavaInputAstVisitor.java From java-n-IDE-for-Android with Apache License 2.0 | 6 votes |
/** * 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); }
Example #13
Source File: Utilities.java From netbeans with Apache License 2.0 | 6 votes |
@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(); } }
Example #14
Source File: JavaInputAstVisitor.java From javaide with GNU General Public License v3.0 | 6 votes |
/** * 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); }
Example #15
Source File: JavaInputAstVisitor.java From google-java-format with Apache License 2.0 | 6 votes |
/** 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); }
Example #16
Source File: AssignmentIssues.java From netbeans with Apache License 2.0 | 6 votes |
@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; }
Example #17
Source File: ModelChecker.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
@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); }
Example #18
Source File: ModelChecker.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
@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); }
Example #19
Source File: JavacTrees.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Override public TypeMirror getLub(CatchTree tree) { JCCatch ct = (JCCatch) tree; JCVariableDecl v = ct.param; if (v.type != null && v.type.getKind() == TypeKind.UNION) { UnionClassType ut = (UnionClassType) v.type; return ut.getLub(); } else { return v.type; } }
Example #20
Source File: JavacTrees.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override @DefinedBy(Api.COMPILER_TREE) public TypeMirror getLub(CatchTree tree) { JCCatch ct = (JCCatch) tree; JCVariableDecl v = ct.param; if (v.type != null && v.type.getKind() == TypeKind.UNION) { UnionClassType ut = (UnionClassType) v.type; return ut.getLub(); } else { return v.type; } }
Example #21
Source File: TreeNode.java From netbeans with Apache License 2.0 | 5 votes |
@Override public Void visitCatch(CatchTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingType(below); addCorrespondingComments(below); super.visitCatch(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
Example #22
Source File: BroadCatchBlock.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected void performRewrite(TransformationContext ctx) throws Exception { CatchTree oldTree = (CatchTree)ctx.getPath().getLeaf(); WorkingCopy wcopy = ctx.getWorkingCopy(); Tree varType = oldTree.getParameter().getType(); Tree newVarType = wcopy.getTreeMaker().Type(newCatchType.resolve(wcopy)); wcopy.rewrite(varType, newVarType); }
Example #23
Source File: JavacTrees.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Override public TypeMirror getLub(CatchTree tree) { JCCatch ct = (JCCatch) tree; JCVariableDecl v = ct.param; if (v.type != null && v.type.getKind() == TypeKind.UNION) { UnionClassType ut = (UnionClassType) v.type; return ut.getLub(); } else { return v.type; } }
Example #24
Source File: FinalizeDoesNotCallSuper.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected void performRewrite(TransformationContext ctx) { WorkingCopy wc = ctx.getWorkingCopy(); final TreeMaker tm = wc.getTreeMaker(); TreePath tp = ctx.getPath(); final BlockTree oldBody = ((MethodTree)tp.getLeaf()).getBody(); if (oldBody == null) { return; } final List<StatementTree> newStatements = new ArrayList<StatementTree>(2); BlockTree superFinalize = tm.Block( Collections.singletonList( tm.ExpressionStatement( tm.MethodInvocation(Collections.<ExpressionTree>emptyList(), tm.MemberSelect( tm.Identifier(SUPER), FINALIZE), Collections.<ExpressionTree>emptyList()))), false); if (oldBody.getStatements().isEmpty()) { wc.rewrite(oldBody, superFinalize); } else { TryTree soleTry = soleTryWithoutFinally(oldBody); if (soleTry != null) { wc.rewrite(soleTry, tm.Try(soleTry.getBlock(), soleTry.getCatches(), superFinalize)); } else { wc.rewrite(oldBody, tm.Block(Collections.singletonList(tm.Try(oldBody, Collections.<CatchTree>emptyList(), superFinalize)), false)); } } }
Example #25
Source File: Flow.java From netbeans with Apache License 2.0 | 5 votes |
public Boolean visitCatch(CatchTree node, ConstructorData p) { inParameters = true; scan(node.getParameter(), p); inParameters = false; scan(node.getBlock(), p); return null; }
Example #26
Source File: JavacTrees.java From javaide with GNU General Public License v3.0 | 5 votes |
@Override public TypeMirror getLub(CatchTree tree) { JCCatch ct = (JCCatch) tree; JCVariableDecl v = ct.param; if (v.type != null && v.type.getKind() == TypeKind.UNION) { UnionClassType ut = (UnionClassType) v.type; return ut.getLub(); } else { return v.type; } }
Example #27
Source File: FmtImports.java From netbeans with Apache License 2.0 | 5 votes |
protected void doModification(ResultIterator resultIterator) throws Exception { WorkingCopy copy = WorkingCopy.get(resultIterator.getParserResult()); copy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = copy.getCompilationUnit(); ClassTree ct = (ClassTree) cut.getTypeDecls().get(0); MethodTree mt = (MethodTree) ct.getMembers().get(1); TreeMaker treeMaker = copy.getTreeMaker(); BlockTree bt = mt.getBody(); StatementTree stmt = treeMaker.Variable(treeMaker.Modifiers(EnumSet.noneOf(Modifier.class)), "is", treeMaker.QualIdent("java.io.InputStream"), treeMaker.Literal(null)); //NOI18N bt = treeMaker.addBlockStatement(bt, stmt); BlockTree tryBlock = treeMaker.Block(Collections.<StatementTree>emptyList(), false); ExpressionTree et = treeMaker.NewClass(null, Collections.<ExpressionTree>emptyList(), treeMaker.QualIdent("java.io.File"), Collections.singletonList(treeMaker.Literal("test.txt")), null); //NOI18N stmt = treeMaker.Variable(treeMaker.Modifiers(EnumSet.noneOf(Modifier.class)), "f", treeMaker.QualIdent("java.io.File"), et); //NOI18N tryBlock = treeMaker.addBlockStatement(tryBlock, stmt); et = treeMaker.NewClass(null, Collections.<ExpressionTree>emptyList(), treeMaker.QualIdent("java.io.FileInputStream"), Collections.singletonList(treeMaker.Identifier("f")), null); //NOI18N et = treeMaker.Assignment(treeMaker.Identifier("is"), et); //NOI18N tryBlock = treeMaker.addBlockStatement(tryBlock, treeMaker.ExpressionStatement(et)); et = treeMaker.MemberSelect(treeMaker.Identifier("is"), "read"); //NOI18N et = treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), et, Collections.<ExpressionTree>emptyList()); stmt = treeMaker.Try(treeMaker.Block(Collections.singletonList(treeMaker.ExpressionStatement(et)), false), Collections.<CatchTree>emptyList(), null); et = treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), treeMaker.MemberSelect(treeMaker.QualIdent("java.util.logging.Logger"), "getLogger"), Collections.<ExpressionTree>emptyList()); //NOI18N et = treeMaker.addMethodInvocationArgument((MethodInvocationTree) et, treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), treeMaker.MemberSelect(treeMaker.MemberSelect(treeMaker.QualIdent("org.netbeans.samples.ClassA"), "class"), "getName"), Collections.<ExpressionTree>emptyList())); //NOI18N et = treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), treeMaker.MemberSelect(et, "log"), Collections.<ExpressionTree>emptyList()); //NOI18N et = treeMaker.addMethodInvocationArgument((MethodInvocationTree) et, treeMaker.MemberSelect(treeMaker.QualIdent("java.util.logging.Logger"), "SEVERE")); //NOI18N et = treeMaker.addMethodInvocationArgument((MethodInvocationTree) et, treeMaker.Literal(null)); et = treeMaker.addMethodInvocationArgument((MethodInvocationTree) et, treeMaker.Identifier("ex")); //NOI18N BlockTree catchBlock = treeMaker.Block(Collections.singletonList(treeMaker.ExpressionStatement(et)), false); stmt = treeMaker.addTryCatch((TryTree) stmt, treeMaker.Catch(treeMaker.Variable(treeMaker.Modifiers(EnumSet.noneOf(Modifier.class)), "ex", treeMaker.QualIdent("java.io.IOException"), null), catchBlock)); //NOI18N tryBlock = treeMaker.addBlockStatement(tryBlock, stmt); et = treeMaker.MemberSelect(treeMaker.Identifier("is"), "close"); //NOI18N et = treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), et, Collections.<ExpressionTree>emptyList()); stmt = treeMaker.Try(treeMaker.Block(Collections.singletonList(treeMaker.ExpressionStatement(et)), false), Collections.<CatchTree>emptyList(), null); stmt = treeMaker.addTryCatch((TryTree) stmt, treeMaker.Catch(treeMaker.Variable(treeMaker.Modifiers(EnumSet.noneOf(Modifier.class)), "ex", treeMaker.QualIdent("java.io.IOException"), null), catchBlock)); //NOI18N stmt = treeMaker.Try(tryBlock, Collections.<CatchTree>emptyList(), treeMaker.Block(Collections.singletonList(stmt), false)); stmt = treeMaker.addTryCatch((TryTree) stmt, treeMaker.Catch(treeMaker.Variable(treeMaker.Modifiers(EnumSet.noneOf(Modifier.class)), "ex", treeMaker.QualIdent("java.io.FileNotFoundException"), null), catchBlock)); //NOI18N bt = treeMaker.addBlockStatement(bt, stmt); copy.rewrite(mt.getBody(), bt); }
Example #28
Source File: AddCatchFix.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected void performRewrite(TransformationContext ctx) throws Exception { TreeMaker make = ctx.getWorkingCopy().getTreeMaker(); TryTree tt = (TryTree) ctx.getPath().getLeaf(); List<CatchTree> catches = new ArrayList<CatchTree>(); catches.addAll(tt.getCatches()); catches.addAll(MagicSurroundWithTryCatchFix.createCatches(ctx.getWorkingCopy(), make, thandles, ctx.getPath())); ctx.getWorkingCopy().rewrite(tt, make.Try(tt.getResources(), tt.getBlock(), catches, tt.getFinallyBlock())); }
Example #29
Source File: MagicSurroundWithTryCatchFix.java From netbeans with Apache License 2.0 | 5 votes |
public @Override Void visitBlock(BlockTree bt, Void p) { List<CatchTree> catches = createCatches(info, make, thandles, statement); //#89379: if inside a constructor, do not wrap the "super"/"this" call: //please note that the "super" or "this" call is supposed to be always //in the constructor body BlockTree toUse = bt; StatementTree toKeep = null; Tree parent = getCurrentPath().getParentPath().getLeaf(); if (parent.getKind() == Kind.METHOD && bt.getStatements().size() > 0) { MethodTree mt = (MethodTree) parent; if (mt.getReturnType() == null) { toKeep = bt.getStatements().get(0); toUse = make.Block(bt.getStatements().subList(1, bt.getStatements().size()), false); } } if (!streamAlike) { info.rewrite(bt, createBlock(bt.isStatic(), toKeep, make.Try(toUse, catches, null))); } else { VariableTree originalDeclaration = (VariableTree) statement.getLeaf(); VariableTree declaration = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), originalDeclaration.getName(), originalDeclaration.getType(), make.Identifier("null")); StatementTree assignment = make.ExpressionStatement(make.Assignment(make.Identifier(originalDeclaration.getName()), originalDeclaration.getInitializer())); BlockTree finallyTree = make.Block(Collections.singletonList(createFinallyCloseBlockStatement(originalDeclaration)), false); info.rewrite(originalDeclaration, assignment); info.rewrite(bt, createBlock(bt.isStatic(), toKeep, declaration, make.Try(toUse, catches, finallyTree))); } return null; }
Example #30
Source File: MagicSurroundWithTryCatchFix.java From netbeans with Apache License 2.0 | 5 votes |
static List<CatchTree> createCatches(WorkingCopy info, TreeMaker make, List<TypeMirrorHandle> thandles, TreePath currentPath) { String name = inferName(info, currentPath); List<CatchTree> catches = new ArrayList<CatchTree>(); for (TypeMirrorHandle th : thandles) { TypeMirror tm = th.resolve(info); if(tm != null) catches.add(createCatch(info, make, currentPath, name, tm)); } return catches; }