From b16ae96559abfbbbb14a84feef6c6f6f0cf7a26a Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 29 Sep 2016 16:40:22 +0200 Subject: [PATCH] IG: new "Overly long lambda expression" inspection --- .../src/META-INF/InspectionGadgets.xml | 3 + .../siyeh/InspectionGadgetsBundle.properties | 4 +- .../OverlyLongLambdaInspection.java | 80 +++++++++++++++++++ .../OverlyLongLambda.html | 12 +++ .../OverlyLongLambdaInspectionTest.java | 41 ++++++++++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspection.java create mode 100644 plugins/InspectionGadgets/src/inspectionDescriptions/OverlyLongLambda.html create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index 333249ada396..9e9477942980 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -1631,6 +1631,9 @@ key="non.comment.source.statements.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.method.metrics" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.methodmetrics.NonCommentSourceStatementsInspection"/> + #ref contains logic but is missing a 'return' statement \ No newline at end of file +method.missing.return.statement.problem.descriptor=Method #ref contains logic but is missing a 'return' statement +overly.long.lambda.display.name=Overly long lambda expression +overly.long.lambda.problem.descriptor=Lambda expression is too long (# Non-comment source statements = {0}) #loc \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspection.java new file mode 100644 index 000000000000..67da5b40dd8d --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspection.java @@ -0,0 +1,80 @@ +/* + * Copyright 2000-2016 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.methodmetrics; + +import com.intellij.psi.*; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspectionVisitor; +import org.jetbrains.annotations.NotNull; + +/** + * @author Bas Leijdekkers + */ +public class OverlyLongLambdaInspection extends MethodMetricInspection { + + private static final int DEFAULT_LIMIT = 3; + + @Override + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message("overly.long.lambda.display.name"); + } + + @Override + protected int getDefaultLimit() { + return DEFAULT_LIMIT; + } + + @Override + protected String getConfigurationLabel() { + return InspectionGadgetsBundle.message("non.comment.source.statements.limit.option"); + } + + @Override + @NotNull + public String buildErrorString(Object... infos) { + final Integer statementCount = (Integer)infos[0]; + return InspectionGadgetsBundle.message("overly.long.lambda.problem.descriptor", statementCount); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new OverlyLongLambdaVisitor(); + } + + private class OverlyLongLambdaVisitor extends BaseInspectionVisitor { + + @Override + public void visitLambdaExpression(PsiLambdaExpression expression) { + final PsiElement body = expression.getBody(); + if (!(body instanceof PsiCodeBlock)) { + return; + } + final PsiCodeBlock block = (PsiCodeBlock)body; + final PsiJavaToken brace = block.getLBrace(); + if (brace == null) { + return; + } + final NCSSVisitor visitor = new NCSSVisitor(); + block.accept(visitor); + final int count = visitor.getStatementCount(); + if (count <= getLimit()) { + return; + } + registerErrorAtOffset(expression, 0, body.getStartOffsetInParent() + 1, Integer.valueOf(count)); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/OverlyLongLambda.html b/plugins/InspectionGadgets/src/inspectionDescriptions/OverlyLongLambda.html new file mode 100644 index 000000000000..970edba93a50 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/OverlyLongLambda.html @@ -0,0 +1,12 @@ + + +Reports lambda expressions that are too long. +Lambda expressions that are too long +may be confusing, and it is often better to extract the statements into their own method. + +

+Use the field below to specify the maximum number of non-comment source statements a lambda expression is allowed to have. +

+New in 2016.3 + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspectionTest.java new file mode 100644 index 000000000000..13f47938d78f --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/methodmetrics/OverlyLongLambdaInspectionTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2016 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.methodmetrics; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class OverlyLongLambdaInspectionTest extends LightInspectionTestCase { + + public void testSimple() { + doStatementTest("Runnable r = /*Lambda is too long (# Non-comment source statements = 4)*/() -> {/**/" + + " System.out.println();" + + " System.out.println();" + + " System.out.println();" + + " System.out.println();" + + "};"); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new OverlyLongLambdaInspection(); + } +} \ No newline at end of file