From 7587ccc73f9562b4b3e3be6e8081859c8c1b4116 Mon Sep 17 00:00:00 2001 From: Dinesh Bolkensteyn Date: Mon, 24 Oct 2011 22:16:14 +0200 Subject: [PATCH] mend SONAR-2038 LCOM4 value is incorrect when mixing getter/setter and direct field access --- .../java/bytecode/visitor/LCOM4Visitor.java | 37 ++++++++++++------- .../squid/bridges/Lcom4BlocksBridge.java | 4 +- 2 files changed, 25 insertions(+), 16 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 a3598b3da12..1c1630d197a 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 @@ -48,23 +48,29 @@ public class LCOM4Visitor extends BytecodeVisitor { unrelatedBlocks = new ArrayList>(); } - public void visitMethod(AsmMethod asmMethod) { + public void processMethod(AsmMethod asmMethod) { if (isMethodElligibleForLCOM4Computation(asmMethod)) { ensureBlockIsCreated(asmMethod); for (AsmEdge edge : asmMethod.getOutgoingEdges()) { if (isCallToInternalFieldOrMethod(edge) && isNotCallToExcludedFieldFromLcom4Calculation(edge.getTo())) { - AsmResource toResource = edge.getTo(); + AsmResource toResource = getAccessedFieldOrMethod(edge.getTo()); linkAsmResources(asmMethod, toResource); } } - } else if (asmMethod.isAccessor()) { - linkAsmResources(asmMethod, asmMethod.getAccessedField()); } } - + + private AsmResource getAccessedFieldOrMethod(AsmResource resource) { + if (resource instanceof AsmMethod && ((AsmMethod)resource).isAccessor()) { + return ((AsmMethod)resource).getAccessedField(); + } else { + return resource; + } + } + private boolean isNotCallToExcludedFieldFromLcom4Calculation(AsmResource to) { if (to instanceof AsmField) { - AsmField field = (AsmField) to; + AsmField field = (AsmField)to; return !fieldsToExcludeFromLcom4Calculation.contains(field.getName()); } return true; @@ -76,6 +82,10 @@ public class LCOM4Visitor extends BytecodeVisitor { } public void leaveClass(AsmClass asmClass) { + for (AsmMethod asmMethod: asmClass.getMethods()) { + processMethod(asmMethod); + } + int lcom4 = unrelatedBlocks.size(); if (lcom4 == 0) { lcom4 = 1; @@ -105,21 +115,20 @@ public class LCOM4Visitor extends BytecodeVisitor { blockA.addAll(blockB); unrelatedBlocks.remove(blockB); } - + private boolean isCallToInternalFieldOrMethod(AsmEdge edge) { - return edge.getTargetAsmClass() == asmClass - && (edge.getUsage() == SourceCodeEdgeUsage.CALLS_FIELD || edge.getUsage() == SourceCodeEdgeUsage.CALLS_METHOD); + return edge.getTargetAsmClass() == asmClass && (edge.getUsage() == SourceCodeEdgeUsage.CALLS_FIELD || edge.getTargetAsmClass() == asmClass && edge.getUsage() == SourceCodeEdgeUsage.CALLS_METHOD); } - private Set getOrCreateResourceBlock(AsmResource fromResource) { - for (Set block : unrelatedBlocks) { - if (block.contains(fromResource)) { + private Set getOrCreateResourceBlock(AsmResource resource) { + for (Set block: unrelatedBlocks) { + if (block.contains(resource)) { return block; } } - + Set block = new HashSet(); - block.add(fromResource); + block.add(resource); unrelatedBlocks.add(block); return block; } diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Lcom4BlocksBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Lcom4BlocksBridge.java index dd11d3d9122..4286600ffd3 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Lcom4BlocksBridge.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Lcom4BlocksBridge.java @@ -121,9 +121,9 @@ public class Lcom4BlocksBridge extends Bridge { private int compareType(AsmResource asmResource1, AsmResource asmResource2) { if (asmResource1 instanceof AsmField) { - return (asmResource2 instanceof AsmField ? 0 : -1); + return (asmResource2 instanceof AsmField) ? 0 : -1; } - return (asmResource2 instanceof AsmMethod ? 0 : 1); + return (asmResource2 instanceof AsmMethod) ? 0 : 1; } }); -- 2.39.5