• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2007 Google Inc.
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 com.tonicsystems.jarjar;
18 
19 import static java.nio.charset.StandardCharsets.UTF_8;
20 
21 import com.tonicsystems.jarjar.util.RuntimeIOException;
22 import com.tonicsystems.jarjar.util.StandaloneJarProcessor;
23 import java.io.BufferedReader;
24 import java.io.BufferedWriter;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.OutputStreamWriter;
30 import java.io.PrintWriter;
31 import java.util.List;
32 
33 public class Main {
34 
35   private static final String LINE_SEPARATOR = System.getProperty("line.separator");
36   private static final String HELP;
37 
38   static {
39     try {
40       HELP = readIntoString(Main.class.getResourceAsStream("help.txt"));
41     } catch (IOException e) {
42       throw new RuntimeIOException(e);
43     }
44   }
45 
readIntoString(InputStream in)46   private static String readIntoString(InputStream in) throws IOException {
47     StringBuilder sb = new StringBuilder();
48     BufferedReader r = new BufferedReader(new InputStreamReader(in, UTF_8));
49     String line = null;
50     while ((line = r.readLine()) != null) {
51       sb.append(line).append(LINE_SEPARATOR);
52     }
53     return sb.toString();
54   }
55 
main(String[] args)56   public static void main(String[] args) throws Exception {
57     MainUtil.runMain(new Main(), args, "help");
58   }
59 
help()60   public void help() {
61     System.err.print(HELP);
62   }
63 
strings(String cp)64   public void strings(String cp) throws IOException {
65     if (cp == null) {
66       throw new IllegalArgumentException("cp is required");
67     }
68     new StringDumper()
69         .run(cp, new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8))));
70   }
71 
72   // TODO: make level an enum
find(String level, String cp1, String cp2)73   public void find(String level, String cp1, String cp2) throws IOException {
74     if (level == null || cp1 == null) {
75       throw new IllegalArgumentException("level and cp1 are required");
76     }
77     if (cp2 == null) {
78       cp2 = cp1;
79     }
80     int levelFlag;
81     if ("class".equals(level)) {
82       levelFlag = DepHandler.LEVEL_CLASS;
83     } else if ("jar".equals(level)) {
84       levelFlag = DepHandler.LEVEL_JAR;
85     } else {
86       throw new IllegalArgumentException("unknown level " + level);
87     }
88     PrintWriter w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8)));
89     DepHandler handler = new TextDepHandler(w, levelFlag);
90     new DepFind().run(cp1, cp2, handler);
91     w.flush();
92   }
93 
process(File rulesFile, File inJar, File outJar)94   public void process(File rulesFile, File inJar, File outJar) throws IOException {
95     if (rulesFile == null || inJar == null || outJar == null) {
96       throw new IllegalArgumentException("rulesFile, inJar, and outJar are required");
97     }
98     List<PatternElement> rules = RulesFileParser.parse(rulesFile);
99     boolean verbose = Boolean.getBoolean("verbose");
100     boolean skipManifest = Boolean.getBoolean("skipManifest");
101     // ANDROID-BEGIN: b/146418363 Add an Android-specific transformer to strip compat annotation
102     boolean removeAndroidCompatAnnotations = Boolean.getBoolean("removeAndroidCompatAnnotations");
103     MainProcessor proc =
104         new MainProcessor(rules, verbose, skipManifest, removeAndroidCompatAnnotations);
105     // ANDROID-END: b/146418363 Add an Android-specific transformer to strip compat annotation
106     StandaloneJarProcessor.run(inJar, outJar, proc);
107     proc.strip(outJar);
108   }
109 }
110