-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCodeAnalyzer.java
More file actions
239 lines (205 loc) · 10.9 KB
/
CodeAnalyzer.java
File metadata and controls
239 lines (205 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright IBM Corporation 2023, 2024
Licensed under the Apache Public License 2.0, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ibm.cldk;
import com.github.javaparser.Problem;
import com.google.common.reflect.TypeToken;
import com.google.gson.*;
import com.ibm.cldk.entities.JavaCompilationUnit;
import com.ibm.cldk.utils.BuildProject;
import com.ibm.cldk.utils.Log;
import org.apache.commons.lang3.tuple.Pair;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class VersionProvider implements CommandLine.IVersionProvider {
public String[] getVersion() {
String version = getClass().getPackage().getImplementationVersion();
return new String[]{ version != null ? version : "unknown" };
}
}
/**
* The type Code analyzer.
*/
@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.")
private static String input;
@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.")
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.")
private static String output;
@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.")
private static boolean noBuild = false;
@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")
private static int analysisLevel = 1;
@Option(names = {"-v", "--verbose"}, description = "Print logs to console.")
private static boolean verbose = false;
@Option(names = {"--no-clean-dependencies"}, description = "Do not attempt to auto-clean dependencies")
public static boolean noCleanDependencies = false;
private static final String outputFileName = "analysis.json";
public static Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.disableHtmlEscaping()
.create();
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
int exitCode = new CommandLine(new CodeAnalyzer()).execute(args);
System.exit(exitCode);
}
@Override
public void run() {
// Set log level based on quiet option
Log.setVerbosity(verbose);
try {
analyze();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
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.
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);
symbolTable = symbolTableExtractionResult.getLeft();
} else {
// download library dependencies of project for type resolution
String dependencies = null;
try {if (BuildProject.downloadLibraryDependencies(input, projectRootPom)) {
dependencies = String.valueOf(BuildProject.libDownloadPath);
} else {
Log.warn("Failed to download library dependencies of project");
}
} catch (IllegalStateException illegalStateException) {
Log.warn("Failed to download library dependencies of project");
}
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 (targetFiles != null) {
Log.info(targetFiles.size() + "target files specified for analysis: " + targetFiles);
// if target files specified for analysis level 2, downgrade to analysis level 1
if (analysisLevel > 1) {
Log.warn("Incremental analysis is supported at analysis level 1 only; " +
"performing analysis level 1 for target files");
analysisLevel = 1;
}
// 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();
// extract symbol table for the specified files
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 (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 (String targetFile : targetFiles) {
String targetPathAbs = Paths.get(targetFile).toAbsolutePath().toString();
JavaCompilationUnit javaCompilationUnit = symbolTable.get(targetPathAbs);
javaCompilationUnit.setModified(true);
existingSymbolTable.put(targetPathAbs, javaCompilationUnit);
}
}
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));
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
build = build == null ? "auto" : build;
// Is noBuild is true, we will not build the project
build = noBuild ? null : build;
List<Dependency> sdgEdges = SystemDependencyGraph.construct(input, dependencies, build);
combinedJsonObject.add("system_dependency_graph", gson.toJsonTree(sdgEdges));
}
}
// Cleanup library dependencies directory
BuildProject.cleanLibraryDependencies();
// Convert the JavaCompilationUnit to JSON and add to consolidated json object
String symbolTableJSONString = gson.toJson(symbolTable);
JsonElement symbolTableJSON = gson.fromJson(symbolTableJSONString, JsonElement.class);
combinedJsonObject.add("symbol_table", symbolTableJSON);
// Add version number to the output JSON
try {
String[] versions = new VersionProvider().getVersion();
if (versions.length > 0) {
combinedJsonObject.addProperty("version", versions[0]);
} else {
combinedJsonObject.addProperty("version", "unknown");
}
} catch (Exception e) {
combinedJsonObject.addProperty("version", "error retrieving version");
}
String consolidatedJSONString = gson.toJson(combinedJsonObject);
emit(consolidatedJSONString);
}
private static void emit(String consolidatedJSONString) throws IOException {
if (output == null) {
System.out.println(consolidatedJSONString);
} else {
Path outputPath = Paths.get(output);
if (!Files.exists(outputPath)) {
Files.createDirectories(outputPath);
}
// If output is not null, export to a file
File file = new File(output, "analysis.json");
try (FileWriter fileWriter = new FileWriter(file)) {
fileWriter.write(consolidatedJSONString);
Log.done("Analysis output saved at " + output);
} catch (IOException e) {
Log.error("Error writing to file: " + e.getMessage());
}
}
}
private static Map<String, JavaCompilationUnit> readSymbolTableFromFile(File analysisJsonFile) {
Type symbolTableType = new TypeToken<Map<String, JavaCompilationUnit>>() {}.getType();
try (FileReader reader = new FileReader(analysisJsonFile)) {
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
return gson.fromJson(jsonObject.get("symbol_table"), symbolTableType);
} catch (IOException e) {
Log.error("Error reading analysis file: " + e.getMessage());
}
return null;
}
}