1 package com.android.class2nonsdklist;
2 
3 import com.android.annotationvisitor.AnnotationConsumer;
4 
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.PrintStream;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.SortedMap;
15 import java.util.TreeMap;
16 
17 public class HiddenapiFlagsWriter implements AnnotationConsumer {
18 
19     private final PrintStream mOutput;
20 
21     // A sorted map from signature to a comma separated sorted list of flags
22     private final SortedMap<String, String> mContents;
23 
HiddenapiFlagsWriter(String csvFile)24     public HiddenapiFlagsWriter(String csvFile) throws FileNotFoundException {
25         mOutput = new PrintStream(new FileOutputStream(new File(csvFile)));
26         mContents = new TreeMap<>();
27     }
28 
consume(String apiSignature, Map<String, String> annotationProperties, Set<String> parsedFlags)29     public void consume(String apiSignature, Map<String, String> annotationProperties,
30             Set<String> parsedFlags) {
31         if (parsedFlags.size() > 0) {
32             mContents.put(apiSignature, String.join(",", asSortedList(parsedFlags)));
33         }
34     }
35 
close()36     public void close() {
37         for (Map.Entry<String, String> row : mContents.entrySet()) {
38             mOutput.println(row.getKey() + "," + row.getValue());
39         }
40 
41         mOutput.close();
42     }
43 
asSortedList(Set<String> s)44     private static List<String> asSortedList(Set<String> s) {
45         List<String> list = new ArrayList<>(s);
46         Collections.sort(list);
47         return list;
48     }
49 }
50