• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.dependencymapper;
17 
18 import java.io.BufferedReader;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 
28 /**
29  * An utility class that reads each java file present in the rsp content then analyzes the same,
30  * collecting the analysis in {@link List<JavaSourceData>}
31  */
32 public class JavaSourceAnalyzer {
33 
34     // Regex that matches against "package abc.xyz.lmn;" declarations in a java file.
35     private static final String PACKAGE_REGEX = "^package\\s+([a-zA-Z_][a-zA-Z0-9_.]*);";
36 
analyze(Path srcRspFile)37     public static List<JavaSourceData> analyze(Path srcRspFile) {
38         List<JavaSourceData> javaSourceDataList = new ArrayList<>();
39         try (BufferedReader reader = new BufferedReader(new FileReader(srcRspFile.toFile()))) {
40             String line;
41             while ((line = reader.readLine()) != null) {
42                 // Split the line by spaces, tabs, multiple java files can be on a single line.
43                 String[] files = line.trim().split("\\s+");
44                 for (String file : files) {
45                     Path p = Paths.get("", file);
46                     System.out.println(p.toAbsolutePath().toString());
47                     javaSourceDataList
48                             .add(new JavaSourceData(file, constructPackagePrependedFileName(file)));
49                 }
50             }
51         } catch (IOException e) {
52             System.err.println("Error reading rsp file at: " + srcRspFile);
53             throw new RuntimeException(e);
54         }
55         return javaSourceDataList;
56     }
57 
constructPackagePrependedFileName(String filePath)58     private static String constructPackagePrependedFileName(String filePath) {
59         String packageAppendedFileName = null;
60         // if the file path is abc/def/ghi/JavaFile.java we extract JavaFile.java
61         String javaFileName = filePath.substring(filePath.lastIndexOf("/") + 1);
62         try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
63             String line;
64             // Process each line and match against the package regex pattern.
65             while ((line = reader.readLine()) != null) {
66                 Pattern pattern = Pattern.compile(PACKAGE_REGEX);
67                 Matcher matcher = pattern.matcher(line);
68                 if (matcher.find()) {
69                     packageAppendedFileName = matcher.group(1) + "." + javaFileName;
70                     break;
71                 }
72             }
73         } catch (IOException e) {
74             System.err.println("Error reading java file at: " + filePath);
75             throw new RuntimeException(e);
76         }
77         // Should not be null
78         assert packageAppendedFileName != null;
79         return packageAppendedFileName;
80     }
81 }
82