blob: d328f49b1cbf41374778956ab78c285031244214 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig.abstraction;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.psi.*;
import com.intellij.psi.util.PropertyUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
/**
* @author Bas Leijdekkers
*/
public class BooleanParameterInspection extends BaseInspection {
@SuppressWarnings("PublicField")
public boolean onlyReportMultiple = false;
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message("boolean.parameter.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
final PsiMethod method = (PsiMethod)infos[0];
final int booleanParameterCount = ((Integer)infos[1]).intValue();
if (booleanParameterCount == 1) {
return method.isConstructor()
? InspectionGadgetsBundle.message("boolean.parameter.constructor.problem.descriptor")
: InspectionGadgetsBundle.message("boolean.parameter.problem.descriptor");
}
else {
return method.isConstructor()
? InspectionGadgetsBundle.message("boolean.parameters.constructor.problem.descriptor")
: InspectionGadgetsBundle.message("boolean.parameters.problem.descriptor");
}
}
@Nullable
@Override
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(
InspectionGadgetsBundle.message("boolean.parameter.only.report.multiple.option"), this, "onlyReportMultiple");
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new BooleanParameterVisitor();
}
private class BooleanParameterVisitor extends BaseInspectionVisitor {
@Override
public void visitMethod(PsiMethod method) {
super.visitMethod(method);
if (!method.hasModifierProperty(PsiModifier.PUBLIC)) {
final PsiClass aClass = method.getContainingClass();
if (aClass == null || !aClass.isInterface()) {
return;
}
}
if (PropertyUtil.isSimpleSetter(method)) {
return;
}
final PsiParameterList parameterList = method.getParameterList();
final PsiParameter[] parameters = parameterList.getParameters();
int count = 0;
for (PsiParameter parameter : parameters) {
final PsiType type = parameter.getType();
if (!PsiPrimitiveType.BOOLEAN.equals(type)) {
continue;
}
count++;
if (count > 1) {
break;
}
}
if (count == 0 || onlyReportMultiple && count == 1) {
return;
}
registerMethodError(method, method, Integer.valueOf(count));
}
}
}
|