@Override public void visitThrowStatement(PsiThrowStatement statement) { super.visitThrowStatement(statement); final PsiCatchSection catchSection = PsiTreeUtil.getParentOfType(statement, PsiCatchSection.class, true, PsiClass.class); if (catchSection == null) { return; } final PsiParameter parameter = catchSection.getParameter(); if (parameter == null) { return; } @NonNls final String parameterName = parameter.getName(); if (PsiUtil.isIgnoredName(parameterName)) { return; } final PsiExpression exception = statement.getException(); if (exception == null) { return; } if (ignoreCantWrap) { final PsiType thrownType = exception.getType(); if (thrownType instanceof PsiClassType) { final PsiClassType classType = (PsiClassType) thrownType; final PsiClass exceptionClass = classType.resolve(); if (exceptionClass != null) { final PsiMethod[] constructors = exceptionClass.getConstructors(); final PsiClassType throwableType = TypeUtils.getType(CommonClassNames.JAVA_LANG_THROWABLE, statement); boolean canWrap = false; outer: for (PsiMethod constructor : constructors) { final PsiParameterList parameterList = constructor.getParameterList(); final PsiParameter[] parameters = parameterList.getParameters(); for (PsiParameter constructorParameter : parameters) { final PsiType type = constructorParameter.getType(); if (throwableType.equals(type)) { canWrap = true; break outer; } } } if (!canWrap) { return; } } } } final ReferenceFinder visitor = new ReferenceFinder(parameter); exception.accept(visitor); if (visitor.usesParameter()) { return; } registerStatementError(statement); }
@Override protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiType thrownType = myThrown.getType(); if (thrownType == null) { return; } final PsiElement typeElement = descriptor.getPsiElement(); if (typeElement == null) { return; } final PsiElement catchParameter = typeElement.getParent(); if (!(catchParameter instanceof PsiParameter)) { return; } final PsiElement catchBlock = ((PsiParameter) catchParameter).getDeclarationScope(); if (!(catchBlock instanceof PsiCatchSection)) { return; } final PsiCatchSection myBeforeCatchSection = (PsiCatchSection) catchBlock; final PsiTryStatement myTryStatement = myBeforeCatchSection.getTryStatement(); final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); final String name = codeStyleManager.suggestUniqueVariableName("e", myTryStatement.getTryBlock(), false); final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiCatchSection section = factory.createCatchSection(thrownType, name, myTryStatement); final PsiCatchSection element = (PsiCatchSection) myTryStatement.addBefore(section, myBeforeCatchSection); codeStyleManager.shortenClassReferences(element); if (isOnTheFly()) { final PsiCodeBlock newBlock = element.getCatchBlock(); assert newBlock != null; final TextRange range = SurroundWithUtil.getRangeToSelect(newBlock); final PsiFile file = element.getContainingFile(); final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor == null) { return; } final Document document = PsiDocumentManager.getInstance(project).getDocument(file); if (editor.getDocument() != document) { return; } editor.getCaretModel().moveToOffset(range.getStartOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset()); } }
@Override protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement element = descriptor.getPsiElement(); final PsiElement parent = element.getParent(); if (!(parent instanceof PsiCatchSection)) { return; } final PsiCatchSection catchSection = (PsiCatchSection) parent; final PsiParameter parameter = catchSection.getParameter(); if (parameter == null) { return; } final PsiIdentifier identifier = parameter.getNameIdentifier(); if (identifier == null) { return; } final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiIdentifier newIdentifier = factory.createIdentifier("ignored"); identifier.replace(newIdentifier); }
private void checkCatchSection(PsiCatchSection section) { final PsiCodeBlock block = section.getCatchBlock(); if (block == null || !isCatchBlockEmpty(block)) { return; } final PsiParameter parameter = section.getParameter(); if (parameter == null) { return; } final PsiIdentifier identifier = parameter.getNameIdentifier(); if (identifier == null) { return; } @NonNls final String parameterName = parameter.getName(); if (m_ignoreIgnoreParameter && ("ignore".equals(parameterName) || "ignored".equals(parameterName))) { return; } final PsiElement catchToken = section.getFirstChild(); if (catchToken == null) { return; } registerError(catchToken); }