diff --git a/plugins/InspectionGadgets/src/META-INF/plugin.xml b/plugins/InspectionGadgets/src/META-INF/plugin.xml index 0c720b913024..dffa6ea0e1fa 100644 --- a/plugins/InspectionGadgets/src/META-INF/plugin.xml +++ b/plugins/InspectionGadgets/src/META-INF/plugin.xml @@ -867,6 +867,11 @@ + + + new #ref() without arguments diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/NewExceptionWithoutArgumentsInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/NewExceptionWithoutArgumentsInspection.java new file mode 100644 index 000000000000..b6453a340e03 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/NewExceptionWithoutArgumentsInspection.java @@ -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); + } + } +} diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/NewExceptionWithoutArguments.html b/plugins/InspectionGadgets/src/inspectionDescriptions/NewExceptionWithoutArguments.html new file mode 100644 index 000000000000..dd38673c63b7 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/NewExceptionWithoutArguments.html @@ -0,0 +1,8 @@ + + +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. +

+New in 11, Powered by InspectionGadgets + + \ No newline at end of file