@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 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); }