private static List<PsiStatement> getFinallyStatements(@Nullable SourcePosition position) { if (position == null) { return Collections.emptyList(); } List<PsiStatement> res = new ArrayList<PsiStatement>(); PsiElement element = position.getFile().findElementAt(position.getOffset()); PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class); while (tryStatement != null) { PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock(); if (finallyBlock != null) { ContainerUtil.addAll(res, finallyBlock.getStatements()); } tryStatement = PsiTreeUtil.getParentOfType(tryStatement, PsiTryStatement.class); } return res; }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); final PsiTryStatement parentTry = PsiTreeUtil.getParentOfType(statement, PsiTryStatement.class); if (parentTry == null) { return; } final PsiCodeBlock tryBlock = parentTry.getTryBlock(); if (tryBlock == null) { return; } if (!PsiTreeUtil.isAncestor(tryBlock, statement, true)) { return; } if (NestedSynchronizedStatementInspection.isNestedStatement(statement, PsiTryStatement.class)) { registerStatementError(statement); } }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); final PsiCodeBlock finallyBlock = statement.getFinallyBlock(); if (finallyBlock == null) { return; } if (ControlFlowUtils.codeBlockMayCompleteNormally(finallyBlock)) { return; } final PsiElement[] children = statement.getChildren(); for (final PsiElement child : children) { final String childText = child.getText(); if (PsiKeyword.FINALLY.equals(childText)) { registerError(child); return; } } }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(!(psiElement instanceof PsiTryStatement)) { return; } PsiTryStatement tryStatement = (PsiTryStatement) psiElement; final Document doc = editor.getDocument(); PsiElement body = tryStatement.getTryBlock(); if(body != null) { return; } doc.insertString(tryStatement.getTextRange().getEndOffset(), "{}"); }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) { if (!FileModificationService.getInstance().prepareFileForWrite(myCatchSection.getContainingFile())) return; try { PsiTryStatement statement = myCatchSection.getTryStatement(); statement.addBefore(myCatchSection, myMoveBeforeSection); myCatchSection.delete(); } catch (IncorrectOperationException e) { LOG.error(e); } }
@Override protected void doUnwrap(final PsiElement element, final Context context) throws IncorrectOperationException { final PsiTryStatement trySt = (PsiTryStatement)element; context.extractFromCodeBlock(trySt.getTryBlock(), trySt); context.extractFromCodeBlock(trySt.getFinallyBlock(), trySt); context.delete(trySt); }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = new ArrayList<TextRange>(); final PsiElement parent = e.getParent(); if (parent instanceof PsiTryStatement) { final PsiTryStatement tryStatement = (PsiTryStatement)parent; final PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock(); if (finallyBlock != null) { result.add(new TextRange(e.getTextRange().getStartOffset(), finallyBlock.getTextRange().getEndOffset())); } } return result; }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); final PsiCodeBlock finallyBlock = statement.getTryBlock(); if (finallyBlock == null) { return; } if (finallyBlock.getStatements().length != 0) { return; } registerStatementError(statement); }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); if (FileTypeUtils.isInJsp(statement.getContainingFile())) { return; } final PsiCodeBlock finallyBlock = statement.getTryBlock(); if (finallyBlock == null) { return; } if (finallyBlock.getStatements().length != 0) { return; } registerStatementError(statement); }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); final PsiTryStatement parentTry = PsiTreeUtil.getParentOfType(statement, PsiTryStatement.class); if (parentTry == null) { return; } final PsiCodeBlock tryBlock = parentTry.getTryBlock(); if (tryBlock == null) { return; } if (!PsiTreeUtil.isAncestor(tryBlock, statement, true)) { return; } final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(statement, PsiMethod.class); final PsiMethod containingContainingMethod = PsiTreeUtil.getParentOfType(parentTry, PsiMethod.class); if (containingMethod == null || containingContainingMethod == null || !containingMethod.equals(containingContainingMethod)) { return; } registerStatementError(statement); }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); if (m_ignoreTestCases && TestUtils.isInTestCode(statement)) { return; } final PsiCatchSection[] catchSections = statement.getCatchSections(); for (PsiCatchSection catchSection : catchSections) { checkCatchSection(catchSection); } }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); /*if (JspPsiUtil.isInJspFile(statement.getContainingFile())) { return; } */ final PsiCodeBlock finallyBlock = statement.getTryBlock(); if (finallyBlock == null) { return; } if (finallyBlock.getStatements().length != 0) { return; } registerStatementError(statement); }
@Override public void visitTryStatement(PsiTryStatement statement) { mVisitor.report("PsiTryStatement", statement.getText(), statement); super.visitStatement(statement); }
@Override public boolean isApplicableTo(PsiElement e) { return e instanceof PsiTryStatement; }
private static boolean tryHasSeveralCatchesOrResourcesDefined(PsiElement el) { PsiTryStatement tryStatement = (PsiTryStatement)el.getParent(); return tryStatement.getCatchBlocks().length > 1 || tryStatement.getResourceList() != null; }
private boolean tryHasSeveralCatches(PsiElement el) { return ((PsiTryStatement)el.getParent()).getCatchBlocks().length > 1; }
TryCatch(PsiTryStatement tryStatement, Map<PsiCatchSection, ControlFlow.ControlFlowOffset> clauses) { super(tryStatement); myTryStatement = tryStatement; myClauses = clauses; }
public PsiTryStatement getTryStatement() { return myTryStatement; }