mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IJ-CR-145121 [java-inspections] IDEA-357009 new option to configure custom classes for IncorrectMessageFormatInspection
- fix table for `IncorrectMessageFormatInspection` GitOrigin-RevId: 272d89fafaac63517e990b1d2808ec54d4c58e69
This commit is contained in:
committed by
intellij-monorepo-bot
parent
acbd648c20
commit
9eb0e15e2c
@@ -2331,10 +2331,7 @@ inspection.incorrect.message.format.incorrect.quotes.number=Probably incorrect n
|
||||
inspection.incorrect.message.format.not.found.argument=No argument for index ''{0}''
|
||||
inspection.incorrect.message.format.not.found.arguments=No arguments for indexes: {0}
|
||||
inspection.incorrect.message.format.not.used.argument=Argument with index ''{0}'' is not used in the pattern
|
||||
inspection.incorrect.message.custom.classes.methods=Custom classes and methods:
|
||||
inspection.incorrect.message.custom.classes=Classes
|
||||
inspection.incorrect.message.custom.methods=Methods
|
||||
inspection.incorrect.message.custom.classes.methods.description=Use this inspection setting to mark additional classes and methods as related to string formatting.
|
||||
inspection.incorrect.message.custom.classes.methods=Custom MessageFormat methods:
|
||||
|
||||
# {0} is one of the inspection.incorrect.message.format messages above
|
||||
inspection.incorrect.message.format.pattern={0} in message format pattern ''{1}''
|
||||
|
||||
+20
-19
@@ -1,13 +1,14 @@
|
||||
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
|
||||
package com.siyeh.ig.bugs;
|
||||
|
||||
import com.intellij.codeInsight.options.JavaClassValidator;
|
||||
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.codeInspection.dataFlow.CommonDataflow;
|
||||
import com.intellij.codeInspection.options.OptPane;
|
||||
import com.intellij.openapi.util.InvalidDataException;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.WriteExternalException;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.source.tree.java.PsiEmptyExpressionImpl;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
@@ -15,6 +16,8 @@ import com.intellij.util.containers.ContainerUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.format.MessageFormatUtil;
|
||||
import com.siyeh.ig.psiutils.ConstructionUtils;
|
||||
import com.siyeh.ig.psiutils.MethodMatcher;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -23,36 +26,22 @@ import java.util.*;
|
||||
|
||||
public final class IncorrectMessageFormatInspection extends AbstractBaseJavaLocalInspectionTool {
|
||||
|
||||
public List<String> customClasses = new ArrayList<>();
|
||||
public List<String> customMethods = new ArrayList<>();
|
||||
public MethodMatcher myMethodMatcher = new MethodMatcher().finishDefault();
|
||||
|
||||
@Override
|
||||
public @NotNull OptPane getOptionsPane() {
|
||||
return OptPane.pane(
|
||||
OptPane.table(
|
||||
InspectionGadgetsBundle.message("inspection.incorrect.message.custom.classes.methods"),
|
||||
OptPane.column("customClasses", InspectionGadgetsBundle.message("inspection.incorrect.message.custom.classes"),
|
||||
new JavaClassValidator()),
|
||||
OptPane.column("customMethods", InspectionGadgetsBundle.message("inspection.incorrect.message.custom.methods"))
|
||||
).description(InspectionGadgetsBundle.message("inspection.incorrect.message.custom.classes.methods.description"))
|
||||
);
|
||||
myMethodMatcher.getTable(InspectionGadgetsBundle.message(
|
||||
"inspection.incorrect.message.custom.classes.methods")).prefix("myMethodMatcher"));
|
||||
}
|
||||
|
||||
private boolean isCustomPatternMethodCall(@NotNull PsiMethodCallExpression call) {
|
||||
PsiReferenceExpression methodExpression = call.getMethodExpression();
|
||||
|
||||
String methodName = methodExpression.getReferenceName();
|
||||
if (methodName == null || !customMethods.contains(methodName)) {
|
||||
if (!myMethodMatcher.matches(call)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PsiMethod method = call.resolveMethod();
|
||||
if (method == null) return false;
|
||||
PsiClass containingClass = method.getContainingClass();
|
||||
if (containingClass == null || !customClasses.contains(containingClass.getQualifiedName())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
PsiParameter[] parameters = method.getParameterList().getParameters();
|
||||
if (parameters.length != 2) {
|
||||
return false;
|
||||
@@ -71,6 +60,18 @@ public final class IncorrectMessageFormatInspection extends AbstractBaseJavaLoca
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSettings(@NotNull Element element) throws InvalidDataException {
|
||||
super.readSettings(element);
|
||||
myMethodMatcher.readSettings(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSettings(@NotNull Element element) throws WriteExternalException {
|
||||
super.writeSettings(element);
|
||||
myMethodMatcher.writeSettings(element);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
|
||||
|
||||
@@ -20,6 +20,13 @@ Reports incorrect message format patterns or incorrect indexes of placeholders
|
||||
MessageFormat.format("{0}", 1, 2); // The argument with index '1' is not used in the pattern
|
||||
</code></pre>
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
Use the <b>Custom MessageFormat methods</b> table
|
||||
to specify method calls that should have their arguments checked as MessageFormat patterns.
|
||||
The table contains pairs of fully qualified class name and method name regular expression
|
||||
to match the containing class and name of the method calls.
|
||||
Class names also match subclasses.
|
||||
</p>
|
||||
<p><small>New in 2023.2</small>
|
||||
</body>
|
||||
</html>
|
||||
@@ -4,6 +4,7 @@ package com.siyeh.ig.bugs;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import com.siyeh.ig.LightJavaInspectionTestCase;
|
||||
import com.siyeh.ig.psiutils.MethodMatcher;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
@@ -22,8 +23,10 @@ public class IncorrectMessageFormatInspectionTest extends LightJavaCodeInsightFi
|
||||
|
||||
private void doTest(List<String> classes, List<String> methods) {
|
||||
IncorrectMessageFormatInspection inspection = new IncorrectMessageFormatInspection();
|
||||
inspection.customClasses.addAll(classes);
|
||||
inspection.customMethods.addAll(methods);
|
||||
MethodMatcher matcher = inspection.myMethodMatcher;
|
||||
for (int i = 0; i < classes.size(); i++) {
|
||||
matcher.add(classes.get(i), methods.get(i));
|
||||
}
|
||||
myFixture.enableInspections(inspection);
|
||||
myFixture.testHighlighting(getTestName(false) + ".java");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user