• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 package proguard.classfile;
22 
23 import proguard.classfile.visitor.*;
24 
25 import java.util.*;
26 
27 /**
28  * This is a set of representations of classes. They can be enumerated or
29  * retrieved by name. They can also be accessed by means of class visitors.
30  *
31  * @author Eric Lafortune
32  */
33 public class ClassPool
34 {
35     // We're using a sorted tree map instead of a hash map to store the classes,
36     // in order to make the processing more deterministic.
37     private final Map classes = new TreeMap();
38 
39 
40     /**
41      * Clears the class pool.
42      */
clear()43     public void clear()
44     {
45         classes.clear();
46     }
47 
48 
49     /**
50      * Adds the given Clazz to the class pool.
51      */
addClass(Clazz clazz)52     public void addClass(Clazz clazz)
53     {
54         classes.put(clazz.getName(), clazz);
55     }
56 
57 
58     /**
59      * Removes the given Clazz from the class pool.
60      */
removeClass(Clazz clazz)61     public void removeClass(Clazz clazz)
62     {
63         removeClass(clazz.getName());
64     }
65 
66 
67     /**
68      * Removes the specified Clazz from the class pool.
69      */
removeClass(String className)70     public void removeClass(String className)
71     {
72         classes.remove(className);
73     }
74 
75 
76     /**
77      * Returns a Clazz from the class pool based on its name. Returns
78      * <code>null</code> if the class with the given name is not in the class
79      * pool.
80      */
getClass(String className)81     public Clazz getClass(String className)
82     {
83         return (Clazz)classes.get(className);
84     }
85 
86 
87     /**
88      * Returns an Iterator of all class names in the class pool.
89      */
classNames()90     public Iterator classNames()
91     {
92         return classes.keySet().iterator();
93     }
94 
95 
96     /**
97      * Returns the number of classes in the class pool.
98      */
size()99     public int size()
100     {
101         return classes.size();
102     }
103 
104 
105     /**
106      * Applies the given ClassPoolVisitor to the class pool.
107      */
accept(ClassPoolVisitor classPoolVisitor)108     public void accept(ClassPoolVisitor classPoolVisitor)
109     {
110         classPoolVisitor.visitClassPool(this);
111     }
112 
113 
114     /**
115      * Applies the given ClassVisitor to all classes in the class pool,
116      * in random order.
117      */
classesAccept(ClassVisitor classVisitor)118     public void classesAccept(ClassVisitor classVisitor)
119     {
120         Iterator iterator = classes.values().iterator();
121         while (iterator.hasNext())
122         {
123             Clazz clazz = (Clazz)iterator.next();
124             clazz.accept(classVisitor);
125         }
126     }
127 
128 
129     /**
130      * Applies the given ClassVisitor to all classes in the class pool,
131      * in sorted order.
132      */
classesAcceptAlphabetically(ClassVisitor classVisitor)133     public void classesAcceptAlphabetically(ClassVisitor classVisitor)
134     {
135         // We're already using a tree map.
136         //TreeMap sortedClasses = new TreeMap(classes);
137         //Iterator iterator = sortedClasses.values().iterator();
138 
139         Iterator iterator = classes.values().iterator();
140         while (iterator.hasNext())
141         {
142             Clazz clazz = (Clazz)iterator.next();
143             clazz.accept(classVisitor);
144         }
145     }
146 
147 
148     /**
149      * Applies the given ClassVisitor to the class with the given name,
150      * if it is present in the class pool.
151      */
classAccept(String className, ClassVisitor classVisitor)152     public void classAccept(String className, ClassVisitor classVisitor)
153     {
154         Clazz clazz = getClass(className);
155         if (clazz != null)
156         {
157             clazz.accept(classVisitor);
158         }
159     }
160 }
161