import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
import com.intellij.codeInspection.i18n.JavaI18nUtil;
import com.intellij.lang.java.JavaLanguage;
+import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
@Override
public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
- SwingUtilities.invokeLater(() -> {
+ ApplicationManager.getApplication().invokeLater(() -> {
if (project.isDisposed()) return;
final List<PsiExpression> expressions = new ArrayList<PsiExpression>();
for(SmartPsiElementPointer ptr: myExpressions) {
final PsiLiteralExpression myOriginalExpression = (PsiLiteralExpression)startElement;
final PsiField myConstant = myConst.getElement();
if (myConstant == null) return;
+ final PsiExpression initializer = myConstant.getInitializer();
+ if (!(initializer instanceof PsiLiteralExpression)) {
+ return;
+ }
if (!FileModificationService.getInstance().prepareFileForWrite(myOriginalExpression.getContainingFile())) return;
try {
final PsiReferenceExpression reference = createReferenceTo(myConstant, myOriginalExpression);
--- /dev/null
+class ClassA {
+ public static final String ABC = "ABCDEF";
+
+ public static void main(String[] args) {
+ System.out.println("ABC = " + ABC);
+ }
+}
+
+class ClassB {
+ public static final String ABC = "ABCDEF";
+}
\ No newline at end of file
--- /dev/null
+class ClassA {
+ public static final String ABC = ClassB.ABC;
+
+ public static void main(String[] args) {
+ System.out.println("ABC = " + ABC);
+ }
+}
+
+class ClassB {
+ public static final String ABC = "ABCDEF";
+}
\ No newline at end of file
@SuppressWarnings({"HardCodedStringLiteral"})
class Test {
@SuppressWarnings({"HardCodedStringLiteral"})
- String s = "abcdefghhijklmnop";
+ String s = <warning descr="Duplicate string literal found in 'Test' (in this file)">"abcdefghhijklmnop"</warning>;
@SuppressWarnings({"HardCodedStringLiteral"})
void f(@PropertyKey(resourceBundle = "xxx.yyy") String key) {
- String s = "abcdefghhijklmnop";
+ String s = <warning descr="Duplicate string literal found in 'Test' (in this file)">"abcdefghhijklmnop"</warning>;
f("abcdefghhijklmnop"); //no way
}
}
\ No newline at end of file
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<problems>
- <problem>
- <file>Test.java</file>
- <line>6</line>
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Duplicate String Literal</problem_class>
- <description><html><body>Duplicate string literal found in<br>'Test'</body></html></description>
- </problem>
-
- <problem>
- <file>Test.java</file>
- <line>10</line>
- <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Duplicate String Literal</problem_class>
- <description><html><body>Duplicate string literal found in<br>'Test'</body></html></description>
- </problem>
-</problems>
\ No newline at end of file
*/
package com.intellij.codeInspection.i18n;
+import com.intellij.codeInsight.intention.IntentionAction;
+import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.duplicateStringLiteral.DuplicateStringLiteralInspection;
-import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.openapi.application.PluginPathManager;
-import com.intellij.testFramework.InspectionTestCase;
+import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
-public class DuplicateStringLiteralInspectionTest extends InspectionTestCase {
+import java.util.List;
+
+public class DuplicateStringLiteralInspectionTest extends JavaCodeInsightFixtureTestCase {
private final DuplicateStringLiteralInspection myInspection = new DuplicateStringLiteralInspection();
- private void doTest() throws Exception {
- doTest("duplicateStringLiteral/"+getTestName(true), new LocalInspectionToolWrapper(myInspection), "java 1.5");
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ myFixture.enableInspections(myInspection);
}
- public void testPropertyKey() throws Exception{ myInspection.IGNORE_PROPERTY_KEYS = true; doTest(); }
+ public void testPropertyKey() throws Exception {
+ myInspection.IGNORE_PROPERTY_KEYS = true;
+ myFixture.testHighlighting("PropertyKey.java");
+ }
+
+ public void testBatchApplication() {
+ final List<IntentionAction> fixes = myFixture.getAllQuickFixes("ApplyRenameForWholeFile.java");
+ fixes
+ .stream()
+ .filter(i -> InspectionsBundle.message("inspection.duplicates.replace.family.quickfix").equals(i.getFamilyName()))
+ .forEach(myFixture::launchAction);
+ myFixture.checkResultByFile("ApplyRenameForWholeFileAfter.java");
+ }
@Override
protected String getTestDataPath() {
- return PluginPathManager.getPluginHomePath("java-i18n") + "/testData/inspections";
+ return PluginPathManager.getPluginHomePath("java-i18n") + "/testData/inspections/duplicateStringLiteral/";
}
}
\ No newline at end of file