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: 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 diff --git a/README.md b/README.md index 837d14ee..c232a100 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) 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 diff --git a/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java b/src/main/java/com/github/mauricioaniche/ck/CKVisitor.java index 0e53b66c..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()) @@ -776,6 +786,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..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", @@ -104,7 +105,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; @@ -147,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(), @@ -208,9 +212,16 @@ 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 + 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 +256,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 e0cab409..20238e38 100644 --- a/src/main/java/com/github/mauricioaniche/ck/Runner.java +++ b/src/main/java/com/github/mauricioaniche/ck/Runner.java @@ -7,7 +7,9 @@ import java.util.Map; public class Runner { - + public static Map methodOccurrences = new HashMap<>(); + public static Map classOccurrences = new HashMap<>(); + public static void main(String[] args) throws IOException { if (args == null || args.length < 1) { @@ -37,11 +39,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);