Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MethodInvocationCounter Metric to Track Method Invocations within Classes #120

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/github/mauricioaniche/ck/CKClassResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class CKClassResult {
private float tightClassCohesion;
private float looseClassCohesion;

private String methodInvocations;

public CKClassResult(String file, String className, String type, int modifiers) {
this.file = file;
this.className = className;
Expand Down Expand Up @@ -539,4 +541,12 @@ public int hashCode() {
return Objects.hash(file, className, type);
}

public void setMethodInvocation(String methodInvocations) {
this.methodInvocations = methodInvocations;
}

public String getMethodInvocations() {
return this.methodInvocations;
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/github/mauricioaniche/ck/ResultWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ResultWriter {
"abstractMethodsQty",
"finalMethodsQty",
"synchronizedMethodsQty",
"methodInvocations",

/* Field Counting */
"totalFieldsQty",
Expand Down Expand Up @@ -178,6 +179,7 @@ public void printResult(CKClassResult result) throws IOException {
result.getNumberOfAbstractMethods(),
result.getNumberOfFinalMethods(),
result.getNumberOfSynchronizedMethods(),
result.getMethodInvocations(),

/* Field Counting */
result.getNumberOfFields(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.mauricioaniche.ck.metric;

import com.github.mauricioaniche.ck.CKClassResult;
import com.github.mauricioaniche.ck.util.MethodCounter;
import org.eclipse.jdt.core.dom.*;

import java.util.*;

public class MethodInvocationCounter implements CKASTVisitor, ClassLevelMetric {
private Map<String, MethodCounter.MethodInformation> methodInvocations = new HashMap<>();
private String currentMethod = null;

@Override
public void visit(MethodDeclaration node) {
// Set the current method context
this.currentMethod = node.getName().getIdentifier();
methodInvocations.putIfAbsent(currentMethod, new MethodCounter().new MethodInformation(currentMethod));
}

@Override
public void visit(MethodInvocation node) {
if (currentMethod != null) {
// Retrieve or create the MethodInformation for the current method
MethodCounter.MethodInformation info = methodInvocations.get(currentMethod);
String methodName = node.getName().getIdentifier();
Map<String, Integer> counts = info.getMethodInvocations();
counts.put(methodName, counts.getOrDefault(methodName, 0) + 1);
}
}

@Override
public void setResult(CKClassResult result) {
// Process all collected method invocation information into the desired format
List<MethodCounter.MethodInformation> infos = new ArrayList<>(methodInvocations.values());
String formattedResult = MethodCounter.formatResult(infos);
result.setMethodInvocation(formattedResult);
}
}
77 changes: 77 additions & 0 deletions src/main/java/com/github/mauricioaniche/ck/util/MethodCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.github.mauricioaniche.ck.util;

import java.util.*;
import java.util.stream.Collectors;

public class MethodCounter {
public class MethodInformation {
private String parentName;
private Map<String, Integer> methodInvocations = new HashMap<>();

public MethodInformation(String parentName) {
this.parentName = parentName;
}

public String getParentName() {
return parentName;
}

public Map<String, Integer> getMethodInvocations() {
return methodInvocations;
}

@Override
public String toString() {
return "MethodInformation{" +
"parentName='" + parentName + '\'' +
", methodInvocation=" + methodInvocations +
'}';
}

public String toFormattedString() {
return parentName + "[ " + formatMethods(sortMethods(methodInvocations)) + " ] ";
}

private Map<String, Integer> sortMethods(Map<String, Integer> methodInvocation) {
return methodInvocation.entrySet().stream()
.sorted((Map.Entry.<String, Integer>comparingByValue().reversed()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}

private String formatMethods(Map<String, Integer> methodInvocation) {
return methodInvocation.entrySet().stream()
.map(entry -> entry.getKey() + "()" + ":" + entry.getValue())
.collect(Collectors.joining(" "));
}

}

public static String formatResult(List<MethodInformation> methodInformations) {
return methodInformations.stream().map(MethodInformation::toFormattedString).collect(Collectors.joining());
}
public static List<MethodInformation> count(String methodList) {
List<MethodInformation> methodInformationList = new ArrayList<>();

String[] methodNames = methodList.split(";");
for (String name : methodNames) {
String[] parts = name.split("/");
String methodName = parts[0];
String parentName = parts[1];

MethodInformation methodInformation = new MethodCounter().new MethodInformation(parentName);
if (methodInformationList.contains(methodInformation)) {
methodInformation = methodInformationList.get(methodInformationList.indexOf(methodInformation));
} else {
methodInformationList.add(methodInformation);
}

if (methodInformation.getMethodInvocations().containsKey(methodName)) {
methodInformation.getMethodInvocations().put(methodName, methodInformation.getMethodInvocations().get(methodName) + 1);
} else {
methodInformation.getMethodInvocations().put(methodName, 1);
}
}

return methodInformationList;
}
}