@@ -82,158 +82,167 @@ public class CodeAnalyzer implements Runnable {
82
82
private static final String outputFileName = "analysis.json" ;
83
83
84
84
public static Gson gson = new GsonBuilder ()
85
- .setFieldNamingPolicy (FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES )
86
- .setPrettyPrinting ()
87
- .disableHtmlEscaping ()
88
- .create ();
89
- /**
90
- * The entry point of application.
91
- *
92
- * @param args the input arguments
93
- */
94
- public static void main (String [] args ) {
95
- int exitCode = new CommandLine (new CodeAnalyzer ()).execute (args );
96
- System .exit (exitCode );
97
- }
85
+ .setFieldNamingPolicy (FieldNamingPolicy .LOWER_CASE_WITH_UNDERSCORES )
86
+ .setPrettyPrinting ()
87
+ .disableHtmlEscaping ()
88
+ .create ();
89
+ /**
90
+ * The entry point of application.
91
+ *
92
+ * @param args the input arguments
93
+ */
94
+ public static void main (String [] args ) {
95
+ int exitCode = new CommandLine (new CodeAnalyzer ()).execute (args );
96
+ System .exit (exitCode );
97
+ }
98
98
99
- @ Override
100
- public void run () {
101
- // Set log level based on quiet option
102
- Log .setVerbosity (verbose );
103
- try {
104
- analyze ();
105
- } catch (Exception e ) {
106
- throw new RuntimeException (e );
99
+ @ Override
100
+ public void run () {
101
+ // Set log level based on quiet option
102
+ Log .setVerbosity (verbose );
103
+ try {
104
+ analyze ();
105
+ } catch (Exception e ) {
106
+ throw new RuntimeException (e );
107
+ }
107
108
}
108
- }
109
109
110
- private static void analyze () throws Exception {
111
-
112
- JsonObject combinedJsonObject = new JsonObject ();
113
- Map <String , JavaCompilationUnit > symbolTable ;
114
- projectRootPom = projectRootPom == null ? input : projectRootPom ;
115
- // First of all if, sourceAnalysis is provided, we will analyze the source code instead of the project.
116
- if (sourceAnalysis != null ) {
117
- // Construct symbol table for source code
118
- Log .debug ("Single file analysis." );
119
- Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> symbolTableExtractionResult = SymbolTable .extractSingle (sourceAnalysis );
120
- symbolTable = symbolTableExtractionResult .getLeft ();
121
- } else {
122
- // download library dependencies of project for type resolution
123
- String dependencies = null ;
124
- try {if (BuildProject .downloadLibraryDependencies (input , projectRootPom )) {
125
- dependencies = String .valueOf (BuildProject .libDownloadPath );
110
+ private static void analyze () throws Exception {
111
+
112
+ JsonObject combinedJsonObject = new JsonObject ();
113
+ Map <String , JavaCompilationUnit > symbolTable ;
114
+ projectRootPom = projectRootPom == null ? input : projectRootPom ;
115
+ // First of all if, sourceAnalysis is provided, we will analyze the source code instead of the project.
116
+ if (sourceAnalysis != null ) {
117
+ // Construct symbol table for source code
118
+ Log .debug ("Single file analysis." );
119
+ Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> symbolTableExtractionResult = SymbolTable .extractSingle (sourceAnalysis );
120
+ symbolTable = symbolTableExtractionResult .getLeft ();
126
121
} else {
127
- Log .warn ("Failed to download library dependencies of project" );
128
- }
129
- } catch (IllegalStateException illegalStateException ) {
130
- Log .warn ("Failed to download library dependencies of project" );
131
- }
122
+ // download library dependencies of project for type resolution
123
+ String dependencies = null ;
124
+ try {if (BuildProject .downloadLibraryDependencies (input , projectRootPom )) {
125
+ dependencies = String .valueOf (BuildProject .libDownloadPath );
126
+ } else {
127
+ Log .warn ("Failed to download library dependencies of project" );
128
+ }
129
+ } catch (IllegalStateException illegalStateException ) {
130
+ Log .warn ("Failed to download library dependencies of project" );
131
+ }
132
132
133
- boolean analysisFileExists = output != null && Files .exists (Paths .get (output + File .separator + outputFileName ));
133
+ boolean analysisFileExists = output != null && Files .exists (Paths .get (output + File .separator + outputFileName ));
134
134
135
- // if target files are specified, compute symbol table information for the given files
136
- if (targetFiles != null ) {
137
- Log .info (targetFiles .size () + "target files specified for analysis: " + targetFiles );
135
+ // if target files are specified, compute symbol table information for the given files
136
+ if (targetFiles != null ) {
137
+ Log .info (targetFiles .size () + "target files specified for analysis: " + targetFiles );
138
138
139
- // if target files specified for analysis level 2, downgrade to analysis level 1
140
- if (analysisLevel > 1 ) {
141
- Log .warn ("Incremental analysis is supported at analysis level 1 only; " +
142
- "performing analysis level 1 for target files" );
143
- analysisLevel = 1 ;
144
- }
139
+ // if target files specified for analysis level 2, downgrade to analysis level 1
140
+ if (analysisLevel > 1 ) {
141
+ Log .warn ("Incremental analysis is supported at analysis level 1 only; " +
142
+ "performing analysis level 1 for target files" );
143
+ analysisLevel = 1 ;
144
+ }
145
145
146
- // Previous code was pointing to toList, which has been introduced in Java 16
147
- // symbolTable = SymbolTable.extract(Paths.get(input), targetFiles.stream().map(Paths::get).toList()).getLeft();
148
- // extract symbol table for the specified files
149
- symbolTable = SymbolTable .extract (Paths .get (input ), targetFiles .stream ().map (Paths ::get ).collect (Collectors .toList ())).getLeft ();
150
-
151
- // if analysis file exists, update it with new symbol table information for the specified fiels
152
- if (analysisFileExists ) {
153
- // read symbol table information from existing analysis file
154
- Map <String , JavaCompilationUnit > existingSymbolTable = readSymbolTableFromFile (
155
- new File (output , outputFileName ));
156
- if (existingSymbolTable != null ) {
157
- // for each file, tag its symbol table information as "updated" and update existing symbol table
158
- for (String targetFile : targetFiles ) {
159
- String targetPathAbs = Paths .get (targetFile ).toAbsolutePath ().toString ();
160
- JavaCompilationUnit javaCompilationUnit = symbolTable .get (targetPathAbs );
161
- javaCompilationUnit .setModified (true );
162
- existingSymbolTable .put (targetPathAbs , javaCompilationUnit );
146
+ // Previous code was pointing to toList, which has been introduced in Java 16
147
+ // symbolTable = SymbolTable.extract(Paths.get(input), targetFiles.stream().map(Paths::get).toList()).getLeft();
148
+ // extract symbol table for the specified files
149
+ symbolTable = SymbolTable .extract (Paths .get (input ), targetFiles .stream ().map (Paths ::get ).collect (Collectors .toList ())).getLeft ();
150
+
151
+ // if analysis file exists, update it with new symbol table information for the specified fiels
152
+ if (analysisFileExists ) {
153
+ // read symbol table information from existing analysis file
154
+ Map <String , JavaCompilationUnit > existingSymbolTable = readSymbolTableFromFile (
155
+ new File (output , outputFileName ));
156
+ if (existingSymbolTable != null ) {
157
+ // for each file, tag its symbol table information as "updated" and update existing symbol table
158
+ for (String targetFile : targetFiles ) {
159
+ String targetPathAbs = Paths .get (targetFile ).toAbsolutePath ().toString ();
160
+ JavaCompilationUnit javaCompilationUnit = symbolTable .get (targetPathAbs );
161
+ javaCompilationUnit .setModified (true );
162
+ existingSymbolTable .put (targetPathAbs , javaCompilationUnit );
163
+ }
163
164
}
165
+ symbolTable = existingSymbolTable ;
164
166
}
165
- symbolTable = existingSymbolTable ;
166
167
}
167
- }
168
168
169
- else {
170
- // construct symbol table for project, write parse problems to file in output directory if specified
171
- Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> symbolTableExtractionResult =
172
- SymbolTable .extractAll (Paths .get (input ));
169
+ else {
170
+ // construct symbol table for project, write parse problems to file in output directory if specified
171
+ Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> symbolTableExtractionResult =
172
+ SymbolTable .extractAll (Paths .get (input ));
173
173
174
- symbolTable = symbolTableExtractionResult .getLeft ();
175
- }
174
+ symbolTable = symbolTableExtractionResult .getLeft ();
175
+ }
176
176
177
- if (analysisLevel > 1 ) {
178
- // Save SDG, and Call graph as JSON
179
- // If noBuild is not true, and build is also not provided, we will use "auto" as the build command
180
- build = build == null ? "auto" : build ;
181
- // Is noBuild is true, we will not build the project
182
- build = noBuild ? null : build ;
183
- List <Dependency > sdgEdges = SystemDependencyGraph .construct (input , dependencies , build );
184
- combinedJsonObject .add ("system_dependency_graph" , gson .toJsonTree (sdgEdges ));
177
+ if (analysisLevel > 1 ) {
178
+ // Save SDG, and Call graph as JSON
179
+ // If noBuild is not true, and build is also not provided, we will use "auto" as the build command
180
+ build = build == null ? "auto" : build ;
181
+ // Is noBuild is true, we will not build the project
182
+ build = noBuild ? null : build ;
183
+ String sdgAsJSONString = SystemDependencyGraph .construct (input , dependencies , build );
184
+ JsonElement sdgAsJSONElement = gson .fromJson (sdgAsJSONString , JsonElement .class );
185
+ JsonObject sdgAsJSONObject = sdgAsJSONElement .getAsJsonObject ();
186
+ JsonElement edges = sdgAsJSONObject .get ("edges" );
187
+
188
+ // We don't really need these fields, so we'll remove it.
189
+ sdgAsJSONObject .remove ("nodes" );
190
+ sdgAsJSONObject .remove ("creator" );
191
+ sdgAsJSONObject .remove ("version" );
192
+ // Remove the 'edges' element and move the list of edges up one level
193
+ combinedJsonObject .add ("system_dependency_graph" , edges );
194
+ }
185
195
}
196
+ // Cleanup library dependencies directory
197
+ BuildProject .cleanLibraryDependencies ();
198
+
199
+ // Convert the JavaCompilationUnit to JSON and add to consolidated json object
200
+ String symbolTableJSONString = gson .toJson (symbolTable );
201
+ JsonElement symbolTableJSON = gson .fromJson (symbolTableJSONString , JsonElement .class );
202
+ combinedJsonObject .add ("symbol_table" , symbolTableJSON );
203
+
204
+ // Add version number to the output JSON
205
+ try {
206
+ String [] versions = new VersionProvider ().getVersion ();
207
+ if (versions .length > 0 ) {
208
+ combinedJsonObject .addProperty ("version" , versions [0 ]);
209
+ } else {
210
+ combinedJsonObject .addProperty ("version" , "unknown" );
211
+ }
212
+ } catch (Exception e ) {
213
+ combinedJsonObject .addProperty ("version" , "error retrieving version" );
214
+ }
215
+ String consolidatedJSONString = gson .toJson (combinedJsonObject );
216
+ emit (consolidatedJSONString );
186
217
}
187
- // Cleanup library dependencies directory
188
- BuildProject .cleanLibraryDependencies ();
189
-
190
- // Convert the JavaCompilationUnit to JSON and add to consolidated json object
191
- String symbolTableJSONString = gson .toJson (symbolTable );
192
- JsonElement symbolTableJSON = gson .fromJson (symbolTableJSONString , JsonElement .class );
193
- combinedJsonObject .add ("symbol_table" , symbolTableJSON );
194
-
195
- // Add version number to the output JSON
196
- try {
197
- String [] versions = new VersionProvider ().getVersion ();
198
- if (versions .length > 0 ) {
199
- combinedJsonObject .addProperty ("version" , versions [0 ]);
218
+
219
+ private static void emit (String consolidatedJSONString ) throws IOException {
220
+ if (output == null ) {
221
+ System .out .println (consolidatedJSONString );
200
222
} else {
201
- combinedJsonObject .addProperty ("version" , "unknown" );
223
+ Path outputPath = Paths .get (output );
224
+ if (!Files .exists (outputPath )) {
225
+ Files .createDirectories (outputPath );
226
+ }
227
+ // If output is not null, export to a file
228
+ File file = new File (output , "analysis.json" );
229
+ try (FileWriter fileWriter = new FileWriter (file )) {
230
+ fileWriter .write (consolidatedJSONString );
231
+ Log .done ("Analysis output saved at " + output );
232
+ } catch (IOException e ) {
233
+ Log .error ("Error writing to file: " + e .getMessage ());
234
+ }
202
235
}
203
- } catch (Exception e ) {
204
- combinedJsonObject .addProperty ("version" , "error retrieving version" );
205
236
}
206
- String consolidatedJSONString = gson .toJson (combinedJsonObject );
207
- emit (consolidatedJSONString );
208
- }
209
237
210
- private static void emit (String consolidatedJSONString ) throws IOException {
211
- if (output == null ) {
212
- System .out .println (consolidatedJSONString );
213
- } else {
214
- Path outputPath = Paths .get (output );
215
- if (!Files .exists (outputPath )) {
216
- Files .createDirectories (outputPath );
217
- }
218
- // If output is not null, export to a file
219
- File file = new File (output , "analysis.json" );
220
- try (FileWriter fileWriter = new FileWriter (file )) {
221
- fileWriter .write (consolidatedJSONString );
222
- Log .done ("Analysis output saved at " + output );
238
+ private static Map <String , JavaCompilationUnit > readSymbolTableFromFile (File analysisJsonFile ) {
239
+ Type symbolTableType = new TypeToken <Map <String , JavaCompilationUnit >>() {}.getType ();
240
+ try (FileReader reader = new FileReader (analysisJsonFile )) {
241
+ JsonObject jsonObject = JsonParser .parseReader (reader ).getAsJsonObject ();
242
+ return gson .fromJson (jsonObject .get ("symbol_table" ), symbolTableType );
223
243
} catch (IOException e ) {
224
- Log .error ("Error writing to file: " + e .getMessage ());
244
+ Log .error ("Error reading analysis file: " + e .getMessage ());
225
245
}
246
+ return null ;
226
247
}
227
- }
228
-
229
- private static Map <String , JavaCompilationUnit > readSymbolTableFromFile (File analysisJsonFile ) {
230
- Type symbolTableType = new TypeToken <Map <String , JavaCompilationUnit >>() {}.getType ();
231
- try (FileReader reader = new FileReader (analysisJsonFile )) {
232
- JsonObject jsonObject = JsonParser .parseReader (reader ).getAsJsonObject ();
233
- return gson .fromJson (jsonObject .get ("symbol_table" ), symbolTableType );
234
- } catch (IOException e ) {
235
- Log .error ("Error reading analysis file: " + e .getMessage ());
236
- }
237
- return null ;
238
- }
239
248
}
0 commit comments