New "Exception constructor called without arguments" inspection added IDEA-75169 (feature request: constructor of exception class with no parameters is called)

This commit is contained in:
Bas Leijdekkers
2011-10-05 16:18:13 +02:00
parent d6179a5e5b
commit 9572436e67
4 changed files with 93 additions and 0 deletions
@@ -867,6 +867,11 @@
<localInspection language="JAVA" shortName="UnusedCatchParameter" bundle="com.siyeh.InspectionGadgetsBundle" key="unused.catch.parameter.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.error.handling" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.errorhandling.UnusedCatchParameterInspection"/>
<localInspection language="JAVA" shortName="NewExceptionWithoutArguments" bundle="com.siyeh.InspectionGadgetsBundle" key="new.exception.without.arguments.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.error.handling" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.errorhandling.NewExceptionWithoutArgumentsInspection"/>
<localInspection language="JAVA" suppressId="FinalizeDoesntCallSuperFinalize" shortName="FinalizeCallsSuperFinalize"
bundle="com.siyeh.InspectionGadgetsBundle" key="finalize.doesnt.call.super.display.name"
groupBundle="messages.InspectionsBundle" groupKey="group.names.finalization.issues" enabledByDefault="true"
@@ -1949,3 +1949,5 @@ ignored.junit.test.method.problem.descriptor=Test method ''{0}()'' annotated wit
unclear.binary.expression.display.name=Unclear binary expression
unclear.binary.expression.problem.descriptor=Expression could use clarifying parentheses #loc
unclear.binary.expression.quickfix=Add clarifying parentheses
new.exception.without.arguments.display.name=Exception constructor called without arguments
new.exception.without.arguments.problem.descriptor=<code>new #ref()</code> without arguments
@@ -0,0 +1,78 @@
/*
* Copyright 2011 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.errorhandling;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.siyeh.InspectionGadgetsBundle;import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
public class NewExceptionWithoutArgumentsInspection extends BaseInspection {
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message("new.exception.without.arguments.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message("new.exception.without.arguments.problem.descriptor");
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new NewExceptionWithoutArgumentsVisitor();
}
private static class NewExceptionWithoutArgumentsVisitor extends BaseInspectionVisitor {
@Override
public void visitNewExpression(PsiNewExpression expression) {
super.visitNewExpression(expression);
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) {
return;
}
final PsiExpression[] expressions = argumentList.getExpressions();
if (expressions.length != 0) {
return;
}
final PsiJavaCodeReferenceElement classReference = expression.getClassReference();
if (classReference == null) {
return;
}
final PsiElement target = classReference.resolve();
if (!(target instanceof PsiClass)) {
return;
}
final PsiClass aClass = (PsiClass)target;
final GlobalSearchScope resolveScope = expression.getResolveScope();
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(expression.getProject());
final PsiClass exceptionClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_EXCEPTION, resolveScope);
if (exceptionClass == null) {
return;
}
if (!aClass.isInheritor(exceptionClass, true)) {
return;
}
registerNewExpressionError(expression);
}
}
}
@@ -0,0 +1,8 @@
<html>
<body>
This inspection reports construction of exceptions without arguments. When an exception is constructed without arguments it contains no
information about the fault that happened, which makes debugging needlessly hard.
<p>
<small>New in 11, Powered by InspectionGadgets</small>
</body>
</html>