diff options
author | Dinesh Bolkensteyn <dinesh@dinsoft.net> | 2011-10-26 20:38:50 +0200 |
---|---|---|
committer | Dinesh Bolkensteyn <dinesh@dinsoft.net> | 2011-10-26 21:41:14 +0200 |
commit | 2b65589c5e5549ac2bd7db4e858feb66bd6dbab4 (patch) | |
tree | 0828d5ff3c251d4191dc4e95eb0f8d0aa7501585 /plugins/sonar-squid-java-plugin/src | |
parent | 0cbd0265b311210fb43ac6ff044005d5ca75d8ce (diff) | |
download | sonarqube-2b65589c5e5549ac2bd7db4e858feb66bd6dbab4.tar.gz sonarqube-2b65589c5e5549ac2bd7db4e858feb66bd6dbab4.zip |
SONAR-2941 Exclude isolated methods from LCOM4 computation
Diffstat (limited to 'plugins/sonar-squid-java-plugin/src')
3 files changed, 22 insertions, 7 deletions
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<Set<AsmResource>> iterator = unrelatedBlocks.iterator(); + + while (iterator.hasNext()) { + Set<AsmResource> 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<Set<AsmResource>> lcom4Blocks = (List<Set<AsmResource>>) 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)); + } } |