'Groovy overly long methods' inspection fixed
authorMaxim Medvedev <maxim.medvedev@jetbrains.com>
Wed, 2 Dec 2009 13:19:01 +0000 (16:19 +0300)
committerMaxim Medvedev <maxim.medvedev@jetbrains.com>
Wed, 2 Dec 2009 13:19:01 +0000 (16:19 +0300)
plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/metrics/GroovyOverlyLongMethodInspection.java
plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.java
plugins/groovy/testdata/highlighting/OverlyLongMethodInspection.groovy [new file with mode: 0644]

index dca2e3d035fbe640bb3e42549df2e26685690e99..156ce3fbc2354a4611438dfd11aea701ca403b07 100644 (file)
@@ -17,6 +17,7 @@ package org.jetbrains.plugins.groovy.codeInspection.metrics;
 
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor;
+import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
 
 public class GroovyOverlyLongMethodInspection extends GroovyMethodMetricInspection {
@@ -48,16 +49,18 @@ public class GroovyOverlyLongMethodInspection extends GroovyMethodMetricInspecti
   }
 
   private class Visitor extends BaseInspectionVisitor {
-    public void visitMethod(GrMethod grMethod) {
-      super.visitMethod(grMethod);
+    public void visitMethod(GrMethod method) {
+      super.visitMethod(method);
       final int limit = getLimit();
       final StatementCountVisitor visitor = new StatementCountVisitor();
-      grMethod.accept(visitor);
+      final GrOpenBlock block = method.getBlock();
+      if (block == null) return;
+      block.accept(visitor);
       final int statementCount = visitor.getStatementCount();
       if (statementCount <= limit) {
         return;
       }
-      registerMethodError(grMethod, statementCount, limit);
+      registerMethodError(method, statementCount, limit);
     }
   }
 }
\ No newline at end of file
index 4f478a88e23b8ab9ee880655d40476b3ade2390b..d9ae1ed39ecfad2247667a6963fe759f352eefbf 100644 (file)
@@ -20,6 +20,7 @@ import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilit
 import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyUncheckedAssignmentOfMemberOfRawTypeInspection;
 import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialConditionalInspection;
 import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspection;
+import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection;
 import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection;
 import org.jetbrains.plugins.groovy.codeInspection.unassignedVariable.UnassignedVariableAccessInspection;
 import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUnresolvedAccessInspection;
@@ -193,4 +194,8 @@ public class GroovyHighlightingTest extends LightCodeInsightFixtureTestCase {
   public void testEverythingAssignableToString() throws Exception {doTest(new GroovyAssignabilityCheckInspection());}
 
   public void testMethodCallWithDefaultParameters() throws Exception {doTest();}
+
+  public void testOverlyLongMethodInspection() throws Exception {
+    doTest(new GroovyOverlyLongMethodInspection());
+  }
 }
\ No newline at end of file
diff --git a/plugins/groovy/testdata/highlighting/OverlyLongMethodInspection.groovy b/plugins/groovy/testdata/highlighting/OverlyLongMethodInspection.groovy
new file mode 100644 (file)
index 0000000..3b3c8e7
--- /dev/null
@@ -0,0 +1,60 @@
+def <warning descr="Method 'foo' is too long ( statement count =48>30)">foo</warning>() {
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+}
+
+def ok() {
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;
+  print 1;  
+}
\ No newline at end of file