99WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010See the License for the specific language governing permissions and
1111limitations under the License.
12- */
13-
12+ */
1413package com .ibm .cldk ;
1514
16- import com .github .javaparser .Problem ;
17- import com .google .common .reflect .TypeToken ;
18- import com .google .gson .*;
19- import com .ibm .cldk .entities .JavaCompilationUnit ;
20- import com .ibm .cldk .utils .BuildProject ;
21- import com .ibm .cldk .utils .Log ;
22- import com .ibm .wala .ipa .callgraph .CallGraphBuilderCancelException ;
23- import com .ibm .wala .ipa .cha .ClassHierarchyException ;
24- import org .apache .commons .lang3 .tuple .Pair ;
25- import picocli .CommandLine ;
26- import picocli .CommandLine .Command ;
27- import picocli .CommandLine .Option ;
28-
2915import java .io .File ;
3016import java .io .FileReader ;
3117import java .io .FileWriter ;
3824import java .util .Map ;
3925import java .util .stream .Collectors ;
4026
27+ import org .apache .commons .lang3 .tuple .Pair ;
28+
29+ import com .github .javaparser .Problem ;
30+ import com .google .common .reflect .TypeToken ;
31+ import com .google .gson .FieldNamingPolicy ;
32+ import com .google .gson .Gson ;
33+ import com .google .gson .GsonBuilder ;
34+ import com .google .gson .JsonElement ;
35+ import com .google .gson .JsonObject ;
36+ import com .google .gson .JsonParser ;
37+ import com .ibm .cldk .entities .JavaCompilationUnit ;
38+ import com .ibm .cldk .utils .BuildProject ;
39+ import com .ibm .cldk .utils .Log ;
40+
41+ import picocli .CommandLine ;
42+ import picocli .CommandLine .Command ;
43+ import picocli .CommandLine .Option ;
4144
4245class VersionProvider implements CommandLine .IVersionProvider {
46+
4347 public String [] getVersion () throws Exception {
4448 String version = getClass ().getPackage ().getImplementationVersion ();
45- return new String []{ version != null ? version : "unknown" };
49+ return new String []{version != null ? version : "unknown" };
4650 }
4751}
52+
4853/**
4954 * The type Code analyzer.
5055 */
@@ -69,6 +74,8 @@ public class CodeAnalyzer implements Runnable {
6974 @ Option (names = {"--no-build" }, description = "Do not build your application. Use this option if you have already built your application." )
7075 private static boolean noBuild = false ;
7176
77+ @ Option (names = {"-f" , "--project-root-path" }, description = "Path to the root pom.xml/build.gradle file of the project." )
78+ public static String projectRootPom ;
7279
7380 @ Option (names = {"-a" , "--analysis-level" }, description = "Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for call graph). Default: 1" )
7481 private static int analysisLevel = 1 ;
@@ -83,6 +90,7 @@ public class CodeAnalyzer implements Runnable {
8390 .setPrettyPrinting ()
8491 .disableHtmlEscaping ()
8592 .create ();
93+
8694 /**
8795 * The entry point of application.
8896 *
@@ -108,22 +116,26 @@ private static void analyze() throws Exception {
108116
109117 JsonObject combinedJsonObject = new JsonObject ();
110118 Map <String , JavaCompilationUnit > symbolTable ;
119+ projectRootPom = projectRootPom == null ? input : projectRootPom ;
111120 // First of all if, sourceAnalysis is provided, we will analyze the source code instead of the project.
112121 if (sourceAnalysis != null ) {
113122 // Construct symbol table for source code
114123 Log .debug ("Single file analysis." );
115124 Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> symbolTableExtractionResult = SymbolTable .extractSingle (sourceAnalysis );
116125 symbolTable = symbolTableExtractionResult .getLeft ();
117- }
118-
119- else {
126+ } else {
120127 // download library dependencies of project for type resolution
121128 String dependencies = null ;
122- if (BuildProject .downloadLibraryDependencies (input )) {
123- dependencies = String .valueOf (BuildProject .libDownloadPath );
124- } else {
129+ try {
130+ if (BuildProject .downloadLibraryDependencies (input , projectRootPom )) {
131+ dependencies = String .valueOf (BuildProject .libDownloadPath );
132+ } else {
133+ Log .warn ("Failed to download library dependencies of project" );
134+ }
135+ } catch (IllegalStateException illegalStateException ) {
125136 Log .warn ("Failed to download library dependencies of project" );
126137 }
138+
127139 boolean analysisFileExists = output != null && Files .exists (Paths .get (output + File .separator + outputFileName ));
128140
129141 // if target files are specified, compute symbol table information for the given files
@@ -132,8 +144,8 @@ private static void analyze() throws Exception {
132144
133145 // if target files specified for analysis level 2, downgrade to analysis level 1
134146 if (analysisLevel > 1 ) {
135- Log .warn ("Incremental analysis is supported at analysis level 1 only; " +
136- "performing analysis level 1 for target files" );
147+ Log .warn ("Incremental analysis is supported at analysis level 1 only; "
148+ + "performing analysis level 1 for target files" );
137149 analysisLevel = 1 ;
138150 }
139151
@@ -158,12 +170,10 @@ private static void analyze() throws Exception {
158170 }
159171 symbolTable = existingSymbolTable ;
160172 }
161- }
162-
163- else {
173+ } else {
164174 // construct symbol table for project, write parse problems to file in output directory if specified
165- Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> symbolTableExtractionResult =
166- SymbolTable .extractAll (Paths .get (input ));
175+ Pair <Map <String , JavaCompilationUnit >, Map <String , List <Problem >>> symbolTableExtractionResult
176+ = SymbolTable .extractAll (Paths .get (input ));
167177
168178 symbolTable = symbolTableExtractionResult .getLeft ();
169179 }
@@ -221,7 +231,8 @@ private static void emit(String consolidatedJSONString) throws IOException {
221231 }
222232
223233 private static Map <String , JavaCompilationUnit > readSymbolTableFromFile (File analysisJsonFile ) {
224- Type symbolTableType = new TypeToken <Map <String , JavaCompilationUnit >>() {}.getType ();
234+ Type symbolTableType = new TypeToken <Map <String , JavaCompilationUnit >>() {
235+ }.getType ();
225236 try (FileReader reader = new FileReader (analysisJsonFile )) {
226237 JsonObject jsonObject = JsonParser .parseReader (reader ).getAsJsonObject ();
227238 return gson .fromJson (jsonObject .get ("symbol_table" ), symbolTableType );
@@ -230,4 +241,4 @@ private static Map<String, JavaCompilationUnit> readSymbolTableFromFile(File ana
230241 }
231242 return null ;
232243 }
233- }
244+ }
0 commit comments