@Override public void visitCatchSection(PsiCatchSection section) { super.visitCatchSection(section); final PsiParameter parameter = section.getParameter(); if (parameter == null) { return; } final PsiTypeElement typeElement = parameter.getTypeElement(); if (typeElement == null) { return; } final PsiTypeElement[] childTypeElements = PsiTreeUtil.getChildrenOfType(typeElement, PsiTypeElement.class); if (childTypeElements != null) { for (PsiTypeElement childTypeElement : childTypeElements) { checkTypeElement(childTypeElement); } } else { checkTypeElement(typeElement); } }
@Override public void visitParameter(@NotNull PsiParameter parameter) { super.visitParameter(parameter); if (parameter.getDeclarationScope() instanceof PsiCatchSection) { return; } final PsiTypeElement typeElement = parameter.getTypeElement(); if (typeElement == null) { return; } if (!ConcreteClassUtil.typeIsConcreteClass(typeElement, ignoreAbstractClasses)) { return; } final String variableName = parameter.getName(); registerError(typeElement, variableName); }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(!(psiElement instanceof PsiCatchSection)) { return; } PsiCatchSection catchSection = (PsiCatchSection) psiElement; final Document doc = editor.getDocument(); PsiCodeBlock body = catchSection.getCatchBlock(); if(body != null && startLine(doc, body) == startLine(doc, catchSection)) { return; } final PsiJavaToken rParenth = catchSection.getRParenth(); if(rParenth == null) { return; } doc.insertString(rParenth.getTextRange().getEndOffset(), "{}"); }
@Override public void visitParameter(@NotNull PsiParameter variable) { final PsiElement scope = variable.getDeclarationScope(); if (scope instanceof PsiCatchSection || scope instanceof PsiForeachStatement) { return; } final String name = variable.getName(); if (name == null || isValid(name)) { return; } registerVariableError(variable, name); }
@NotNull private List<DfaInstructionState> processCatches(Trap.TryCatch tryCatch, DfaValue thrownValue, FList<Trap> traps) { List<DfaInstructionState> result = new ArrayList<>(); for(Map.Entry<PsiCatchSection, ControlFlow.ControlFlowOffset> entry : tryCatch.getClauses().entrySet()) { PsiCatchSection catchSection = entry.getKey(); ControlFlow.ControlFlowOffset jumpOffset = entry.getValue(); PsiParameter param = catchSection.getParameter(); if(param == null) { continue; } if(throwableState == null) { throwableState = initVariableState(param, thrownValue); } for(DfaTypeValue caughtType : allCaughtTypes(param)) { DfaVariableState varState = throwableState.withInstanceofValue(caughtType); if(varState != null) { result.add(new DfaInstructionState(myRunner.getInstruction(jumpOffset.getInstructionOffset()), stateForCatchClause(param, varState))); } throwableState = throwableState.withNotInstanceofValue(caughtType); if(throwableState == null) { return result; } } } return ContainerUtil.concat(result, iteration(traps)); }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(psiElement instanceof PsiCatchSection) { final Document doc = editor.getDocument(); final PsiCatchSection catchSection = (PsiCatchSection) psiElement; final int catchStart = catchSection.getTextRange().getStartOffset(); int stopOffset = doc.getLineEndOffset(doc.getLineNumber(catchStart)); final PsiCodeBlock catchBlock = catchSection.getCatchBlock(); if(catchBlock != null) { stopOffset = Math.min(stopOffset, catchBlock.getTextRange().getStartOffset()); } stopOffset = Math.min(stopOffset, catchSection.getTextRange().getEndOffset()); final PsiJavaToken lParenth = catchSection.getLParenth(); if(lParenth == null) { doc.replaceString(catchStart, stopOffset, "catch ()"); processor.registerUnresolvedError(catchStart + "catch (".length()); } else { if(catchSection.getParameter() == null) { processor.registerUnresolvedError(lParenth.getTextRange().getEndOffset()); } if(catchSection.getRParenth() == null) { doc.insertString(stopOffset, ")"); } } } }
@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); } }
private void checkCatchSection(PsiCatchSection section) { final PsiParameter parameter = section.getParameter(); if (parameter == null) { return; } @NonNls final String parameterName = parameter.getName(); final boolean namedIgnore = parameterName.contains("ignore"); final PsiCodeBlock block = section.getCatchBlock(); if (block == null) { return; } if (m_ignoreCatchBlocksWithComments) { final PsiElement[] children = block.getChildren(); for (final PsiElement child : children) { if (child instanceof PsiComment) { return; } } } final CatchParameterUsedVisitor visitor = new CatchParameterUsedVisitor(parameter); block.accept(visitor); if (visitor.isUsed()) { if (namedIgnore) { registerVariableError(parameter, Boolean.valueOf(true)); } return; } else if (namedIgnore) { return; } registerVariableError(parameter, Boolean.valueOf(false)); }
@Override public void visitCatchSection(PsiCatchSection section) { mVisitor.report("PsiCatchSection", section.getText(), section); super.visitElement(section); }
public MoveCatchUpFix(@NotNull PsiCatchSection catchSection, @NotNull PsiCatchSection moveBeforeSection) { this.myCatchSection = catchSection; myMoveBeforeSection = moveBeforeSection; }
@Override public boolean isApplicableTo(PsiElement e) { return e instanceof PsiCatchSection && tryHasSeveralCatchesOrResourcesDefined(e); }
@Override protected boolean isCorrectScope(PsiElement declarationScope) { return declarationScope instanceof PsiCatchSection; }
public MoveCatchUpFix(PsiCatchSection catchSection, PsiCatchSection moveBeforeSection) { this.myCatchSection = catchSection; myMoveBeforeSection = moveBeforeSection; }
@Override public boolean isApplicableTo(PsiElement e) { return e instanceof PsiCatchSection && tryHasSeveralCatches(e); }
@Override public void setupCatchBlock(String exceptionName, PsiElement context, PsiCatchSection element) { throw new UnsupportedOperationException("TODO"); }
TryCatch(PsiTryStatement tryStatement, Map<PsiCatchSection, ControlFlow.ControlFlowOffset> clauses) { super(tryStatement); myTryStatement = tryStatement; myClauses = clauses; }
@NotNull public Map<PsiCatchSection, ControlFlow.ControlFlowOffset> getClauses() { return myClauses; }
public abstract void setupCatchBlock(String exceptionName, PsiElement context, PsiCatchSection element);