mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
IDEA-116071 (Field can be final inspection change)
This commit is contained in:
+25
-61
@@ -18,6 +18,7 @@ package com.intellij.codeInspection.localCanBeFinal;
|
||||
import com.intellij.codeInsight.FileModificationService;
|
||||
import com.intellij.codeInsight.daemon.GroupNames;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
@@ -44,6 +45,8 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
|
||||
public boolean REPORT_VARIABLES = true;
|
||||
public boolean REPORT_PARAMETERS = true;
|
||||
public boolean REPORT_CATCH_PARAMETERS = true;
|
||||
public boolean REPORT_FOREACH_PARAMETERS = true;
|
||||
|
||||
private final LocalQuickFix myQuickFix;
|
||||
@NonNls public static final String SHORT_NAME = "LocalCanBeFinal";
|
||||
@@ -148,6 +151,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
@Override
|
||||
public void visitCatchSection(PsiCatchSection section) {
|
||||
super.visitCatchSection(section);
|
||||
if (!REPORT_CATCH_PARAMETERS) return;
|
||||
final PsiParameter parameter = section.getParameter();
|
||||
if (PsiTreeUtil.getParentOfType(parameter, PsiClass.class) != PsiTreeUtil.getParentOfType(body, PsiClass.class)) {
|
||||
return;
|
||||
@@ -164,6 +168,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
|
||||
@Override public void visitForeachStatement(PsiForeachStatement statement) {
|
||||
super.visitForeachStatement(statement);
|
||||
if (!REPORT_FOREACH_PARAMETERS) return;
|
||||
final PsiParameter param = statement.getIterationParameter();
|
||||
if (PsiTreeUtil.getParentOfType(param, PsiClass.class) != PsiTreeUtil.getParentOfType(body, PsiClass.class)) {
|
||||
return;
|
||||
@@ -219,7 +224,7 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
}
|
||||
});
|
||||
|
||||
if (body.getParent() instanceof PsiMethod && isReportParameters()) {
|
||||
if (body.getParent() instanceof PsiMethod && REPORT_PARAMETERS) {
|
||||
final PsiMethod method = (PsiMethod)body.getParent();
|
||||
if (!(method instanceof SyntheticElement)) { // e.g. JspHolderMethod
|
||||
Collections.addAll(result, method.getParameterList().getParameters());
|
||||
@@ -271,19 +276,21 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
|
||||
private boolean shouldBeIgnored(PsiVariable psiVariable) {
|
||||
if (psiVariable.hasModifierProperty(PsiModifier.FINAL)) return true;
|
||||
return isLocalVariable(psiVariable) ? !isReportVariables() : !isReportParameters();
|
||||
}
|
||||
|
||||
private static boolean isLocalVariable(PsiVariable variable) {
|
||||
if (variable instanceof PsiLocalVariable) {
|
||||
return true;
|
||||
if (psiVariable instanceof PsiLocalVariable) {
|
||||
return !REPORT_VARIABLES;
|
||||
}
|
||||
if (!(variable instanceof PsiParameter)) {
|
||||
return false;
|
||||
if (psiVariable instanceof PsiParameter) {
|
||||
final PsiParameter parameter = (PsiParameter)psiVariable;
|
||||
final PsiElement declarationScope = parameter.getDeclarationScope();
|
||||
if (declarationScope instanceof PsiCatchSection) {
|
||||
return !REPORT_CATCH_PARAMETERS;
|
||||
}
|
||||
else if (declarationScope instanceof PsiForeachStatement) {
|
||||
return !REPORT_FOREACH_PARAMETERS;
|
||||
}
|
||||
return !REPORT_PARAMETERS;
|
||||
}
|
||||
final PsiParameter parameter = (PsiParameter)variable;
|
||||
final PsiElement declarationScope = parameter.getDeclarationScope();
|
||||
return !(declarationScope instanceof PsiMethod) && !(declarationScope instanceof PsiLambdaExpression);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -336,55 +343,12 @@ public class LocalCanBeFinal extends BaseJavaBatchLocalInspectionTool {
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
return new OptionsPanel();
|
||||
}
|
||||
|
||||
private boolean isReportVariables() {
|
||||
return REPORT_VARIABLES;
|
||||
}
|
||||
|
||||
private boolean isReportParameters() {
|
||||
return REPORT_PARAMETERS;
|
||||
}
|
||||
|
||||
private class OptionsPanel extends JPanel {
|
||||
private final JCheckBox myReportVariablesCheckbox;
|
||||
private final JCheckBox myReportParametersCheckbox;
|
||||
|
||||
private OptionsPanel() {
|
||||
super(new GridBagLayout());
|
||||
|
||||
GridBagConstraints gc = new GridBagConstraints();
|
||||
gc.weighty = 0;
|
||||
gc.weightx = 1;
|
||||
gc.fill = GridBagConstraints.HORIZONTAL;
|
||||
gc.anchor = GridBagConstraints.NORTHWEST;
|
||||
|
||||
|
||||
myReportVariablesCheckbox = new JCheckBox(InspectionsBundle.message("inspection.local.can.be.final.option"));
|
||||
myReportVariablesCheckbox.setSelected(REPORT_VARIABLES);
|
||||
myReportVariablesCheckbox.getModel().addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
REPORT_VARIABLES = myReportVariablesCheckbox.isSelected();
|
||||
}
|
||||
});
|
||||
gc.gridy = 0;
|
||||
add(myReportVariablesCheckbox, gc);
|
||||
|
||||
myReportParametersCheckbox = new JCheckBox(InspectionsBundle.message("inspection.local.can.be.final.option1"));
|
||||
myReportParametersCheckbox.setSelected(REPORT_PARAMETERS);
|
||||
myReportParametersCheckbox.getModel().addChangeListener(new ChangeListener() {
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
REPORT_PARAMETERS = myReportParametersCheckbox.isSelected();
|
||||
}
|
||||
});
|
||||
|
||||
gc.weighty = 1;
|
||||
gc.gridy++;
|
||||
add(myReportParametersCheckbox, gc);
|
||||
}
|
||||
final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox(InspectionsBundle.message("inspection.local.can.be.final.option"), "REPORT_VARIABLES");
|
||||
panel.addCheckbox(InspectionsBundle.message("inspection.local.can.be.final.option1"), "REPORT_PARAMETERS");
|
||||
panel.addCheckbox(InspectionsBundle.message("inspection.local.can.be.final.option2"), "REPORT_CATCH_PARAMETERS");
|
||||
panel.addCheckbox(InspectionsBundle.message("inspection.local.can.be.final.option3"), "REPORT_FOREACH_PARAMETERS");
|
||||
return panel;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -127,12 +127,14 @@ public class LocalCanBeFinalTest extends InspectionTestCase {
|
||||
public void testForeachNotReported() throws Exception {
|
||||
myTool.REPORT_PARAMETERS = true;
|
||||
myTool.REPORT_VARIABLES = false;
|
||||
myTool.REPORT_FOREACH_PARAMETERS = false;
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNestedForeach() throws Exception {
|
||||
myTool.REPORT_PARAMETERS = false;
|
||||
myTool.REPORT_VARIABLES = true;
|
||||
myTool.REPORT_FOREACH_PARAMETERS = true;
|
||||
doTest();
|
||||
}
|
||||
|
||||
|
||||
@@ -123,6 +123,8 @@ inspection.1.7.problem.descriptor=Usage of generified after 1.6 API which would
|
||||
inspection.local.can.be.final.display.name=Local variable or parameter can be final
|
||||
inspection.local.can.be.final.option=Report local variables
|
||||
inspection.local.can.be.final.option1=Report method parameters
|
||||
inspection.local.can.be.final.option2=Report catch parameters
|
||||
inspection.local.can.be.final.option3=Report foreach parameters
|
||||
inspection.can.be.local.parameter.problem.descriptor=Parameter <code>#ref</code> can have <code>final</code> modifier
|
||||
inspection.can.be.local.variable.problem.descriptor=Variable <code>#ref</code> can have <code>final</code> modifier
|
||||
|
||||
|
||||
Reference in New Issue
Block a user