Skip to content

Commit 567f1fe

Browse files
authored
Localizing inspections (part 2). (#209)
1 parent 637c7cd commit 567f1fe

50 files changed

Lines changed: 3789 additions & 3738 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

plugin/src/main/java/com/intellij/java/impl/ig/bugs/IteratorNextDoesNotThrowNoSuchElementExceptionInspection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class IteratorNextDoesNotThrowNoSuchElementExceptionInspection extends BaseInspection {
3636
@Nonnull
3737
@Override
38-
@Pattern("[a-zA-Z_0-9.]+")
38+
@Pattern(VALID_ID_PATTERN)
3939
public String getID() {
4040
return "IteratorNextCanNotThrowNoSuchElementException";
4141
}

plugin/src/main/java/com/intellij/java/impl/ig/bugs/MalformedFormatStringInspection.java

Lines changed: 100 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -25,116 +25,116 @@
2525
import com.siyeh.ig.psiutils.ExpressionUtils;
2626
import com.siyeh.localize.InspectionGadgetsLocalize;
2727
import consulo.annotation.component.ExtensionImpl;
28+
import consulo.localize.LocalizeValue;
2829
import jakarta.annotation.Nonnull;
2930

3031
@ExtensionImpl
3132
public class MalformedFormatStringInspection extends BaseInspection {
32-
33-
@Override
34-
@Nonnull
35-
public String getDisplayName() {
36-
return InspectionGadgetsLocalize.malformedFormatStringDisplayName().get();
37-
}
38-
39-
@Override
40-
@Nonnull
41-
public String buildErrorString(Object... infos) {
42-
final Object value = infos[0];
43-
if (value instanceof Exception) {
44-
return InspectionGadgetsLocalize.malformedFormatStringProblemDescriptorMalformed().get();
45-
}
46-
final Validator[] validators = (Validator[])value;
47-
final int argumentCount = ((Integer)infos[1]).intValue();
48-
if (validators.length < argumentCount) {
49-
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.many.arguments");
33+
@Nonnull
34+
@Override
35+
public LocalizeValue getDisplayName() {
36+
return InspectionGadgetsLocalize.malformedFormatStringDisplayName();
5037
}
51-
if (validators.length > argumentCount) {
52-
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.few.arguments");
38+
39+
@Override
40+
@Nonnull
41+
public String buildErrorString(Object... infos) {
42+
final Object value = infos[0];
43+
if (value instanceof Exception) {
44+
return InspectionGadgetsLocalize.malformedFormatStringProblemDescriptorMalformed().get();
45+
}
46+
final Validator[] validators = (Validator[]) value;
47+
final int argumentCount = ((Integer) infos[1]).intValue();
48+
if (validators.length < argumentCount) {
49+
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.many.arguments");
50+
}
51+
if (validators.length > argumentCount) {
52+
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.few.arguments");
53+
}
54+
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.arguments.do.not.match.type");
5355
}
54-
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.arguments.do.not.match.type");
55-
}
5656

57-
@Override
58-
public boolean isEnabledByDefault() {
59-
return true;
60-
}
57+
@Override
58+
public boolean isEnabledByDefault() {
59+
return true;
60+
}
6161

62-
@Override
63-
public BaseInspectionVisitor buildVisitor() {
64-
return new MalformedFormatStringVisitor();
65-
}
62+
@Override
63+
public BaseInspectionVisitor buildVisitor() {
64+
return new MalformedFormatStringVisitor();
65+
}
6666

67-
private static class MalformedFormatStringVisitor extends BaseInspectionVisitor {
67+
private static class MalformedFormatStringVisitor extends BaseInspectionVisitor {
6868

69-
@Override
70-
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
71-
super.visitMethodCallExpression(expression);
72-
if (!FormatUtils.isFormatCall(expression)) {
73-
return;
74-
}
75-
final PsiExpressionList argumentList = expression.getArgumentList();
76-
final PsiExpression[] arguments = argumentList.getExpressions();
77-
if (arguments.length == 0) {
78-
return;
79-
}
80-
final PsiExpression firstArgument = arguments[0];
81-
final PsiType type = firstArgument.getType();
82-
if (type == null) {
83-
return;
84-
}
85-
final int formatArgumentIndex;
86-
if ("java.util.Locale".equals(type.getCanonicalText()) && arguments.length > 1) {
87-
formatArgumentIndex = 1;
88-
}
89-
else {
90-
formatArgumentIndex = 0;
91-
}
92-
final PsiExpression formatArgument = arguments[formatArgumentIndex];
93-
if (!ExpressionUtils.hasStringType(formatArgument)) {
94-
return;
95-
}
96-
if (!PsiUtil.isConstantExpression(formatArgument)) {
97-
return;
98-
}
99-
final PsiType formatType = formatArgument.getType();
100-
if (formatType == null) {
101-
return;
102-
}
103-
final String value = (String)ConstantExpressionUtil.computeCastTo(formatArgument, formatType);
104-
if (value == null) {
105-
return;
106-
}
107-
final int argumentCount = arguments.length - (formatArgumentIndex + 1);
108-
final Validator[] validators;
109-
try {
110-
validators = FormatDecode.decode(value, argumentCount);
111-
}
112-
catch (Exception e) {
113-
registerError(formatArgument, e);
114-
return;
115-
}
116-
if (validators.length != argumentCount) {
117-
if (argumentCount == 1) {
118-
final PsiExpression argument = arguments[formatArgumentIndex + 1];
119-
final PsiType argumentType = argument.getType();
120-
if (argumentType instanceof PsiArrayType) {
121-
return;
122-
}
123-
}
124-
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
125-
return;
126-
}
127-
for (int i = 0; i < validators.length; i++) {
128-
final Validator validator = validators[i];
129-
final PsiType argumentType = arguments[i + formatArgumentIndex + 1].getType();
130-
if (argumentType == null) {
131-
continue;
132-
}
133-
if (!validator.valid(argumentType)) {
134-
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
135-
return;
69+
@Override
70+
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
71+
super.visitMethodCallExpression(expression);
72+
if (!FormatUtils.isFormatCall(expression)) {
73+
return;
74+
}
75+
final PsiExpressionList argumentList = expression.getArgumentList();
76+
final PsiExpression[] arguments = argumentList.getExpressions();
77+
if (arguments.length == 0) {
78+
return;
79+
}
80+
final PsiExpression firstArgument = arguments[0];
81+
final PsiType type = firstArgument.getType();
82+
if (type == null) {
83+
return;
84+
}
85+
final int formatArgumentIndex;
86+
if ("java.util.Locale".equals(type.getCanonicalText()) && arguments.length > 1) {
87+
formatArgumentIndex = 1;
88+
}
89+
else {
90+
formatArgumentIndex = 0;
91+
}
92+
final PsiExpression formatArgument = arguments[formatArgumentIndex];
93+
if (!ExpressionUtils.hasStringType(formatArgument)) {
94+
return;
95+
}
96+
if (!PsiUtil.isConstantExpression(formatArgument)) {
97+
return;
98+
}
99+
final PsiType formatType = formatArgument.getType();
100+
if (formatType == null) {
101+
return;
102+
}
103+
final String value = (String) ConstantExpressionUtil.computeCastTo(formatArgument, formatType);
104+
if (value == null) {
105+
return;
106+
}
107+
final int argumentCount = arguments.length - (formatArgumentIndex + 1);
108+
final Validator[] validators;
109+
try {
110+
validators = FormatDecode.decode(value, argumentCount);
111+
}
112+
catch (Exception e) {
113+
registerError(formatArgument, e);
114+
return;
115+
}
116+
if (validators.length != argumentCount) {
117+
if (argumentCount == 1) {
118+
final PsiExpression argument = arguments[formatArgumentIndex + 1];
119+
final PsiType argumentType = argument.getType();
120+
if (argumentType instanceof PsiArrayType) {
121+
return;
122+
}
123+
}
124+
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
125+
return;
126+
}
127+
for (int i = 0; i < validators.length; i++) {
128+
final Validator validator = validators[i];
129+
final PsiType argumentType = arguments[i + formatArgumentIndex + 1].getType();
130+
if (argumentType == null) {
131+
continue;
132+
}
133+
if (!validator.valid(argumentType)) {
134+
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
135+
return;
136+
}
137+
}
136138
}
137-
}
138139
}
139-
}
140140
}

plugin/src/main/java/com/intellij/java/impl/ig/bugs/MalformedRegexInspection.java

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,76 +27,76 @@
2727
import com.siyeh.ig.psiutils.MethodCallUtils;
2828
import com.siyeh.localize.InspectionGadgetsLocalize;
2929
import consulo.annotation.component.ExtensionImpl;
30+
import consulo.localize.LocalizeValue;
3031
import jakarta.annotation.Nonnull;
3132

3233
import java.util.regex.Pattern;
3334
import java.util.regex.PatternSyntaxException;
3435

3536
@ExtensionImpl
3637
public class MalformedRegexInspection extends BaseInspection {
38+
@Nonnull
39+
@Override
40+
public LocalizeValue getDisplayName() {
41+
return InspectionGadgetsLocalize.malformedRegularExpressionDisplayName();
42+
}
3743

38-
@Override
39-
@Nonnull
40-
public String getDisplayName() {
41-
return InspectionGadgetsLocalize.malformedRegularExpressionDisplayName().get();
42-
}
43-
44-
@Override
45-
@Nonnull
46-
public String buildErrorString(Object... infos) {
47-
return infos.length == 0
48-
? InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor1().get()
49-
: InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor2(infos[0]).get();
50-
}
44+
@Override
45+
@Nonnull
46+
public String buildErrorString(Object... infos) {
47+
return infos.length == 0
48+
? InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor1().get()
49+
: InspectionGadgetsLocalize.malformedRegularExpressionProblemDescriptor2(infos[0]).get();
50+
}
5151

52-
@Override
53-
public boolean isEnabledByDefault() {
54-
return true;
55-
}
52+
@Override
53+
public boolean isEnabledByDefault() {
54+
return true;
55+
}
5656

57-
@Override
58-
public BaseInspectionVisitor buildVisitor() {
59-
return new MalformedRegexVisitor();
60-
}
57+
@Override
58+
public BaseInspectionVisitor buildVisitor() {
59+
return new MalformedRegexVisitor();
60+
}
6161

62-
private static class MalformedRegexVisitor extends BaseInspectionVisitor {
62+
private static class MalformedRegexVisitor extends BaseInspectionVisitor {
6363

64-
@Override
65-
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
66-
super.visitMethodCallExpression(expression);
67-
final PsiExpressionList argumentList = expression.getArgumentList();
68-
if (argumentList == null) {
69-
return;
70-
}
71-
final PsiExpression[] arguments = argumentList.getExpressions();
72-
if (arguments.length == 0) {
73-
return;
74-
}
75-
final PsiExpression argument = arguments[0];
76-
if (!ExpressionUtils.hasStringType(argument)) {
77-
return;
78-
}
79-
if (!PsiUtil.isConstantExpression(argument)) {
80-
return;
81-
}
82-
final PsiType regexType = argument.getType();
83-
final String value = (String)ConstantExpressionUtil.computeCastTo(argument, regexType);
84-
if (value == null) {
85-
return;
86-
}
87-
if (!MethodCallUtils.isCallToRegexMethod(expression)) {
88-
return;
89-
}
90-
//noinspection UnusedCatchParameter,ProhibitedExceptionCaught
91-
try {
92-
Pattern.compile(value);
93-
}
94-
catch (PatternSyntaxException e) {
95-
registerError(argument, e.getDescription());
96-
}
97-
catch (NullPointerException e) {
98-
registerError(argument); // due to a bug in the sun regex code
99-
}
64+
@Override
65+
public void visitMethodCallExpression(@Nonnull PsiMethodCallExpression expression) {
66+
super.visitMethodCallExpression(expression);
67+
final PsiExpressionList argumentList = expression.getArgumentList();
68+
if (argumentList == null) {
69+
return;
70+
}
71+
final PsiExpression[] arguments = argumentList.getExpressions();
72+
if (arguments.length == 0) {
73+
return;
74+
}
75+
final PsiExpression argument = arguments[0];
76+
if (!ExpressionUtils.hasStringType(argument)) {
77+
return;
78+
}
79+
if (!PsiUtil.isConstantExpression(argument)) {
80+
return;
81+
}
82+
final PsiType regexType = argument.getType();
83+
final String value = (String) ConstantExpressionUtil.computeCastTo(argument, regexType);
84+
if (value == null) {
85+
return;
86+
}
87+
if (!MethodCallUtils.isCallToRegexMethod(expression)) {
88+
return;
89+
}
90+
//noinspection UnusedCatchParameter,ProhibitedExceptionCaught
91+
try {
92+
Pattern.compile(value);
93+
}
94+
catch (PatternSyntaxException e) {
95+
registerError(argument, e.getDescription());
96+
}
97+
catch (NullPointerException e) {
98+
registerError(argument); // due to a bug in the sun regex code
99+
}
100+
}
100101
}
101-
}
102102
}

0 commit comments

Comments
 (0)