aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorDinesh Bolkensteyn <dinesh@dinsoft.net>2011-10-24 22:16:14 +0200
committerDinesh Bolkensteyn <dinesh@dinsoft.net>2011-10-24 22:32:55 +0200
commit7587ccc73f9562b4b3e3be6e8081859c8c1b4116 (patch)
tree20e1dcf5ba8d28177352d59c2733ba5abecaebab /plugins
parentdeedcd037c00fdb4cc83aa8d2098053c4e49519e (diff)
downloadsonarqube-7587ccc73f9562b4b3e3be6e8081859c8c1b4116.tar.gz
sonarqube-7587ccc73f9562b4b3e3be6e8081859c8c1b4116.zip
mend
SONAR-2038 LCOM4 value is incorrect when mixing getter/setter and direct field access
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/visitor/LCOM4Visitor.java37
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/Lcom4BlocksBridge.java4
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<Set<AsmResource>>();
}
- 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<AsmResource> getOrCreateResourceBlock(AsmResource fromResource) {
- for (Set<AsmResource> block : unrelatedBlocks) {
- if (block.contains(fromResource)) {
+ private Set<AsmResource> getOrCreateResourceBlock(AsmResource resource) {
+ for (Set<AsmResource> block: unrelatedBlocks) {
+ if (block.contains(resource)) {
return block;
}
}
-
+
Set<AsmResource> block = new HashSet<AsmResource>();
- 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;
}
});