11/**
22 * Copyright 2007 Google Inc.
3- *
3+ * <p>
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
7- *
7+ * <p>
88 * http://www.apache.org/licenses/LICENSE-2.0
9- *
9+ * <p>
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
1212 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1717package com .eed3si9n .jarjar ;
1818
1919import com .eed3si9n .jarjar .util .*;
20+
2021import java .io .*;
2122import java .util .*;
23+
2224import org .objectweb .asm .*;
23- import org .objectweb .asm .Type ;
2425import org .objectweb .asm .commons .*;
2526
2627// TODO: this can probably be refactored into JarClassVisitor, etc.
27- class KeepProcessor extends Remapper implements JarProcessor
28- {
28+ class KeepProcessor extends Remapper implements JarProcessor {
2929 private final ClassVisitor cv = new ClassRemapper (new EmptyClassVisitor (), this );
3030 private final List <Wildcard > wildcards ;
3131 private final List <String > roots = new ArrayList <String >();
@@ -41,56 +41,57 @@ public boolean isEnabled() {
4141
4242 public Set <String > getExcludes () {
4343 Set <String > closure = new HashSet <String >();
44- closureHelper (closure , roots );
44+ recursiveProcessDependencies (closure , roots );
45+
4546 Set <String > removable = new HashSet <String >(depend .keySet ());
4647 removable .removeAll (closure );
4748 return removable ;
4849 }
4950
50- private void closureHelper (Set <String > closure , Collection <String > process ) {
51- if (process == null )
51+ private void recursiveProcessDependencies (Set <String > result , Collection <String > roots ) {
52+ if (roots == null )
5253 return ;
53- for (String name : process ) {
54- if (closure .add (name ))
55- closureHelper ( closure , depend .get (name ));
54+ for (String name : roots ) {
55+ if (result .add (name ))
56+ recursiveProcessDependencies ( result , depend .get (name ));
5657 }
5758 }
5859
59- private Set <String > curSet ;
60- private byte [] buf = new byte [0x2000 ];
60+ private Set <String > currentDependenciesSet ;
6161
6262 public boolean process (EntryStruct struct ) throws IOException {
63- try {
64- if (struct .name .endsWith (".class" )) {
65- String name = struct .name .substring (0 , struct .name .length () - 6 );
66- for (Wildcard wildcard : wildcards ) {
67- if (wildcard .matches (name )) {
68- roots .add (name );
69- depend .put (name , curSet = new HashSet <String >());
70- new ClassReader (new ByteArrayInputStream (struct .data )).accept (cv ,
71- ClassReader .EXPAND_FRAMES );
72- curSet .remove (name );
73- return true ;
74- }
63+ if (struct .name .endsWith (".class" )) {
64+ String name = struct .name .substring (0 , struct .name .length () - 6 );
65+ depend .put (name , currentDependenciesSet = new HashSet <String >());
66+ try {
67+ new ClassReader (new ByteArrayInputStream (struct .data )).accept (cv ,
68+ ClassReader .EXPAND_FRAMES );
69+ currentDependenciesSet .remove (name );
70+ } catch (Exception e ) {
71+ System .err .println ("Error reading " + struct .name + ": " + e .getMessage ());
72+ }
73+
74+ for (Wildcard wildcard : wildcards ) {
75+ if (wildcard .matches (name )) {
76+ roots .add (name );
77+ return true ;
7578 }
76- return false ;
7779 }
78- } catch (Exception e ) {
79- System .err .println ("Error reading " + struct .name + ": " + e .getMessage ());
80+ return false ;
8081 }
8182 return true ;
8283 }
8384
8485 public String map (String key ) {
8586 if (key .startsWith ("java/" ) || key .startsWith ("javax/" ))
8687 return null ;
87- curSet .add (key );
88+ currentDependenciesSet .add (key );
8889 return null ;
8990 }
9091
9192 public Object mapValue (Object value ) {
9293 if (value instanceof String ) {
93- String s = (String )value ;
94+ String s = (String ) value ;
9495 if (PackageRemapper .isArrayForName (s )) {
9596 mapDesc (s .replace ('.' , '/' ));
9697 } else if (isForName (s )) {
@@ -113,4 +114,4 @@ private static boolean isForName(String value) {
113114 }
114115 return true ;
115116 }
116- }
117+ }
0 commit comments