Skip to content

Commit e6d476e

Browse files
authored
Inspections localizing and refactoring (part 3) (#252)
* Inspections localizing and refactoring (part 3). * Returning deprecated method getting PsiSearchHelper.
1 parent e573ef8 commit e6d476e

File tree

16 files changed

+1042
-968
lines changed

16 files changed

+1042
-968
lines changed

java-analysis-api/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.analysis.JavaAnalysisLocalize.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ contract.return.validator.return.type.must.be.boolean:
4444
text: method return type must be 'boolean'
4545
contract.return.validator.too.few.parameters:
4646
text: not applicable for method that has {0, choice, 0#no parameters|1#one parameter|2#{0} parameters}
47-
contract.return.value.validation.prefix:
48-
text: 'Contract return value ''''{0}'''':'
47+
contract.return.value.validation.problem:
48+
text: 'Contract return value ''''{0}'''': {1}'
4949
convert.0.to.float:
5050
text: Convert ''{0}'' to float
5151
custom.exception.class.should.have.a.constructor:
@@ -663,9 +663,9 @@ inspection.message.nullable.method.implements.non.null.method:
663663
inspection.module.exports.package.to.itself:
664664
text: Module exports/opens package to itself
665665
inspection.non.final.field.in.immutable.display.name:
666-
text: Non-final field in @Immutable class
666+
text: Non-final field in '@Immutable' class
667667
inspection.non.final.guard.display.name:
668-
text: Non-final @GuardedBy field
668+
text: Non-final '@GuardedBy' field
669669
inspection.nullable.problems.NotNull.parameter.overrides.Nullable:
670670
text: Parameter annotated @{0} must not override @{1} parameter
671671
inspection.nullable.problems.NotNull.parameter.overrides.not.annotated:
@@ -877,11 +877,11 @@ mutation.signature.problem.static.method.cannot.mutate.this:
877877
navigate.to.overridden.methods.title:
878878
text: Overriding methods of {0}
879879
non.final.field.code.ref.code.in.immutable.class.loc:
880-
text: 'Non-final field <code>#ref</code> in @Immutable class #loc'
880+
text: 'Non-final field <code>#ref</code> in ''@Immutable'' class #loc'
881881
non.final.guarded.by.field.0.loc:
882-
text: 'Non-final @GuardedBy field "{0}" #loc'
882+
text: 'Non-final ''@GuardedBy'' field "{0}" #loc'
883883
non.final.guarded.by.field.ref.loc:
884-
text: 'Non-final @GuardedBy field #ref #loc'
884+
text: 'Non-final ''@GuardedBy'' field #ref #loc'
885885
non.null.type.argument.is.expected:
886886
text: Non-null type argument is expected
887887
not.annotated.method.is.used.as.an.override.for.a.method.annotated.with.0:

java-analysis-impl/src/main/java/org/jetbrains/java/generate/inspection/ClassHasNoToStringMethodInspection.java

Lines changed: 100 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
import com.intellij.java.language.codeInsight.TestFrameworks;
1919
import com.intellij.java.language.psi.*;
2020
import com.intellij.java.language.psi.util.InheritanceUtil;
21+
import consulo.annotation.access.RequiredReadAction;
2122
import consulo.annotation.component.ExtensionImpl;
2223
import consulo.language.editor.inspection.LocalInspectionToolSession;
23-
import consulo.language.editor.inspection.ProblemHighlightType;
2424
import consulo.language.editor.inspection.ProblemsHolder;
2525
import consulo.language.psi.PsiElementVisitor;
2626
import consulo.localize.LocalizeValue;
@@ -37,107 +37,112 @@
3737
*/
3838
@ExtensionImpl
3939
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+
}
4545

46-
@Override
47-
@Nonnull
48-
public String getShortName() {
49-
return "ClassHasNoToStringMethod";
50-
}
46+
@Override
47+
@Nonnull
48+
public String getShortName() {
49+
return "ClassHasNoToStringMethod";
50+
}
5151

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+
}
6568

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+
}
7174

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+
}
9093

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+
}
98101

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+
}
104107

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+
}
112115

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+
}
117120

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+
}
143148
}

java-analysis-impl/src/main/resources/LOCALIZE-LIB/en_US/consulo.java.analysis.impl.JavaInspectionsLocalize.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,3 +1592,5 @@ xml.suppressable.for.file.title:
15921592
text: Suppress for file
15931593
xml.suppressable.for.tag.title:
15941594
text: Suppress for tag
1595+
inspection.message.redundant.default.parameter.value.assignment:
1596+
text: Redundant default parameter value assignment

0 commit comments

Comments
 (0)