This module, named sourcecodeanalyzer
, is designed to analyze Java source code using various design patterns. It features a command-line interface for running the application and supports multiple output formats.
To build the executable Java application, use the following command:
mvn package jacoco:report
To run the executable, execute the following command:
java -jar "jar-with-dependencies" arg0 arg1 arg2 arg3 arg4
The arguments are as follows:
- arg0: JavaSourceCodeInputFile (e.g., src/test/resources/TestClass.java)
- arg1: sourceCodeAnalyzerType [regex|strcomp]
- arg2: SourceCodeLocationType [local|web]
- arg3: OutputFilePath (e.g., ../output_metrics_file)
- arg4: OutputFileType [csv|json]
java -jar ./target/sourcecodeanalyzer-0.0.1-SNAPSHOT-jar-with-dependencies.jar ./src/test/resources/TestClass.java regex local metrics_results csv
Download Class diagram.drawio
from this module and open it in diagrams.net
Following the Open/Closed principle of SOLID, the Strategy design pattern is applied. Instead of having concrete implementations of metric calculations (calculateLOC(), calculateNOM(), calculateNOC()) within the SourceCodeAnalyzer class, an interface (MetricCalculator) was added instead and has multiple implementations for each metric. This allows new metrics to be added by implementing the MetricCalculator interface without modifying the existing class.
The same is applied for the other 2 classes (MetricExporter.java, SourceFileReader.java).
Classes
SourceCodeAnalyzer.java
is connected with the interface MetricCalculator.java
.
This interface is currently implemented by calculateLOC.java
, calculateNOM.java
and calculateNOC.java
.
SourceFileReader.java
communicates with SourceCodeAnalyzer.java
and is also connected with the interface FileReader.java
.
This interface is currently implemented by ReadToList.java
and ReadToString.java
.
MetricsExporter.java
is connected with the interface MetricExporter.java
.
This interface is currently implemented by ExportCsv.java
and ExportJson.java
.
Benefits:
- More flexible
- More scaleable
- Easier to test
Drawbacks:
- Higher Complexity
The Facade pattern provides a unified interface that encapsulates complex subsystems or libraries, simplifying their usage for clients. It acts as a single entry point and shields clients from the underlying implementation details.
Here a new class CodeAnalyzerFacade is created, which will serve as the facade for the code analysis functionality. The CodeAnalyzerFacade class will have a simplified interface that accepts the required information from the DemoClient. It will internally handle the complexity of interacting with the existing classes in the codeanalyzer package.
Classes
DemoClient.java
communicates only with CodeAnalyzerFacade.java
.
CodeAnalyzerFacade.java
is connected with SourceCodeAnalyzer.java
and MetricsExporter.java
and handles all the underlying functionality.
Benefits:
- Simplifies usage
- Hides implementation details
- Enhances maintainability
- Improves Readability
Drawbacks:
- Limited Flexibility