1 /*
2  * Copyright (C) 2023 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 
17 package libcore.tools.analyzer.openjdk;
18 
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.UncheckedIOException;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 
29 /**
30  * An instance of {@link UnsupportedNewApis } contains a set of class / method / field signatures
31  * read from {@link #FILE_NAME} in the jar resources.
32  */
33 public class UnsupportedNewApis {
34 
35     public static final String FILE_NAME = "unsupported_new_apis.txt";
36 
37     private static UnsupportedNewApis sInstance;
38 
39     private final Set<String> apis;
40 
UnsupportedNewApis()41     private UnsupportedNewApis() {
42          apis = parse();
43     }
44 
getInstance()45     public static synchronized UnsupportedNewApis getInstance() {
46         if (sInstance == null) {
47             sInstance = new UnsupportedNewApis();
48         }
49         return sInstance;
50     }
51 
contains(String internalClassName)52     public boolean contains(String internalClassName) {
53         return apis.contains(internalClassName);
54     }
55 
contains(SignaturesCollector.Method method)56     public boolean contains(SignaturesCollector.Method method) {
57         return apis.contains(method.toString());
58     }
59 
contains(SignaturesCollector.Field field)60     public boolean contains(SignaturesCollector.Field field) {
61         String signature = field.getOwner() + "#" + field.getName() + ":" + field.getDesc();
62         return apis.contains(signature);
63     }
64 
parse()65     private static Set<String> parse() {
66         try (InputStream in = UnsupportedNewApis.class.getResourceAsStream(FILE_NAME)) {
67             List<String> result = new ArrayList<>();
68             try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
69                 String line;
70                 while ((line = reader.readLine()) != null) {
71                     line = line.strip();
72                     if (line.isEmpty() || line.startsWith("#")) {
73                         continue;
74                     }
75 
76                     result.add(line);
77                 }
78             }
79             return new HashSet<>(result);
80         } catch (IOException e) {
81             throw new UncheckedIOException(e);
82         }
83     }
84 }
85