aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/visitor/LCOM4Visitor.java19
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/BytecodeVisitorsTest.java6
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/visitor/LCOM4VisitorTest.java4
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/ExclusionOfFieldNamesFromLcom4Calculation.classbin890 -> 1182 bytes
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4Exclusions.classbin695 -> 859 bytes
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithIsolatedMethods.classbin0 -> 573 bytes
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithWorkers.classbin0 -> 991 bytes
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/ExclusionOfFieldNamesFromLcom4Calculation.java21
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4Exclusions.java11
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithIsolatedMethods.java11
-rw-r--r--plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithWorkers.java27
11 files changed, 86 insertions, 13 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));
+ }
}
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/ExclusionOfFieldNamesFromLcom4Calculation.class b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/ExclusionOfFieldNamesFromLcom4Calculation.class
index f779fe90360..bfa2977a941 100644
--- a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/ExclusionOfFieldNamesFromLcom4Calculation.class
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/ExclusionOfFieldNamesFromLcom4Calculation.class
Binary files differ
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4Exclusions.class b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4Exclusions.class
index 1c3b11f2568..53c65ec0981 100644
--- a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4Exclusions.class
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4Exclusions.class
Binary files differ
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithIsolatedMethods.class b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithIsolatedMethods.class
new file mode 100644
index 00000000000..0e414fd437a
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithIsolatedMethods.class
Binary files differ
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithWorkers.class b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithWorkers.class
new file mode 100644
index 00000000000..7ab3f59ce13
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/bin/LCOM4WithWorkers.class
Binary files differ
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/ExclusionOfFieldNamesFromLcom4Calculation.java b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/ExclusionOfFieldNamesFromLcom4Calculation.java
index 8e0490f3ea2..760389da3bb 100644
--- a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/ExclusionOfFieldNamesFromLcom4Calculation.java
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/ExclusionOfFieldNamesFromLcom4Calculation.java
@@ -5,13 +5,28 @@ import java.util.logging.Logger;
public class ExclusionOfFieldNamesFromLcom4Calculation {
- private static final Logger LOG =Logger.getLogger("log");
+ private final Logger LOG = Logger.getLogger("log");
+ private boolean killAccessorBlockA;
+ private boolean killAccessorBlockB;
- public void firstMethod(){
+ public void firstMethodBlockA(){
+ secondMethodBlockA();
LOG.log(Level.INFO, "a message");
}
- public void secondMethod(){
+ public void secondMethodBlockA(){
LOG.log(Level.INFO, "a message");
+ killAccessorBlockA = true;
}
+
+ public void firstMethodBlockB(){
+ secondMethodBlockB();
+ LOG.log(Level.INFO, "a message");
+ }
+
+ public void secondMethodBlockB(){
+ LOG.log(Level.INFO, "a message");
+ killAccessorBlockB = true;
+ }
+
}
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4Exclusions.java b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4Exclusions.java
index aa1f6bffa26..25f4e8ed464 100644
--- a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4Exclusions.java
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4Exclusions.java
@@ -2,7 +2,8 @@
public abstract class LCOM4Exclusions implements Runnable {
- public static String field;
+ public static String field1;
+ public static String field2;
public LCOM4Exclusions(){}
@@ -13,10 +14,14 @@ public abstract class LCOM4Exclusions implements Runnable {
public abstract void doAbstractWork();
public void run(){
- int i =0;
+ run2();
+ }
+
+ public void run2(){
+ run();
}
public boolean equals(Object object){
- return false;
+ return (field1 == null) ? false : field1.equals(field2);
}
}
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithIsolatedMethods.java b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithIsolatedMethods.java
new file mode 100644
index 00000000000..6e310f01703
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithIsolatedMethods.java
@@ -0,0 +1,11 @@
+public class LCOM4WithIsolatedMethods {
+
+ public void myUnsupportedMethod(){
+ throw new UnsupportedOperationException("...");
+ }
+
+ public String myConstantValue() {
+ return "...";
+ }
+
+}
diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithWorkers.java b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithWorkers.java
new file mode 100644
index 00000000000..a4f966e0ed3
--- /dev/null
+++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/lcom4/src/LCOM4WithWorkers.java
@@ -0,0 +1,27 @@
+public class LCOM4WithWorkers {
+ private String field = "toto";
+
+ public String getField() {
+ return field;
+ }
+
+ public void doSomething() {
+ task1();
+ field = "tata";
+ }
+
+ public void doSomethingElse() {
+ task2();
+ getField();
+ }
+
+ public void task1() {
+ System.out.println("Hello 1");
+ System.out.println("Hello 2");
+ }
+
+ public void task2() {
+ System.out.println("Hello 1");
+ System.out.println("Hello 2");
+ }
+}