From 68dc91af5165da949b1f1c0685d4c911892fce03 Mon Sep 17 00:00:00 2001 From: Breno Farias da Silva Date: Sat, 30 Mar 2024 13:21:18 -0300 Subject: [PATCH 1/8] REFACTOR: Fixing bad identation in Runner.java --- src/main/java/com/github/mauricioaniche/ck/Runner.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/mauricioaniche/ck/Runner.java b/src/main/java/com/github/mauricioaniche/ck/Runner.java index e0cab409..c1343c06 100644 --- a/src/main/java/com/github/mauricioaniche/ck/Runner.java +++ b/src/main/java/com/github/mauricioaniche/ck/Runner.java @@ -37,11 +37,11 @@ public static void main(String[] args) throws IOException { if(args.length >= 5) outputDir = args[4]; - // load possible additional ignored directories - //noinspection ManualArrayToCollectionCopy - for (int i = 5; i < args.length; i++) { - FileUtils.IGNORED_DIRECTORIES.add(args[i]); - } + // load possible additional ignored directories + //noinspection ManualArrayToCollectionCopy + for (int i = 5; i < args.length; i++) { + FileUtils.IGNORED_DIRECTORIES.add(args[i]); + } ResultWriter writer = new ResultWriter(outputDir + "class.csv", outputDir + "method.csv", outputDir + "variable.csv", outputDir + "field.csv", variablesAndFields); From bc6119addced801ac2872a4c2f091137b119f73e Mon Sep 17 00:00:00 2001 From: Breno Farias da Silva Date: Sat, 30 Mar 2024 19:09:31 -0300 Subject: [PATCH 2/8] FIX: Updating the Java Version in the pom.xml file --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index f12790b8..769b41b5 100644 --- a/pom.xml +++ b/pom.xml @@ -131,8 +131,8 @@ maven-compiler-plugin 3.9.0 - 11 - 11 + 17 + 17 From f1336431acca9639b9c8b497d121337c60c8f77c Mon Sep 17 00:00:00 2001 From: Breno Farias da Silva Date: Sat, 30 Mar 2024 19:58:35 -0300 Subject: [PATCH 3/8] FIX: Updating the Java Version in the pom.xml file --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8d816aa1..6d9e738d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,10 +6,10 @@ jobs: steps: - name: Checkout the repository uses: actions/checkout@v2 - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v1 with: - java-version: 11 + java-version: 17 - name: Cache Maven packages uses: actions/cache@v2 with: From 4c330cb51aa4eb0d7dff79bf04fe964edb7abdba Mon Sep 17 00:00:00 2001 From: Breno Farias da Silva Date: Sat, 30 Mar 2024 20:00:04 -0300 Subject: [PATCH 4/8] DOCS: Updating the Build Badge in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 837d14ee..bb1b7953 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CK -[![Build Status](https://travis-ci.org/mauricioaniche/ck.svg?branch=master)](https://travis-ci.org/mauricioaniche/ck) +[![Build Status](https://img.shields.io/github/actions/workflow/status/BrenoFariasdaSilva/ck/test.yml) [![Code Coverage](https://codecov.io/github/mauricioaniche/ck/coverage.svg)](https://codecov.io/gh/mauricioaniche/ck) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.mauricioaniche/ck/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.mauricioaniche/ck) From 5b2db1d8410107f5859f2eed207929abd53ee46f Mon Sep 17 00:00:00 2001 From: Breno Farias <57010178+BrenoFariasdaSilva@users.noreply.github.com> Date: Sat, 30 Mar 2024 20:14:51 -0300 Subject: [PATCH 5/8] DOCS: Updating the Build Badge in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bb1b7953..c232a100 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CK -[![Build Status](https://img.shields.io/github/actions/workflow/status/BrenoFariasdaSilva/ck/test.yml) +![Build Status](https://img.shields.io/github/actions/workflow/status/BrenoFariasdaSilva/ck/test.yml) [![Code Coverage](https://codecov.io/github/mauricioaniche/ck/coverage.svg)](https://codecov.io/gh/mauricioaniche/ck) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.mauricioaniche/ck/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.mauricioaniche/ck) From ad84470dee68495554bd6e3e0248a3bc9f1590f9 Mon Sep 17 00:00:00 2001 From: Breno Farias da Silva Date: Sun, 31 Mar 2024 10:18:28 -0300 Subject: [PATCH 6/8] FEAT: Adding a counter to the number of times a method was invoked --- .../java/com/github/mauricioaniche/ck/CKVisitor.java | 3 +++ .../com/github/mauricioaniche/ck/ResultWriter.java | 12 ++++++++++-- .../java/com/github/mauricioaniche/ck/Runner.java | 3 ++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java b/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java index 0e53b66c..26f0c5ff 100644 --- a/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java +++ b/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java @@ -776,6 +776,9 @@ public boolean visit(MethodRefParameter node) { } public boolean visit(MethodInvocation node) { + // Add the method name to the map of the Runner.methodOccurrences + Runner.methodOccurrences.put(node.getName().toString(), Runner.methodOccurrences.getOrDefault(node.getName().toString(), 0) + 1); + if(!classes.isEmpty()) { classes.peek().classLevelMetrics.stream().map(metric -> (CKASTVisitor) metric).forEach(ast -> ast.visit(node)); if(!classes.peek().methods.isEmpty()) diff --git a/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java b/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java index e294c530..f84e23ea 100644 --- a/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java +++ b/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java @@ -104,7 +104,8 @@ public class ResultWriter { "uniqueWordsQty", "modifiers", "logStatementsQty", - "hasJavaDoc" }; + "hasJavaDoc", + "methodOccurrences" }; private static final String[] VAR_FIELD_HEADER = { "file", "class", "method", "variable", "usage" }; private final boolean variablesAndFields; @@ -211,6 +212,12 @@ public void printResult(CKClassResult result) throws IOException { result.getNumberOfLogStatements()); for (CKMethodResult method : result.getMethods()) { + // Extract the clean method name + String cleanMethodName = method.getMethodName().split("/")[0]; + + // Calculate occurrences count for the clean method name + Integer occurrences = Runner.methodOccurrences.getOrDefault(cleanMethodName, 0); + this.methodPrinter.printRecord( result.getFile(), result.getClassName(), @@ -245,7 +252,8 @@ public void printResult(CKClassResult result) throws IOException { method.getUniqueWordsQty(), method.getModifiers(), method.getLogStatementsQty(), - method.getHasJavadoc()); + method.getHasJavadoc(), + occurrences); if(variablesAndFields) { for (Map.Entry entry : method.getVariablesUsage().entrySet()) { diff --git a/src/main/java/com/github/mauricioaniche/ck/Runner.java b/src/main/java/com/github/mauricioaniche/ck/Runner.java index c1343c06..c7528ad1 100644 --- a/src/main/java/com/github/mauricioaniche/ck/Runner.java +++ b/src/main/java/com/github/mauricioaniche/ck/Runner.java @@ -7,7 +7,8 @@ import java.util.Map; public class Runner { - + public static Map methodOccurrences = new HashMap<>(); + public static void main(String[] args) throws IOException { if (args == null || args.length < 1) { From a1611a697a27e99de249991377393f58cfa457c2 Mon Sep 17 00:00:00 2001 From: Breno Farias da Silva Date: Sun, 31 Mar 2024 10:21:07 -0300 Subject: [PATCH 7/8] FEAT: Adding a counter to the number of times a class was instantiated --- .../java/com/github/mauricioaniche/ck/CKVisitor.java | 12 +++++++++++- .../com/github/mauricioaniche/ck/ResultWriter.java | 8 ++++++-- .../java/com/github/mauricioaniche/ck/Runner.java | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java b/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java index 26f0c5ff..371c2eac 100644 --- a/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java +++ b/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java @@ -63,7 +63,7 @@ public boolean visit(TypeDeclaration node) { int modifiers = node.getModifiers(); CKClassResult currentClass = new CKClassResult(sourceFilePath, className, type, modifiers); currentClass.setLoc(calculate(node.toString())); - + // there might be metrics that use it // (even before a class is declared) if(!classes.isEmpty()) { @@ -487,6 +487,16 @@ public boolean visit(CharacterLiteral node) { } public boolean visit(ClassInstanceCreation node) { + String className; + if (node.getType().resolveBinding() != null) { + className = node.getType().resolveBinding().getQualifiedName(); + } else { + className = node.getType().toString(); + } + + // Safely update the occurrences count for the class being instantiated. + Runner.classOccurrences.put(className, Runner.classOccurrences.getOrDefault(className, 0) + 1); + if(!classes.isEmpty()) { classes.peek().classLevelMetrics.stream().map(metric -> (CKASTVisitor) metric).forEach(ast -> ast.visit(node)); if(!classes.peek().methods.isEmpty()) diff --git a/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java b/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java index f84e23ea..bed80865 100644 --- a/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java +++ b/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java @@ -69,7 +69,8 @@ public class ResultWriter { "lambdasQty", "uniqueWordsQty", "modifiers", - "logStatementsQty"}; + "logStatementsQty", + "classOccurrences" }; private static final String[] METHOD_HEADER = { "file", "class", @@ -148,6 +149,8 @@ public ResultWriter(String classFile, String methodFile, String variableFile, St * @throws IOException If output files cannot be written to */ public void printResult(CKClassResult result) throws IOException { + String classNameKey = result.getClassName(); + Integer classOccurrencesCount = Runner.classOccurrences.getOrDefault(classNameKey, 0); // Fallback to 0 if the class name is not found this.classPrinter.printRecord( result.getFile(), @@ -209,7 +212,8 @@ public void printResult(CKClassResult result) throws IOException { result.getLambdasQty(), result.getUniqueWordsQty(), result.getModifiers(), - result.getNumberOfLogStatements()); + result.getNumberOfLogStatements(), + classOccurrencesCount); for (CKMethodResult method : result.getMethods()) { // Extract the clean method name diff --git a/src/main/java/com/github/mauricioaniche/ck/Runner.java b/src/main/java/com/github/mauricioaniche/ck/Runner.java index c7528ad1..20238e38 100644 --- a/src/main/java/com/github/mauricioaniche/ck/Runner.java +++ b/src/main/java/com/github/mauricioaniche/ck/Runner.java @@ -8,6 +8,7 @@ public class Runner { public static Map methodOccurrences = new HashMap<>(); + public static Map classOccurrences = new HashMap<>(); public static void main(String[] args) throws IOException { From 2904516b530bf9176b1a88bd674c092c218ff0de Mon Sep 17 00:00:00 2001 From: Breno Farias da Silva Date: Sun, 31 Mar 2024 10:29:31 -0300 Subject: [PATCH 8/8] FEAT: Adding a Makefile to help running standard Maven Commands --- Makefile | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..7fc4c314 --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +# Makefile for managing the build process of a Java project using Maven + +# Default rule +default: build + +# Rule for building the project +build: + mvn compile + +# Rule for cleaning and packaging the project without running tests +package: + clear + mvn clean package -DskipTests + +# Rule for running tests +test: + mvn test + +# Rule for cleaning the project +clean: + mvn clean + +# Rule for generating documentation +doc: + mvn javadoc:javadoc + +# Rule for installing the project's artifacts into the local repository +install: + mvn install + +.PHONY: default build package test clean doc install