From 2b65589c5e5549ac2bd7db4e858feb66bd6dbab4 Mon Sep 17 00:00:00 2001 From: Dinesh Bolkensteyn Date: Wed, 26 Oct 2011 20:38:50 +0200 Subject: SONAR-2941 Exclude isolated methods from LCOM4 computation --- .../org/sonar/java/bytecode/visitor/LCOM4Visitor.java | 19 +++++++++++++++---- .../org/sonar/java/bytecode/BytecodeVisitorsTest.java | 6 +++--- .../sonar/java/bytecode/visitor/LCOM4VisitorTest.java | 4 ++++ 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'plugins/sonar-squid-java-plugin/src') diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/visitor/LCOM4Visitor.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/visitor/LCOM4Visitor.java index dd124321073..2bfd6d1cde6 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/visitor/LCOM4Visitor.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/visitor/LCOM4Visitor.java @@ -19,10 +19,7 @@ */ package org.sonar.java.bytecode.visitor; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import org.sonar.java.bytecode.asm.AsmClass; import org.sonar.java.bytecode.asm.AsmEdge; @@ -80,8 +77,22 @@ public class LCOM4Visitor extends BytecodeVisitor { return !asmMethod.isAbstract() && !asmMethod.isStatic() && !asmMethod.isConstructor() && !asmMethod.isEmpty() && !asmMethod.isAccessor() && asmMethod.isBodyLoaded(); } + + private void removeIsolatedMethodBlocks() { + Iterator> iterator = unrelatedBlocks.iterator(); + + while (iterator.hasNext()) { + Set block = iterator.next(); + if (block.size() == 1) { + iterator.remove(); + } + } + + } public void leaveClass(AsmClass asmClass) { + removeIsolatedMethodBlocks(); + int lcom4 = unrelatedBlocks.size(); if (lcom4 == 0) { lcom4 = 1; diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/BytecodeVisitorsTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/BytecodeVisitorsTest.java index c7f04fa841c..d3cad104ead 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/BytecodeVisitorsTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/BytecodeVisitorsTest.java @@ -74,11 +74,11 @@ public class BytecodeVisitorsTest { @Test public void testLCOM4Visitor() { - assertEquals(2, squid.search("tags/impl/Todo").getInt(Metric.LCOM4)); - assertEquals(2, squid.search("tags/impl/Todo.java").getInt(Metric.LCOM4)); + assertEquals(1, squid.search("tags/impl/Todo").getInt(Metric.LCOM4)); + assertEquals(1, squid.search("tags/impl/Todo.java").getInt(Metric.LCOM4)); List> lcom4Blocks = (List>) squid.search("tags/impl/Todo.java").getData(Metric.LCOM4_BLOCKS); - assertEquals(2, lcom4Blocks.size()); + assertEquals(0, lcom4Blocks.size()); assertEquals(1, squid.search("tags/Tag").getInt(Metric.LCOM4)); assertEquals(1, squid.search("tags/TagName").getInt(Metric.LCOM4)); diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/visitor/LCOM4VisitorTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/visitor/LCOM4VisitorTest.java index 0bd61e77a63..5ffc439b41b 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/visitor/LCOM4VisitorTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/visitor/LCOM4VisitorTest.java @@ -65,5 +65,9 @@ public class LCOM4VisitorTest { assertThat(squid.search("LCOM4WithWorkers").getInt(Metric.LCOM4), is(1)); } + @Test + public void testLCOM4WithIsolatedMethods() { + assertThat(squid.search("LCOM4WithIsolatedMethods").getInt(Metric.LCOM4), is(1)); + } } -- cgit v1.2.3