diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java index 00e66219d18c..b7d99687f87e 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/ex/InspectionProfileTest.java @@ -663,6 +663,39 @@ public class InspectionProfileTest extends LightIdeaTestCase { " \n" + ""); } + + public void testMergedRedundantStringOperationsInspections() throws Exception { + InspectionProfileImpl profile = checkMergedNoChanges("\n" + + " "); + assertFalse(profile.isToolEnabled(HighlightDisplayKey.find("StringOperationCanBeSimplified"), null)); + } + + public void testSecondMergedRedundantStringOperationsInspections() throws Exception { + InspectionProfileImpl bothDisabled = checkMergedNoChanges("\n" + + " "); + assertFalse(bothDisabled.isToolEnabled(HighlightDisplayKey.find("StringOperationCanBeSimplified"), null)); + + InspectionProfileImpl oneEnabled = checkMergedNoChanges("\n" + + " "); + assertTrue(oneEnabled.isToolEnabled(HighlightDisplayKey.find("StringOperationCanBeSimplified"), null)); + } public void testMergedCallToSuspiciousStringMethodInspections() throws Exception { checkMergedNoChanges("\n" + @@ -745,10 +778,11 @@ public class InspectionProfileTest extends LightIdeaTestCase { ""); } - private static void checkMergedNoChanges(@Language("XML") String initialText) throws Exception { + private static InspectionProfileImpl checkMergedNoChanges(@Language("XML") String initialText) throws Exception { InspectionProfileImpl profile = createProfile(new InspectionProfileImpl("foo")); readFromXml(profile, initialText); assertEquals(initialText, serialize(profile)); + return profile; } public void testLockProfile() { diff --git a/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionElementsMerger.java b/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionElementsMerger.java index 4b85fd8ad870..f8011f7e12b3 100644 --- a/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionElementsMerger.java +++ b/platform/analysis-api/src/com/intellij/codeInspection/ex/InspectionElementsMerger.java @@ -38,6 +38,8 @@ public abstract class InspectionElementsMerger { /** * @return the shortNames of the inspections whose settings needs to be merged. + * + * when one of toolNames doesn't present in the profile, default settings for that tool are expected, e.g. by default the result would be enabled with min severity WARNING */ @NotNull public abstract String[] getSourceToolNames(); diff --git a/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionElementsMergerBase.java b/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionElementsMergerBase.java index 67a6a7bbc822..fade7d2b368e 100644 --- a/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionElementsMergerBase.java +++ b/platform/analysis-impl/src/com/intellij/codeInspection/ex/InspectionElementsMergerBase.java @@ -105,7 +105,7 @@ public abstract class InspectionElementsMergerBase extends InspectionElementsMer } } } - if (!toolElement.getChildren().isEmpty()) { + if (writeMergedContent(toolElement)) { toolElement.setAttribute(InspectionProfileImpl.CLASS_TAG, getMergedToolName()); toolElement.setAttribute(ToolsImpl.ENABLED_ATTRIBUTE, String.valueOf(enabled)); if (level != null) { @@ -130,6 +130,10 @@ public abstract class InspectionElementsMergerBase extends InspectionElementsMer return null; } + protected boolean writeMergedContent(Element toolElement) { + return !toolElement.getChildren().isEmpty(); + } + protected Element getSourceElement(Map inspectionElements, String sourceToolName) { return inspectionElements.get(sourceToolName); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationMerger.java b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationMerger.java index a0bc4217e698..f7ae5beef1e6 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationMerger.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/redundancy/RedundantStringOperationMerger.java @@ -1,24 +1,87 @@ // Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.siyeh.ig.redundancy; -import com.intellij.codeInspection.ex.InspectionElementsMerger; +import com.google.common.collect.ImmutableSet; +import com.intellij.codeInspection.ex.InspectionElementsMergerBase; +import com.intellij.util.ArrayUtil; +import org.jdom.Element; import org.jetbrains.annotations.NotNull; -public class RedundantStringOperationMerger extends InspectionElementsMerger { +import java.util.Map; +import java.util.Set; + +public class RedundantStringOperationMerger extends InspectionElementsMergerBase { + + private static final String OLD_MERGER_NAME = "RedundantStringOperation"; + private static final Set OLD_SOURCE_NAMES = ImmutableSet.of("StringToString", "SubstringZero", "ConstantStringIntern"); + @NotNull @Override public String getMergedToolName() { return "StringOperationCanBeSimplified"; } + @Override + protected Element getSourceElement(Map inspectionElements, String sourceToolName) { + if (inspectionElements.containsKey(sourceToolName)) { + return inspectionElements.get(sourceToolName); + } + + if (sourceToolName.equals(OLD_MERGER_NAME)) {//need to merge initial tools to get merged redundant string operations + return new InspectionElementsMergerBase(){ + @NotNull + @Override + public String getMergedToolName() { + return OLD_MERGER_NAME; + } + + @NotNull + @Override + public String[] getSourceToolNames() { + return ArrayUtil.toStringArray(OLD_SOURCE_NAMES); + } + + @Override + public Element merge(Map inspectionElements) { + return super.merge(inspectionElements); + } + + @Override + protected boolean writeMergedContent(Element toolElement) { + return true; + } + }.merge(inspectionElements); + } + else if (OLD_SOURCE_NAMES.contains(sourceToolName)) { + Element merged = inspectionElements.get(OLD_MERGER_NAME); + if (merged != null) { // RedundantStringOperation already replaced the content + Element clone = merged.clone(); + clone.setAttribute("class", sourceToolName); + return clone; + } + } + return null; + } + @NotNull @Override public String[] getSourceToolNames() { + return new String[] { + "StringToString", + "SubstringZero", + "ConstantStringIntern", + "StringConstructor", + OLD_MERGER_NAME + }; + } + + @NotNull + @Override + public String[] getSuppressIds() { return new String[] { "StringToString", "RedundantStringToString", "SubstringZero", "ConstantStringIntern", - "RedundantStringConstructorCall", "StringConstructor", - "RedundantStringOperation" + "RedundantStringConstructorCall", "StringConstructor", OLD_MERGER_NAME }; } }