From 244f808a8d3a14fddc6e07f856ee1a7286fb5d79 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 13 May 2015 16:46:04 +0200 Subject: [PATCH] IG: new "Lambda parameter naming convention" inspection --- .../siyeh/InspectionGadgetsBundle.properties | 9 +- ...rameterNamingConventionInspectionBase.java | 98 +++++++++++++++++++ ...daParameterNamingConventionInspection.java | 30 ++++++ .../LambdaParameterNamingConvention.html | 13 +++ .../LambdaParameterNamingConvention.java | 12 +++ ...rameterNamingConventionInspectionTest.java | 39 ++++++++ 6 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionBase.java create mode 100644 plugins/InspectionGadgets/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspection.java create mode 100644 plugins/InspectionGadgets/src/inspectionDescriptions/LambdaParameterNamingConvention.html create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/naming/lambda_parameter_naming_convention/LambdaParameterNamingConvention.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 0053016cc939..dfbdf6054b4e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -2159,5 +2159,10 @@ array.objects.equals.display.name='Objects.equals()' called on arrays array.objects.equals.problem.descriptor=Objects.#ref() on arrays should probably be 'Arrays.equals()' #loc array.objects.deep.equals.problem.descriptor=Objects.#ref() on arrays should probably be 'Arrays.deepEquals()' #loc extends.throwable.display.name=Class directly extends 'java.lang.Throwable' -anonymous.extends.throwable.problem.descriptor=Anonymous class directly extends 'java.lang.Throwable' \#loc -extends.throwable.problem.descriptor=class \#ref directly extends 'java.lang.Throwable' \#loc \ No newline at end of file +anonymous.extends.throwable.problem.descriptor=Anonymous class directly extends 'java.lang.Throwable' #loc +extends.throwable.problem.descriptor=class #ref directly extends 'java.lang.Throwable' #loc +lambda.parameter.naming.convention.display.name=Lambda parameter naming convention +lambda.parameter.naming.convention.problem.descriptor.short=Lambda parameter name #ref is too short #loc +lambda.parameter.naming.convention.problem.descriptor.long=Lambda parameter name #ref is too long #loc +lambda.parameter.naming.convention.problem.descriptor.regex.mismatch=Lambda parameter name #ref doesn''t match regex ''{0}'' #loc + diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionBase.java new file mode 100644 index 000000000000..6434ba6a21bc --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionBase.java @@ -0,0 +1,98 @@ +/* + * Copyright 2000-2015 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.naming; + +import com.intellij.psi.*; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspectionVisitor; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class LambdaParameterNamingConventionInspectionBase extends ConventionInspection { + + private static final int DEFAULT_MIN_LENGTH = 1; + private static final int DEFAULT_MAX_LENGTH = 20; + + @Override + @NotNull + public String getID() { + return "LambdaParameterNamingConvention"; + } + + @Override + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.display.name"); + } + + @Override + protected boolean buildQuickFixesOnlyForOnTheFlyErrors() { + return true; + } + + @Override + @NotNull + public String buildErrorString(Object... infos) { + final String parameterName = (String)infos[0]; + if (parameterName.length() < getMinLength()) { + return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.problem.descriptor.short"); + } + else if (parameterName.length() > getMaxLength()) { + return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.problem.descriptor.long"); + } + else { + return InspectionGadgetsBundle.message("lambda.parameter.naming.convention.problem.descriptor.regex.mismatch", getRegex()); + } + } + + @Override + protected String getDefaultRegex() { + return "[a-z][A-Za-z\\d]*"; + } + + @Override + protected int getDefaultMinLength() { + return DEFAULT_MIN_LENGTH; + } + + @Override + protected int getDefaultMaxLength() { + return DEFAULT_MAX_LENGTH; + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new NamingConventionsVisitor(); + } + + private class NamingConventionsVisitor extends BaseInspectionVisitor { + + @Override + public void visitParameter(@NotNull PsiParameter variable) { + final PsiElement scope = variable.getDeclarationScope(); + if (!(scope instanceof PsiLambdaExpression)) { + return; + } + final String name = variable.getName(); + if (name == null || isValid(name)) { + return; + } + registerVariableError(variable, name); + } + } +} diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspection.java new file mode 100644 index 000000000000..036aeda24bb2 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/naming/LambdaParameterNamingConventionInspection.java @@ -0,0 +1,30 @@ +/* + * Copyright 2003-2015 Dave Griffith, Bas Leijdekkers + * + * 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.naming; + +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.RenameFix; + +/** + * @author Bas Leijdekkers + */ +public class LambdaParameterNamingConventionInspection extends LambdaParameterNamingConventionInspectionBase { + + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + return new RenameFix(); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/LambdaParameterNamingConvention.html b/plugins/InspectionGadgets/src/inspectionDescriptions/LambdaParameterNamingConvention.html new file mode 100644 index 000000000000..79adb48a71d9 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/LambdaParameterNamingConvention.html @@ -0,0 +1,13 @@ + + +Reports lambda parameters whose names are either too short, too long, or do not follow +the specified regular expression pattern. + +

+Use the fields below to specify minimum length, maximum length and regular expression expected for method parameter names. +Specify 0 to not check the length of names. Regular expressions are in standard java.util.regex format. +

+New in 15 +

+ + \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/naming/lambda_parameter_naming_convention/LambdaParameterNamingConvention.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/naming/lambda_parameter_naming_convention/LambdaParameterNamingConvention.java new file mode 100644 index 000000000000..02fd7e3fd974 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/naming/lambda_parameter_naming_convention/LambdaParameterNamingConvention.java @@ -0,0 +1,12 @@ +public class LambdaParameterNamingConvention { + + void m(int a) {} + void n(int abcd) { + F f = (i) -> 10; + F g = abc -> 12; + } + + interface F { + int a(int i); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionTest.java new file mode 100644 index 000000000000..727985fbf22c --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/naming/LambdaParameterNamingConventionInspectionTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2015 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.naming; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import junit.framework.TestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class LambdaParameterNamingConventionInspectionTest extends LightInspectionTestCase { + + public void testLambdaParameterNamingConvention() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + final LambdaParameterNamingConventionInspection inspection = new LambdaParameterNamingConventionInspection(); + inspection.m_minLength = 2; + return inspection; + } +} \ No newline at end of file