1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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 */
21package proguard.classfile;
22
23import proguard.classfile.util.ClassUtil;
24import proguard.classfile.visitor.*;
25
26import 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 */
34public class ClassPool
35{
36    // We're using a sorted tree map instead of a hash map to store the classes,
37    // in order to make the processing more deterministic.
38    private final Map classes = new TreeMap();
39
40
41    /**
42     * Clears the class pool.
43     */
44    public void clear()
45    {
46        classes.clear();
47    }
48
49
50    /**
51     * Adds the given Clazz to the class pool.
52     */
53    public void addClass(Clazz clazz)
54    {
55        classes.put(clazz.getName(), clazz);
56    }
57
58
59    /**
60     * Removes the given Clazz from the class pool.
61     */
62    public void removeClass(Clazz clazz)
63    {
64        removeClass(clazz.getName());
65    }
66
67
68    /**
69     * Removes the specified Clazz from the class pool.
70     */
71    public void removeClass(String className)
72    {
73        classes.remove(className);
74    }
75
76
77    /**
78     * Returns a Clazz from the class pool based on its name. Returns
79     * <code>null</code> if the class with the given name is not in the class
80     * pool.
81     */
82    public Clazz getClass(String className)
83    {
84        return (Clazz)classes.get(className);
85    }
86
87
88    /**
89     * Returns an Iterator of all class names in the class pool.
90     */
91    public Iterator classNames()
92    {
93        return classes.keySet().iterator();
94    }
95
96
97    /**
98     * Returns the number of classes in the class pool.
99     */
100    public int size()
101    {
102        return classes.size();
103    }
104
105
106    /**
107     * Applies the given ClassPoolVisitor to the class pool.
108     */
109    public void accept(ClassPoolVisitor classPoolVisitor)
110    {
111        classPoolVisitor.visitClassPool(this);
112    }
113
114
115    /**
116     * Applies the given ClassVisitor to all classes in the class pool,
117     * in random order.
118     */
119    public void classesAccept(ClassVisitor classVisitor)
120    {
121        Iterator iterator = classes.values().iterator();
122        while (iterator.hasNext())
123        {
124            Clazz clazz = (Clazz)iterator.next();
125            clazz.accept(classVisitor);
126        }
127    }
128
129
130    /**
131     * Applies the given ClassVisitor to all classes in the class pool,
132     * in sorted order.
133     */
134    public void classesAcceptAlphabetically(ClassVisitor classVisitor)
135    {
136        // We're already using a tree map.
137        //TreeMap sortedClasses = new TreeMap(classes);
138        //Iterator iterator = sortedClasses.values().iterator();
139
140        Iterator iterator = classes.values().iterator();
141        while (iterator.hasNext())
142        {
143            Clazz clazz = (Clazz)iterator.next();
144            clazz.accept(classVisitor);
145        }
146    }
147
148
149    /**
150     * Applies the given ClassVisitor to the class with the given name,
151     * if it is present in the class pool.
152     */
153    public void classAccept(String className, ClassVisitor classVisitor)
154    {
155        Clazz clazz = getClass(className);
156        if (clazz != null)
157        {
158            clazz.accept(classVisitor);
159        }
160    }
161}
162