mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
same parameter value: introduce an option for minimal usage count
This commit is contained in:
@@ -68,4 +68,8 @@ public interface RefParameter extends RefJavaElement {
|
||||
|
||||
@Override
|
||||
PsiParameter getElement();
|
||||
|
||||
default int getUsageCount() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
+7
-1
@@ -17,9 +17,9 @@ public class RefParameterImpl extends RefJavaElementImpl implements RefParameter
|
||||
private static final int USED_FOR_READING_MASK = 0x10000;
|
||||
private static final int USED_FOR_WRITING_MASK = 0x20000;
|
||||
|
||||
|
||||
private final short myIndex;
|
||||
private Object myActualValueTemplate;
|
||||
private int myUsageCount;
|
||||
|
||||
RefParameterImpl(PsiParameter parameter, int index, RefManager manager) {
|
||||
super(parameter, manager);
|
||||
@@ -55,6 +55,11 @@ public class RefParameterImpl extends RefJavaElementImpl implements RefParameter
|
||||
return (PsiParameter)super.getElement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUsageCount() {
|
||||
return myUsageCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUsedForWriting() {
|
||||
return checkFlag(USED_FOR_WRITING_MASK);
|
||||
@@ -92,6 +97,7 @@ public class RefParameterImpl extends RefJavaElementImpl implements RefParameter
|
||||
}
|
||||
|
||||
void updateTemplateValue(PsiExpression expression, @Nullable PsiElement accessPlace) {
|
||||
myUsageCount++;
|
||||
if (myActualValueTemplate == VALUE_IS_NOT_CONST) return;
|
||||
|
||||
Object newTemplate = getAccessibleExpressionValue(expression, () -> accessPlace == null ? getContainingFile() : accessPlace);
|
||||
|
||||
+26
-4
@@ -30,6 +30,10 @@ import com.intellij.refactoring.safeDelete.JavaSafeDeleteProcessor;
|
||||
import com.intellij.refactoring.ui.ConflictsDialog;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.InlineUtil;
|
||||
import com.intellij.ui.components.JBCheckBox;
|
||||
import com.intellij.ui.components.fields.IntegerField;
|
||||
import com.intellij.ui.components.fields.valueEditors.IntegerValueEditor;
|
||||
import com.intellij.ui.components.fields.valueEditors.ValueEditor;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.VisibilityUtil;
|
||||
@@ -39,6 +43,8 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
@@ -55,18 +61,31 @@ public class SameParameterValueInspection extends GlobalJavaBatchInspectionTool
|
||||
private static final String DEFAULT_HIGHEST_MODIFIER = PsiModifier.PROTECTED;
|
||||
@PsiModifier.ModifierConstant
|
||||
public String highestModifier = DEFAULT_HIGHEST_MODIFIER;
|
||||
public int minimalUsageCount = 1;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
LabeledComponent<VisibilityModifierChooser> component = LabeledComponent.create(new VisibilityModifierChooser(() -> true,
|
||||
highestModifier,
|
||||
(newModifier) -> highestModifier = newModifier),
|
||||
"Methods to report:",
|
||||
"Minimal reported method visibility:",
|
||||
BorderLayout.WEST);
|
||||
panel.add(component, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, JBUI.emptyInsets(), 0, 0));
|
||||
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
panel.add(component, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NORTHEAST, JBUI.emptyInsets(), 0, 0));
|
||||
|
||||
IntegerField minimalUsageCountEditor = new IntegerField(null, 1, Integer.MAX_VALUE);
|
||||
minimalUsageCountEditor.getValueEditor().addListener(new ValueEditor.Listener<Integer>() {
|
||||
@Override
|
||||
public void valueChanged(@NotNull Integer newValue) {
|
||||
minimalUsageCount = newValue;
|
||||
}
|
||||
});
|
||||
minimalUsageCountEditor.setValue(minimalUsageCount);
|
||||
minimalUsageCountEditor.setColumns(4);
|
||||
panel.add(LabeledComponent.create(minimalUsageCountEditor, "Minimal reported method usage count:", BorderLayout.WEST),
|
||||
new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NORTHWEST, JBUI.emptyInsets(), 0, 0));
|
||||
return panel;
|
||||
}
|
||||
|
||||
@@ -94,6 +113,7 @@ public class SameParameterValueInspection extends GlobalJavaBatchInspectionTool
|
||||
for (RefParameter refParameter : parameters) {
|
||||
Object value = refParameter.getActualConstValue();
|
||||
if (value != VALUE_IS_NOT_CONST && value != VALUE_UNDEFINED) {
|
||||
if (minimalUsageCount != 0 && refParameter.getUsageCount() < minimalUsageCount) continue;
|
||||
if (!globalContext.shouldCheck(refParameter, this)) continue;
|
||||
if (problems == null) problems = new ArrayList<>(1);
|
||||
problems.add(registerProblem(manager, refParameter.getElement(), value, refParameter.isUsedForWriting()));
|
||||
@@ -412,10 +432,11 @@ public class SameParameterValueInspection extends GlobalJavaBatchInspectionTool
|
||||
}
|
||||
Arrays.fill(paramValues, VALUE_UNDEFINED);
|
||||
|
||||
int[] usageCount = {0};
|
||||
if (UnusedSymbolUtil
|
||||
.processUsages(holder.getProject(), method.getContainingFile(), method, new EmptyProgressIndicator(), null, info -> {
|
||||
PsiElement element = info.getElement();
|
||||
|
||||
usageCount[0]++;
|
||||
if (!(element instanceof PsiReferenceExpression)) {
|
||||
return false;
|
||||
}
|
||||
@@ -447,6 +468,7 @@ public class SameParameterValueInspection extends GlobalJavaBatchInspectionTool
|
||||
|
||||
return needFurtherProcess;
|
||||
})) {
|
||||
if (minimalUsageCount != 0 && usageCount[0] < minimalUsageCount) return;
|
||||
for (int i = 0, length = paramValues.length; i < length; i++) {
|
||||
Object value = paramValues[i];
|
||||
if (value != VALUE_UNDEFINED && value != VALUE_IS_NOT_CONST) {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>16</line>
|
||||
<problem_class>Actual method parameter is the same constant</problem_class>
|
||||
<description>Actual value of parameter 'val' is always '239'</description>
|
||||
</problem>
|
||||
</problems>
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
fooFooFooBarBaz(239);
|
||||
fooFooFooBarBaz(239);
|
||||
fooFooFooBarBaz(239);
|
||||
fooFooFooBarBaz(239);
|
||||
fooFooFooBarBaz(239);
|
||||
|
||||
bazSomeSome(239);
|
||||
bazSomeSome(239);
|
||||
bazSomeSome(239);
|
||||
bazSomeSome(239);
|
||||
}
|
||||
|
||||
private static void fooFooFooBarBaz(int val) {
|
||||
|
||||
}
|
||||
|
||||
private static void bazSomeSome(int val) {
|
||||
|
||||
}
|
||||
}
|
||||
+11
@@ -75,4 +75,15 @@ public class SameParameterValueLocalTest extends InspectionTestCase {
|
||||
public void testNegativeDouble() {
|
||||
doTest(getGlobalTestDir(), myTool);
|
||||
}
|
||||
|
||||
public void testUsageCount() {
|
||||
int previous = myGlobalTool.minimalUsageCount;
|
||||
try {
|
||||
myGlobalTool.minimalUsageCount = 5;
|
||||
doTest(getGlobalTestDir(), myTool);
|
||||
}
|
||||
finally {
|
||||
myGlobalTool.minimalUsageCount = previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,4 +86,15 @@ public class SameParameterValueTest extends InspectionTestCase {
|
||||
public void testClassObject() {
|
||||
doTest(getTestDir(), myTool, false, true);
|
||||
}
|
||||
|
||||
public void testUsageCount() {
|
||||
int previous = myTool.minimalUsageCount;
|
||||
try {
|
||||
myTool.minimalUsageCount = 5;
|
||||
doTest(getTestDir(), myTool, false, true);
|
||||
}
|
||||
finally {
|
||||
myTool.minimalUsageCount = previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user