1/*
2 * Copyright (C) 2015 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 */
16package com.android.icu4j.srcgen.checker;
17
18import com.google.common.collect.Sets;
19import com.google.currysrc.Main;
20import com.google.currysrc.api.input.InputFileGenerator;
21
22import com.android.icu4j.srcgen.Icu4jTransformRules;
23
24import java.io.BufferedWriter;
25import java.io.File;
26import java.io.FileWriter;
27import java.util.List;
28import java.util.Set;
29
30/**
31 * Checks that the source in android_icu4j doesn't have obvious problems.
32 */
33public class CheckAndroidIcu4JSource {
34
35  private static final boolean DEBUG = false;
36
37  private CheckAndroidIcu4JSource() {
38  }
39
40  /**
41   * Usage:
42   * java com.android.icu4j.srcgen.CheckAndroidIcu4JSource {android_icu4j src directories}
43   *   {report output file path}
44   */
45  public static void main(String[] args) throws Exception {
46    if (args.length < 2) {
47      throw new IllegalArgumentException("At least 2 argument required.");
48    }
49
50    Main main = new Main(DEBUG);
51
52    // We assume we only need to look at ICU4J code for this for both passes.
53    String[] inputDirs = new String[args.length - 1];
54    System.arraycopy(args, 0, inputDirs, 0, inputDirs.length);
55    InputFileGenerator inputFileGenerator = Icu4jTransformRules.createInputFileGenerator(inputDirs);
56
57    // Pass 1: Establish the set of classes and members that are public in the Android API.
58    System.out.println("Establishing Android public ICU4J API");
59    RecordPublicApiRules recordPublicApiRulesRules = new RecordPublicApiRules(inputFileGenerator);
60    main.execute(recordPublicApiRulesRules);
61    List<String> publicMemberLocatorStrings = recordPublicApiRulesRules.publicMembers();
62    System.out.println("Public API is:");
63    for (String publicMemberLocatorString : publicMemberLocatorStrings) {
64      System.out.println(publicMemberLocatorString);
65    }
66
67    // Pass 2: Check for issues.
68    System.out.println("Checking for issues");
69    Set<String> publicMembersSet = Sets.newHashSet(publicMemberLocatorStrings);
70    File outputReportFile = new File(args[args.length - 1]);
71    FileWriter out = new FileWriter(outputReportFile, false /* append */);
72    try (BufferedWriter reportWriter = new BufferedWriter(out)) {
73      reportWriter.append("Beginning of report:\n");
74      CheckAndroidIcu4jSourceRules reportRules =
75          new CheckAndroidIcu4jSourceRules(inputFileGenerator, publicMembersSet);
76      main.execute(reportRules, reportWriter);
77      reportWriter.append("End of report\n");
78    }
79
80    System.out.println("Report file: " + outputReportFile);
81  }
82
83}
84