redundant string operation: support two step merging

This commit is contained in:
Anna.Kozlova
2018-11-07 17:58:39 +01:00
parent c0802407e4
commit dc2db462a5
4 changed files with 109 additions and 6 deletions
@@ -663,6 +663,39 @@ public class InspectionProfileTest extends LightIdeaTestCase {
" <inspection_tool class=\"ThrowableNotThrown\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
"</profile>");
}
public void testMergedRedundantStringOperationsInspections() throws Exception {
InspectionProfileImpl profile = checkMergedNoChanges("<profile version=\"1.0\">\n" +
" <option name=\"myName\" value=\"" + PROFILE + "\" />\n" +
" <inspection_tool class=\"ConstantStringIntern\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
" <inspection_tool class=\"StringConstructor\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
" <option name=\"ignoreSubstringArguments\" value=\"false\" />\n" +
" </inspection_tool>\n" +
" <inspection_tool class=\"StringToString\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
" <inspection_tool class=\"SubstringZero\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
"</profile>");
assertFalse(profile.isToolEnabled(HighlightDisplayKey.find("StringOperationCanBeSimplified"), null));
}
public void testSecondMergedRedundantStringOperationsInspections() throws Exception {
InspectionProfileImpl bothDisabled = checkMergedNoChanges("<profile version=\"1.0\">\n" +
" <option name=\"myName\" value=\"ToConvert\" />\n" +
" <inspection_tool class=\"RedundantStringOperation\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
" <inspection_tool class=\"StringConstructor\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
" <option name=\"ignoreSubstringArguments\" value=\"false\" />\n" +
" </inspection_tool>\n" +
"</profile>");
assertFalse(bothDisabled.isToolEnabled(HighlightDisplayKey.find("StringOperationCanBeSimplified"), null));
InspectionProfileImpl oneEnabled = checkMergedNoChanges("<profile version=\"1.0\">\n" +
" <option name=\"myName\" value=\"ToConvert\" />\n" +
" <inspection_tool class=\"RedundantStringOperation\" enabled=\"false\" level=\"WARNING\" enabled_by_default=\"false\" />\n" +
" <inspection_tool class=\"StringConstructor\" enabled=\"true\" level=\"WARNING\" enabled_by_default=\"false\">\n" +
" <option name=\"ignoreSubstringArguments\" value=\"false\" />\n" +
" </inspection_tool>\n" +
"</profile>");
assertTrue(oneEnabled.isToolEnabled(HighlightDisplayKey.find("StringOperationCanBeSimplified"), null));
}
public void testMergedCallToSuspiciousStringMethodInspections() throws Exception {
checkMergedNoChanges("<profile version=\"1.0\">\n" +
@@ -745,10 +778,11 @@ public class InspectionProfileTest extends LightIdeaTestCase {
"</profile>");
}
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() {
@@ -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();
@@ -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<String, Element> inspectionElements, String sourceToolName) {
return inspectionElements.get(sourceToolName);
}
@@ -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<String> OLD_SOURCE_NAMES = ImmutableSet.of("StringToString", "SubstringZero", "ConstantStringIntern");
@NotNull
@Override
public String getMergedToolName() {
return "StringOperationCanBeSimplified";
}
@Override
protected Element getSourceElement(Map<String, Element> 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<String, Element> 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
};
}
}