summaryrefslogtreecommitdiff
path: root/src/main/com/tonicsystems/jarjar/MainProcessor.java
blob: 5839719e85aa32cfa8f0bce03e74d6a1edd46658 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonicsystems.jarjar;

import com.android.jarjar.RemoveAndroidCompatAnnotationsJarTransformer;
import com.android.jarjar.StripAnnotation;
import com.android.jarjar.StripAnnotationsJarTransformer;
import com.tonicsystems.jarjar.util.EntryStruct;
import com.tonicsystems.jarjar.util.JarProcessor;
import com.tonicsystems.jarjar.util.JarProcessorChain;
import com.tonicsystems.jarjar.util.JarTransformerChain;
import com.tonicsystems.jarjar.util.RemappingClassTransformer;
import com.tonicsystems.jarjar.util.StandaloneJarProcessor;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

class MainProcessor implements JarProcessor {
  private final boolean verbose;
  private final JarProcessorChain chain;
  private final KeepProcessor kp;
  private final Map<String, String> renames = new HashMap<>();

  // ANDROID-BEGIN: b/146418363 Add an Android-specific transformer to strip compat annotation
  public MainProcessor(List<PatternElement> patterns, boolean verbose, boolean skipManifest) {
    this(patterns, verbose, skipManifest, false /* removeAndroidCompatAnnotations */);
  }

  public MainProcessor(
      List<PatternElement> patterns,
      boolean verbose,
      boolean skipManifest,
      boolean removeAndroidCompatAnnotations) {
    // ANDROID-END: b/146418363 Add an Android-specific transformer to strip compat annotation
    this.verbose = verbose;
    List<Zap> zapList = new ArrayList<>();
    List<Rule> ruleList = new ArrayList<>();
    List<Keep> keepList = new ArrayList<>();
    // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
    List<StripAnnotation> stripAnnotationList = new ArrayList<StripAnnotation>();
    // ANDROID-END: b/222743634 Strip annotations from system module stubs
    for (PatternElement pattern : patterns) {
      if (pattern instanceof Zap) {
        zapList.add((Zap) pattern);
      } else if (pattern instanceof Rule) {
        ruleList.add((Rule) pattern);
      } else if (pattern instanceof Keep) {
        keepList.add((Keep) pattern);
      // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
      } else if (pattern instanceof StripAnnotation) {
        stripAnnotationList.add((StripAnnotation) pattern);
      }
      // ANDROID-END: b/222743634 Strip annotations from system module stubs
    }

    PackageRemapper pr = new PackageRemapper(ruleList, verbose);
    kp = keepList.isEmpty() ? null : new KeepProcessor(keepList);

    List<JarProcessor> processors = new ArrayList<>();
    if (skipManifest) {
      processors.add(ManifestProcessor.getInstance());
    }
    if (kp != null) {
      processors.add(kp);
    }
    processors.add(new ZapProcessor(zapList));
    // ANDROID-BEGIN: b/146418363 Add an Android-specific transformer to strip compat annotation
    if (removeAndroidCompatAnnotations)
      processors.add(new RemoveAndroidCompatAnnotationsJarTransformer(pr));
    // ANDROID-END: b/146418363 Add an Android-specific transformer to strip compat annotation
    // ANDROID-BEGIN: b/222743634 Strip annotations from system module stubs
    if (!stripAnnotationList.isEmpty()) {
      processors.add(new StripAnnotationsJarTransformer(stripAnnotationList));
    }
    // ANDROID-END: b/222743634 Strip annotations from system module stubs
    processors.add(
        new JarTransformerChain(
            new RemappingClassTransformer[] {new RemappingClassTransformer(pr)}));
    processors.add(new ResourceProcessor(pr));
    processors.add(new ServiceProcessor(pr));
    chain = new JarProcessorChain(processors.toArray(new JarProcessor[0]));
  }

  public void strip(File file) throws IOException {
    if (kp == null) {
      return;
    }
    Set<String> excludes = getExcludes();
    if (!excludes.isEmpty()) {
      StandaloneJarProcessor.run(file, file, new ExcludeProcessor(excludes, verbose));
    }
  }

  /**
   * Returns the <code>.class</code> files to delete. As well the root-parameter as the rename ones
   * are taken in consideration, so that the concerned files are not listed in the result.
   *
   * @return the paths of the files in the jar-archive, including the <code>.class</code> suffix
   */
  private Set<String> getExcludes() {
    Set<String> result = new HashSet<>();
    for (String exclude : kp.getExcludes()) {
      String name = exclude + ".class";
      String renamed = renames.get(name);
      result.add((renamed != null) ? renamed : name);
    }
    return result;
  }

  /**
   * @param struct
   * @return <code>true</code> if the entry is to include in the output jar
   * @throws IOException
   */
  public boolean process(EntryStruct struct) throws IOException {
    String name = struct.name;
    boolean keepIt = chain.process(struct);
    if (keepIt) {
      if (!name.equals(struct.name)) {
        if (kp != null) {
          renames.put(name, struct.name);
        }
        if (verbose) {
          System.err.println("Renamed " + name + " -> " + struct.name);
        }
      }
    } else {
      if (verbose) {
        System.err.println("Removed " + name);
      }
    }
    return keepIt;
  }
}