1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.tonicsystems.jarjar;
18
19 import com.tonicsystems.jarjar.util.*;
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.*;
23
24 class MainProcessor implements JarProcessor
25 {
26 private final boolean verbose;
27 private final JarProcessorChain chain;
28 private final KeepProcessor kp;
29 private final Map<String, String> renames = new HashMap<String, String>();
30
31 public MainProcessor(List<PatternElement> patterns, boolean verbose, boolean skipManifest) {
32 this.verbose = verbose;
33 List<Zap> zapList = new ArrayList<Zap>();
34 List<Rule> ruleList = new ArrayList<Rule>();
35 List<Keep> keepList = new ArrayList<Keep>();
36 for (PatternElement pattern : patterns) {
37 if (pattern instanceof Zap) {
38 zapList.add((Zap) pattern);
39 } else if (pattern instanceof Rule) {
40 ruleList.add((Rule) pattern);
41 } else if (pattern instanceof Keep) {
42 keepList.add((Keep) pattern);
43 }
44 }
45
46 PackageRemapper pr = new PackageRemapper(ruleList, verbose);
47 kp = keepList.isEmpty() ? null : new KeepProcessor(keepList);
48
49 List<JarProcessor> processors = new ArrayList<JarProcessor>();
50 if (skipManifest)
51 processors.add(ManifestProcessor.getInstance());
52 if (kp != null)
53 processors.add(kp);
54 processors.add(new ZapProcessor(zapList));
55 processors.add(new JarTransformerChain(new RemappingClassTransformer[]{ new RemappingClassTransformer(pr) }));
56 processors.add(new ResourceProcessor(pr));
57 chain = new JarProcessorChain(processors.toArray(new JarProcessor[processors.size()]));
58 }
59
60 public void strip(File file) throws IOException {
61 if (kp == null)
62 return;
63 Set<String> excludes = getExcludes();
64 if (!excludes.isEmpty())
65 StandaloneJarProcessor.run(file, file, new ExcludeProcessor(excludes, verbose));
66 }
67
68
69
70
71
72
73
74 private Set<String> getExcludes() {
75 Set<String> result = new HashSet<String>();
76 for (String exclude : kp.getExcludes()) {
77 String name = exclude + ".class";
78 String renamed = renames.get(name);
79 result.add((renamed != null) ? renamed : name);
80 }
81 return result;
82 }
83
84
85
86
87
88
89
90 public boolean process(EntryStruct struct) throws IOException {
91 String name = struct.name;
92 boolean keepIt = chain.process(struct);
93 if (keepIt) {
94 if (!name.equals(struct.name)) {
95 if (kp != null)
96 renames.put(name, struct.name);
97 if (verbose)
98 System.err.println("Renamed " + name + " -> " + struct.name);
99 }
100 } else {
101 if (verbose)
102 System.err.println("Removed " + name);
103 }
104 return keepIt;
105 }
106 }