Skip to content

Feature Request Issue 125: Analyze test classes optionally if the user wants to #126

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

Merged
merged 9 commits into from
Feb 19, 2025
Merged
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: 4 additions & 0 deletions .github/workflows/release_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
{
"title": "## \uD83D\uDEE0 Other Updates",
"labels": ["other", "kind/dependency-change"]
},
{
"title": "## 🚨 Breaking Changes",
"labels": ["breaking"]
}
],
"ignore_labels": [
Expand Down
13 changes: 0 additions & 13 deletions .settings/org.eclipse.buildship.core.prefs

This file was deleted.

66 changes: 43 additions & 23 deletions src/main/java/com/ibm/cldk/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class VersionProvider implements CommandLine.IVersionProvider {

public String[] getVersion() throws Exception {
String version = getClass().getPackage().getImplementationVersion();
return new String[]{version != null ? version : "unknown"};
return new String[] { version != null ? version : "unknown" };
}
}

Expand All @@ -56,34 +56,43 @@ public String[] getVersion() throws Exception {
@Command(name = "codeanalyzer", mixinStandardHelpOptions = true, sortOptions = false, versionProvider = VersionProvider.class, description = "Analyze java application.")
public class CodeAnalyzer implements Runnable {

@Option(names = {"-i", "--input"}, description = "Path to the project root directory.")
@Option(names = { "-i", "--input" }, description = "Path to the project root directory.")
private static String input;

@Option(names = {"-t", "--target-files"}, description = "Paths to files to be analyzed from the input application.")
@Option(names = { "-t",
"--target-files" }, description = "Paths to files to be analyzed from the input application.")
private static List<String> targetFiles;

@Option(names = {"-s", "--source-analysis"}, description = "Analyze a single string of java source code instead the project.")
@Option(names = { "-s",
"--source-analysis" }, description = "Analyze a single string of java source code instead the project.")
private static String sourceAnalysis;

@Option(names = {"-o", "--output"}, description = "Destination directory to save the output graphs. By default, the SDG formatted as a JSON will be printed to the console.")
@Option(names = { "-o",
"--output" }, description = "Destination directory to save the output graphs. By default, the SDG formatted as a JSON will be printed to the console.")
private static String output;

@Option(names = {"-b", "--build-cmd"}, description = "Custom build command. Defaults to auto build.")
@Option(names = { "-b", "--build-cmd" }, description = "Custom build command. Defaults to auto build.")
private static String build;

@Option(names = {"--no-build"}, description = "Do not build your application. Use this option if you have already built your application.")
@Option(names = {
"--no-build" }, description = "Do not build your application. Use this option if you have already built your application.")
private static boolean noBuild = false;

@Option(names = {"--no-clean-dependencies"}, description = "Do not attempt to auto-clean dependencies")
@Option(names = { "--no-clean-dependencies" }, description = "Do not attempt to auto-clean dependencies")
public static boolean noCleanDependencies = false;

@Option(names = {"-f", "--project-root-path"}, description = "Path to the root pom.xml/build.gradle file of the project.")
@Option(names = { "-f",
"--project-root-path" }, description = "Path to the root pom.xml/build.gradle file of the project.")
public static String projectRootPom;

@Option(names = {"-a", "--analysis-level"}, description = "Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for call graph). Default: 1")
@Option(names = { "-a",
"--analysis-level" }, description = "Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for call graph). Default: 1")
private static int analysisLevel = 1;

@Option(names = {"-v", "--verbose"}, description = "Print logs to console.")
@Option(names = { "--include-test-classes" }, hidden = true, description = "Print logs to console.")
public static boolean includeTestClasses = false;

@Option(names = { "-v", "--verbose" }, description = "Print logs to console.")
private static boolean verbose = false;

private static final String outputFileName = "analysis.json";
Expand Down Expand Up @@ -121,11 +130,13 @@ private static void analyze() throws Exception {
JsonObject combinedJsonObject = new JsonObject();
Map<String, JavaCompilationUnit> symbolTable;
projectRootPom = projectRootPom == null ? input : projectRootPom;
// First of all if, sourceAnalysis is provided, we will analyze the source code instead of the project.
// First of all if, sourceAnalysis is provided, we will analyze the source code
// instead of the project.
if (sourceAnalysis != null) {
// Construct symbol table for source code
Log.debug("Single file analysis.");
Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>> symbolTableExtractionResult = SymbolTable.extractSingle(sourceAnalysis);
Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>> symbolTableExtractionResult = SymbolTable
.extractSingle(sourceAnalysis);
symbolTable = symbolTableExtractionResult.getLeft();
} else {
// download library dependencies of project for type resolution
Expand All @@ -140,9 +151,11 @@ private static void analyze() throws Exception {
Log.warn("Failed to download library dependencies of project");
}

boolean analysisFileExists = output != null && Files.exists(Paths.get(output + File.separator + outputFileName));
boolean analysisFileExists = output != null
&& Files.exists(Paths.get(output + File.separator + outputFileName));

// if target files are specified, compute symbol table information for the given files
// if target files are specified, compute symbol table information for the given
// files
if (targetFiles != null) {
Log.info(targetFiles.size() + "target files specified for analysis: " + targetFiles);

Expand All @@ -154,17 +167,22 @@ private static void analyze() throws Exception {
}

// Previous code was pointing to toList, which has been introduced in Java 16
// symbolTable = SymbolTable.extract(Paths.get(input), targetFiles.stream().map(Paths::get).toList()).getLeft();
// symbolTable = SymbolTable.extract(Paths.get(input),
// targetFiles.stream().map(Paths::get).toList()).getLeft();
// extract symbol table for the specified files
symbolTable = SymbolTable.extract(Paths.get(input), targetFiles.stream().map(Paths::get).collect(Collectors.toList())).getLeft();
symbolTable = SymbolTable
.extract(Paths.get(input), targetFiles.stream().map(Paths::get).collect(Collectors.toList()))
.getLeft();

// if analysis file exists, update it with new symbol table information for the specified fiels
// if analysis file exists, update it with new symbol table information for the
// specified fiels
if (analysisFileExists) {
// read symbol table information from existing analysis file
Map<String, JavaCompilationUnit> existingSymbolTable = readSymbolTableFromFile(
new File(output, outputFileName));
if (existingSymbolTable != null) {
// for each file, tag its symbol table information as "updated" and update existing symbol table
// for each file, tag its symbol table information as "updated" and update
// existing symbol table
for (String targetFile : targetFiles) {
String targetPathAbs = Paths.get(targetFile).toAbsolutePath().toString();
JavaCompilationUnit javaCompilationUnit = symbolTable.get(targetPathAbs);
Expand All @@ -175,16 +193,18 @@ private static void analyze() throws Exception {
symbolTable = existingSymbolTable;
}
} else {
// construct symbol table for project, write parse problems to file in output directory if specified
Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>> symbolTableExtractionResult
= SymbolTable.extractAll(Paths.get(input));
// construct symbol table for project, write parse problems to file in output
// directory if specified
Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>> symbolTableExtractionResult = SymbolTable
.extractAll(Paths.get(input));

symbolTable = symbolTableExtractionResult.getLeft();
}

if (analysisLevel > 1) {
// Save SDG, and Call graph as JSON
// If noBuild is not true, and build is also not provided, we will use "auto" as the build command
// If noBuild is not true, and build is also not provided, we will use "auto" as
// the build command
build = build == null ? "auto" : build;
// Is noBuild is true, we will not build the project
build = noBuild ? null : build;
Expand Down
Loading