@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (psiElement instanceof PsiThrowStatement) { PsiThrowStatement throwStatement = (PsiThrowStatement) psiElement; if (throwStatement.getException() != null && startLine(editor, throwStatement) == startLine(editor, throwStatement.getException())) { return; } final int startOffset = throwStatement.getTextRange().getStartOffset(); if (throwStatement.getException() != null) { editor.getDocument().insertString(startOffset + "throw".length(), ";"); } processor.registerUnresolvedError(startOffset + "throw".length()); } }
@Override @Nullable public UsageTarget[] getTargets(Editor editor, final PsiFile file) { if (editor == null || file == null) return null; PsiElement element = file.findElementAt(TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset())); if (element == null) return null; if (element instanceof PsiKeyword && PsiKeyword.THROWS.equals(element.getText())) { return new UsageTarget[]{new PsiElement2UsageTargetAdapter(element)}; } final PsiElement parent = element.getParent(); if (parent instanceof PsiThrowStatement) { return new UsageTarget[] {new PsiElement2UsageTargetAdapter(parent)}; } return null; }
@Override public void visitThrowStatement(PsiThrowStatement statement) { super.visitThrowStatement(statement); final PsiExpression exception = statement.getException(); if (exception == null) { return; } final PsiType type = exception.getType(); if (type == null) { return; } final String text = type.getCanonicalText(); if (exceptions.contains(text)) { registerStatementError(statement, type); } }
@Override @Nullable public UsageTarget[] getTargets(Editor editor, final PsiFile file) { if (editor == null || file == null) return null; PsiElement element = file.findElementAt(TargetElementUtilBase.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset())); if (element == null) return null; if (element instanceof PsiKeyword && PsiKeyword.THROWS.equals(element.getText())) { return new UsageTarget[]{new PsiElement2UsageTargetAdapter(element)}; } final PsiElement parent = element.getParent(); if (parent instanceof PsiThrowStatement) { return new UsageTarget[] {new PsiElement2UsageTargetAdapter(parent)}; } return null; }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(psiElement instanceof PsiThrowStatement) { PsiThrowStatement throwStatement = (PsiThrowStatement) psiElement; if(throwStatement.getException() != null && startLine(editor, throwStatement) == startLine(editor, throwStatement.getException())) { return; } final int startOffset = throwStatement.getTextRange().getStartOffset(); if(throwStatement.getException() != null) { editor.getDocument().insertString(startOffset + "throw".length(), ";"); } processor.registerUnresolvedError(startOffset + "throw".length()); } }
@Override public void visitThrowStatement(PsiThrowStatement statement) { super.visitThrowStatement(statement); if (!ControlFlowUtils.isInFinallyBlock(statement)) { return; } registerStatementError(statement); }
private static PsiClassType getRuntimeExceptionAtCaret(Editor editor, PsiFile file) { PsiElement element = elementAtCaret(editor, file); if (element == null) return null; PsiThrowStatement expression = PsiTreeUtil.getParentOfType(element, PsiThrowStatement.class); if (expression == null) return null; PsiExpression exception = expression.getException(); if (exception == null) return null; PsiType type = exception.getType(); if (!(type instanceof PsiClassType)) return null; if (!ExceptionUtil.isUncheckedException((PsiClassType)type)) return null; return (PsiClassType)type; }
@Override public void visitThrowStatement(PsiThrowStatement statement) { mVisitor.report("PsiThrowStatement", statement.getText(), statement); super.visitStatement(statement); }
/** * Handles situations like the one below: * <pre> * void foo(int i) { * if (i < 0) { * return;[caret] * } * } * </pre> * * <b>Output:</b> * <pre> * void foo(int i) { * if (i < 0) { * return; * } * [caret] * } * </pre> * * @param element * @return */ private static boolean isControlFlowBreak(@Nullable PsiElement element) { return element instanceof PsiReturnStatement || element instanceof PsiThrowStatement; }
/** * Handles situations like the one below: * <pre> * void foo(int i) { * if (i < 0) { * return;[caret] * } * } * </pre> * <p> * <b>Output:</b> * <pre> * void foo(int i) { * if (i < 0) { * return; * } * [caret] * } * </pre> * * @param element * @return */ private static boolean isControlFlowBreak(@Nullable PsiElement element) { return element instanceof PsiReturnStatement || element instanceof PsiThrowStatement; }