2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.siyeh
.ig
.naming
;
18 import com
.intellij
.codeInspection
.ProblemDescriptor
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.search
.GlobalSearchScope
;
22 import com
.intellij
.util
.IncorrectOperationException
;
23 import com
.siyeh
.InspectionGadgetsBundle
;
24 import com
.siyeh
.ig
.BaseInspection
;
25 import com
.siyeh
.ig
.BaseInspectionVisitor
;
26 import com
.siyeh
.ig
.InspectionGadgetsFix
;
27 import com
.siyeh
.ig
.fixes
.RenameFix
;
28 import com
.siyeh
.ig
.psiutils
.ClassUtils
;
29 import org
.jetbrains
.annotations
.NonNls
;
30 import org
.jetbrains
.annotations
.NotNull
;
32 public class NonExceptionNameEndsWithExceptionInspection
33 extends BaseInspection
{
36 public String
getDisplayName() {
37 return InspectionGadgetsBundle
.message(
38 "non.exception.name.ends.with.exception.display.name");
42 protected String
buildErrorString(Object
... infos
) {
43 return InspectionGadgetsBundle
.message(
44 "non.exception.name.ends.with.exception.problem.descriptor");
48 protected InspectionGadgetsFix
[] buildFixes(Object
... infos
) {
49 final String name
= (String
) infos
[0];
50 return new InspectionGadgetsFix
[] {new RenameFix(),
51 new ExtendExceptionFix(name
)};
54 private static class ExtendExceptionFix
extends InspectionGadgetsFix
{
56 private final String name
;
58 public ExtendExceptionFix(String name
) {
63 public String
getName() {
64 return InspectionGadgetsBundle
.message(
65 "non.exception.name.ends.with.exception.quickfix", name
);
69 protected void doFix(Project project
, ProblemDescriptor descriptor
)
70 throws IncorrectOperationException
{
71 final PsiElement element
= descriptor
.getPsiElement();
72 final PsiElement parent
= element
.getParent();
73 if (!(parent
instanceof PsiClass
)) {
76 final PsiClass aClass
= (PsiClass
) parent
;
77 final PsiReferenceList extendsList
= aClass
.getExtendsList();
78 if (extendsList
== null) {
81 final JavaPsiFacade facade
= JavaPsiFacade
.getInstance(project
);
82 final PsiElementFactory factory
= facade
.getElementFactory();
83 final GlobalSearchScope scope
= aClass
.getResolveScope();
84 final PsiJavaCodeReferenceElement reference
=
85 factory
.createReferenceElementByFQClassName(
86 "java.lang.Exception", scope
);
87 extendsList
.add(reference
);
92 protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
97 public BaseInspectionVisitor
buildVisitor() {
98 return new NonExceptionNameEndsWithExceptionVisitor();
101 private static class NonExceptionNameEndsWithExceptionVisitor
102 extends BaseInspectionVisitor
{
104 @Override public void visitClass(@NotNull PsiClass aClass
) {
105 // no call to super, so it doesn't drill down into inner classes
106 final String className
= aClass
.getName();
107 if (className
== null) {
110 @NonNls final String exception
= "Exception";
111 if (!className
.endsWith(exception
)) {
114 if (ClassUtils
.isSubclass(aClass
, "java.lang.Exception")) {
117 registerClassError(aClass
, className
);