• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2009 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.util.ClassUtil;
24 import proguard.classfile.visitor.*;
25 
26 import java.util.*;
27 
28 /**
29  * This is a set of representations of classes. They      can be enumerated or
30  * retrieved by name. They can also be accessed by means of class visitors.
31  *
32  * @author Eric Lafortune
33  */
34 public class ClassPool
35 {
36     private final Map classes = new HashMap();
37 
38 
39     /**
40      * Clears the class pool.
41      */
clear()42     public void clear()
43     {
44         classes.clear();
45     }
46 
47 
48     /**
49      * Adds the given Clazz to the class pool.
50      */
addClass(Clazz clazz)51     public void addClass(Clazz clazz)
52     {
53         classes.put(clazz.getName(), clazz);
54     }
55 
56 
57     /**
58      * Removes the given Clazz from the class pool.
59      */
removeClass(Clazz clazz)60     public void removeClass(Clazz clazz)
61     {
62         classes.remove(clazz.getName());
63     }
64 
65 
66     /**
67      * Returns a Clazz from the class pool based on its name. Returns
68      * <code>null</code> if the class with the given name is not in the class
69      * pool. Returns the base class if the class name is an array type.
70      */
getClass(String className)71     public Clazz getClass(String className)
72     {
73         return (Clazz)classes.get(ClassUtil.internalClassNameFromClassType(className));
74     }
75 
76 
77     /**
78      * Returns an Iterator of all class names in the class pool.
79      */
classNames()80     public Iterator classNames()
81     {
82         return classes.keySet().iterator();
83     }
84 
85 
86     /**
87      * Returns the number of classes in the class pool.
88      */
size()89     public int size()
90     {
91         return classes.size();
92     }
93 
94 
95     /**
96      * Applies the given ClassPoolVisitor to the class pool.
97      */
accept(ClassPoolVisitor classPoolVisitor)98     public void accept(ClassPoolVisitor classPoolVisitor)
99     {
100         classPoolVisitor.visitClassPool(this);
101     }
102 
103 
104     /**
105      * Applies the given ClassVisitor to all classes in the class pool,
106      * in random order.
107      */
classesAccept(ClassVisitor classVisitor)108     public void classesAccept(ClassVisitor classVisitor)
109     {
110         Iterator iterator = classes.values().iterator();
111         while (iterator.hasNext())
112         {
113             Clazz clazz = (Clazz)iterator.next();
114             clazz.accept(classVisitor);
115         }
116     }
117 
118 
119     /**
120      * Applies the given ClassVisitor to all classes in the class pool,
121      * in sorted order.
122      */
classesAcceptAlphabetically(ClassVisitor classVisitor)123     public void classesAcceptAlphabetically(ClassVisitor classVisitor)
124     {
125         TreeMap sortedClasses = new TreeMap(classes);
126         Iterator iterator = sortedClasses.values().iterator();
127         while (iterator.hasNext())
128         {
129             Clazz clazz = (Clazz)iterator.next();
130             clazz.accept(classVisitor);
131         }
132     }
133 
134 
135     /**
136      * Applies the given ClassVisitor to the class with the given name,
137      * if it is present in the class pool.
138      */
classAccept(String className, ClassVisitor classVisitor)139     public void classAccept(String className, ClassVisitor classVisitor)
140     {
141         Clazz clazz = getClass(className);
142         if (clazz != null)
143         {
144             clazz.accept(classVisitor);
145         }
146     }
147 }
148