|
18 | 18 | import com.intellij.java.language.codeInsight.TestFrameworks; |
19 | 19 | import com.intellij.java.language.psi.*; |
20 | 20 | import com.intellij.java.language.psi.util.InheritanceUtil; |
| 21 | +import consulo.annotation.access.RequiredReadAction; |
21 | 22 | import consulo.annotation.component.ExtensionImpl; |
22 | 23 | import consulo.language.editor.inspection.LocalInspectionToolSession; |
23 | | -import consulo.language.editor.inspection.ProblemHighlightType; |
24 | 24 | import consulo.language.editor.inspection.ProblemsHolder; |
25 | 25 | import consulo.language.psi.PsiElementVisitor; |
26 | 26 | import consulo.localize.LocalizeValue; |
|
37 | 37 | */ |
38 | 38 | @ExtensionImpl |
39 | 39 | public class ClassHasNoToStringMethodInspection extends AbstractToStringInspection { |
40 | | - @Override |
41 | | - @Nonnull |
42 | | - public LocalizeValue getDisplayName() { |
43 | | - return LocalizeValue.localizeTODO("Class does not override 'toString()' method"); |
44 | | - } |
| 40 | + @Override |
| 41 | + @Nonnull |
| 42 | + public LocalizeValue getDisplayName() { |
| 43 | + return LocalizeValue.localizeTODO("Class does not override 'toString()' method"); |
| 44 | + } |
45 | 45 |
|
46 | | - @Override |
47 | | - @Nonnull |
48 | | - public String getShortName() { |
49 | | - return "ClassHasNoToStringMethod"; |
50 | | - } |
| 46 | + @Override |
| 47 | + @Nonnull |
| 48 | + public String getShortName() { |
| 49 | + return "ClassHasNoToStringMethod"; |
| 50 | + } |
51 | 51 |
|
52 | | - @Nonnull |
53 | | - @Override |
54 | | - public PsiElementVisitor buildVisitor(@Nonnull final ProblemsHolder holder, |
55 | | - boolean isOnTheFly, |
56 | | - LocalInspectionToolSession session, |
57 | | - Object state) { |
58 | | - ClassHasNoToStringMethodInspectionState inspectionState = (ClassHasNoToStringMethodInspectionState)state; |
59 | | - return new JavaElementVisitor() { |
60 | | - @Override |
61 | | - public void visitClass(PsiClass clazz) { |
62 | | - if (AbstractToStringInspection.log.isDebugEnabled()) { |
63 | | - AbstractToStringInspection.log.debug("checkClass: clazz=" + clazz); |
64 | | - } |
| 52 | + @Nonnull |
| 53 | + @Override |
| 54 | + public PsiElementVisitor buildVisitor( |
| 55 | + @Nonnull final ProblemsHolder holder, |
| 56 | + boolean isOnTheFly, |
| 57 | + @Nonnull LocalInspectionToolSession session, |
| 58 | + @Nonnull Object state |
| 59 | + ) { |
| 60 | + ClassHasNoToStringMethodInspectionState inspectionState = (ClassHasNoToStringMethodInspectionState) state; |
| 61 | + return new JavaElementVisitor() { |
| 62 | + @Override |
| 63 | + @RequiredReadAction |
| 64 | + public void visitClass(@Nonnull PsiClass clazz) { |
| 65 | + if (AbstractToStringInspection.log.isDebugEnabled()) { |
| 66 | + AbstractToStringInspection.log.debug("checkClass: clazz=" + clazz); |
| 67 | + } |
65 | 68 |
|
66 | | - // must be a class |
67 | | - final PsiIdentifier nameIdentifier = clazz.getNameIdentifier(); |
68 | | - if (nameIdentifier == null || clazz.getName() == null) { |
69 | | - return; |
70 | | - } |
| 69 | + // must be a class |
| 70 | + PsiIdentifier nameIdentifier = clazz.getNameIdentifier(); |
| 71 | + if (nameIdentifier == null || clazz.getName() == null) { |
| 72 | + return; |
| 73 | + } |
71 | 74 |
|
72 | | - if (inspectionState.excludeException && InheritanceUtil.isInheritor(clazz, CommonClassNames.JAVA_LANG_THROWABLE)) { |
73 | | - return; |
74 | | - } |
75 | | - if (inspectionState.excludeDeprecated && clazz.isDeprecated()) { |
76 | | - return; |
77 | | - } |
78 | | - if (inspectionState.excludeEnum && clazz.isEnum()) { |
79 | | - return; |
80 | | - } |
81 | | - if (inspectionState.excludeAbstract && clazz.hasModifierProperty(PsiModifier.ABSTRACT)) { |
82 | | - return; |
83 | | - } |
84 | | - if (inspectionState.excludeTestCode && TestFrameworks.getInstance().isTestClass(clazz)) { |
85 | | - return; |
86 | | - } |
87 | | - if (inspectionState.excludeInnerClasses && clazz.getContainingClass() != null) { |
88 | | - return; |
89 | | - } |
| 75 | + if (inspectionState.excludeException && InheritanceUtil.isInheritor(clazz, CommonClassNames.JAVA_LANG_THROWABLE)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + if (inspectionState.excludeDeprecated && clazz.isDeprecated()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + if (inspectionState.excludeEnum && clazz.isEnum()) { |
| 82 | + return; |
| 83 | + } |
| 84 | + if (inspectionState.excludeAbstract && clazz.isAbstract()) { |
| 85 | + return; |
| 86 | + } |
| 87 | + if (inspectionState.excludeTestCode && TestFrameworks.getInstance().isTestClass(clazz)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + if (inspectionState.excludeInnerClasses && clazz.getContainingClass() != null) { |
| 91 | + return; |
| 92 | + } |
90 | 93 |
|
91 | | - // if it is an excluded class - then skip |
92 | | - if (StringUtil.isNotEmpty(inspectionState.excludeClassNames)) { |
93 | | - String name = clazz.getName(); |
94 | | - if (name != null && name.matches(inspectionState.excludeClassNames)) { |
95 | | - return; |
96 | | - } |
97 | | - } |
| 94 | + // if it is an excluded class - then skip |
| 95 | + if (StringUtil.isNotEmpty(inspectionState.excludeClassNames)) { |
| 96 | + String name = clazz.getName(); |
| 97 | + if (name != null && name.matches(inspectionState.excludeClassNames)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
98 | 101 |
|
99 | | - // must have fields |
100 | | - PsiField[] fields = clazz.getFields(); |
101 | | - if (fields.length == 0) { |
102 | | - return; |
103 | | - } |
| 102 | + // must have fields |
| 103 | + PsiField[] fields = clazz.getFields(); |
| 104 | + if (fields.length == 0) { |
| 105 | + return; |
| 106 | + } |
104 | 107 |
|
105 | | - // get list of fields and getter methods supposed to be dumped in the toString method |
106 | | - fields = GenerateToStringUtils.filterAvailableFields(clazz, GenerateToStringContext.getConfig().getFilterPattern()); |
107 | | - PsiMethod[] methods = null; |
108 | | - if (GenerateToStringContext.getConfig().isEnableMethods()) { |
109 | | - // okay 'getters in code generation' is enabled so check |
110 | | - methods = GenerateToStringUtils.filterAvailableMethods(clazz, GenerateToStringContext.getConfig().getFilterPattern()); |
111 | | - } |
| 108 | + // get list of fields and getter methods supposed to be dumped in the toString method |
| 109 | + fields = GenerateToStringUtils.filterAvailableFields(clazz, GenerateToStringContext.getConfig().getFilterPattern()); |
| 110 | + PsiMethod[] methods = null; |
| 111 | + if (GenerateToStringContext.getConfig().isEnableMethods()) { |
| 112 | + // okay 'getters in code generation' is enabled so check |
| 113 | + methods = GenerateToStringUtils.filterAvailableMethods(clazz, GenerateToStringContext.getConfig().getFilterPattern()); |
| 114 | + } |
112 | 115 |
|
113 | | - // there should be any fields |
114 | | - if (Math.max(fields.length, methods == null ? 0 : methods.length) == 0) { |
115 | | - return; |
116 | | - } |
| 116 | + // there should be any fields |
| 117 | + if (Math.max(fields.length, methods == null ? 0 : methods.length) == 0) { |
| 118 | + return; |
| 119 | + } |
117 | 120 |
|
118 | | - // okay some fields/getter methods are supposed to dumped, does a toString method exist |
119 | | - final PsiMethod[] toStringMethods = clazz.findMethodsByName("toString", false); |
120 | | - for (PsiMethod method : toStringMethods) { |
121 | | - final PsiParameterList parameterList = method.getParameterList(); |
122 | | - if (parameterList.getParametersCount() == 0) { |
123 | | - // toString() method found |
124 | | - return; |
125 | | - } |
126 | | - } |
127 | | - final PsiMethod[] superMethods = clazz.findMethodsByName("toString", true); |
128 | | - for (PsiMethod method : superMethods) { |
129 | | - final PsiParameterList parameterList = method.getParameterList(); |
130 | | - if (parameterList.getParametersCount() != 0) { |
131 | | - continue; |
132 | | - } |
133 | | - if (method.hasModifierProperty(PsiModifier.FINAL)) { |
134 | | - // final toString() in super class found |
135 | | - return; |
136 | | - } |
137 | | - } |
138 | | - holder.registerProblem(nameIdentifier, "Class '" + clazz.getName() + "' does not override 'toString()' method", |
139 | | - ProblemHighlightType.GENERIC_ERROR_OR_WARNING, GenerateToStringQuickFix.getInstance()); |
140 | | - } |
141 | | - }; |
142 | | - } |
| 121 | + // okay some fields/getter methods are supposed to dumped, does a toString method exist |
| 122 | + PsiMethod[] toStringMethods = clazz.findMethodsByName("toString", false); |
| 123 | + for (PsiMethod method : toStringMethods) { |
| 124 | + PsiParameterList parameterList = method.getParameterList(); |
| 125 | + if (parameterList.getParametersCount() == 0) { |
| 126 | + // toString() method found |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | + PsiMethod[] superMethods = clazz.findMethodsByName("toString", true); |
| 131 | + for (PsiMethod method : superMethods) { |
| 132 | + PsiParameterList parameterList = method.getParameterList(); |
| 133 | + if (parameterList.getParametersCount() != 0) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + if (method.isFinal()) { |
| 137 | + // final toString() in super class found |
| 138 | + return; |
| 139 | + } |
| 140 | + } |
| 141 | + holder.newProblem(LocalizeValue.of("Class '" + clazz.getName() + "' does not override 'toString()' method")) |
| 142 | + .range(nameIdentifier) |
| 143 | + .withFixes(GenerateToStringQuickFix.getInstance()) |
| 144 | + .create(); |
| 145 | + } |
| 146 | + }; |
| 147 | + } |
143 | 148 | } |
0 commit comments