Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't change that to your repository. Mine should be the official one.

[![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)

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
<configuration>
<source>11</source>
<target>11</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/github/mauricioaniche/ck/CKVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/com/github/mauricioaniche/ck/ResultWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public class ResultWriter {
"lambdasQty",
"uniqueWordsQty",
"modifiers",
"logStatementsQty"};
"logStatementsQty",
"classOccurrences" };
private static final String[] METHOD_HEADER = {
"file",
"class",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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<String, Integer> entry : method.getVariablesUsage().entrySet()) {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/github/mauricioaniche/ck/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.Map;

public class Runner {

public static Map<String,Integer> methodOccurrences = new HashMap<>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Runner should ideally have no business logic at all. You should move this somewhere else.

public static Map<String,Integer> classOccurrences = new HashMap<>();

public static void main(String[] args) throws IOException {

if (args == null || args.length < 1) {
Expand Down Expand Up @@ -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);

Expand Down