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 */
21package proguard.classfile.visitor;
22
23import proguard.classfile.*;
24
25/**
26 * This <code>ClassVisitor</code> delegates its visits to one of two
27 * <code>ClassVisitor</code> instances, depending on whether the name of
28 * the visited class file is present in a given <code>ClassPool</code> or not.
29 *
30 * @author Eric Lafortune
31 */
32public class ClassPresenceFilter implements ClassVisitor
33{
34    private final ClassPool    classPool;
35    private final ClassVisitor presentClassVisitor;
36    private final ClassVisitor missingClassVisitor;
37
38
39    /**
40     * Creates a new ClassPresenceFilter.
41     * @param classPool           the <code>ClassPool</code> in which the
42     *                            presence will be tested.
43     * @param presentClassVisitor the <code>ClassVisitor</code> to which visits
44     *                            of present class files will be delegated.
45     * @param missingClassVisitor the <code>ClassVisitor</code> to which visits
46     *                            of missing class files will be delegated.
47     */
48    public ClassPresenceFilter(ClassPool    classPool,
49                               ClassVisitor presentClassVisitor,
50                               ClassVisitor missingClassVisitor)
51    {
52        this.classPool           = classPool;
53        this.presentClassVisitor = presentClassVisitor;
54        this.missingClassVisitor = missingClassVisitor;
55    }
56
57
58    // Implementations for ClassVisitor.
59
60    public void visitProgramClass(ProgramClass programClass)
61    {
62        ClassVisitor classFileVisitor = classFileVisitor(programClass);
63
64        if (classFileVisitor != null)
65        {
66            classFileVisitor.visitProgramClass(programClass);
67        }
68    }
69
70
71    public void visitLibraryClass(LibraryClass libraryClass)
72    {
73        ClassVisitor classFileVisitor = classFileVisitor(libraryClass);
74
75        if (classFileVisitor != null)
76        {
77            classFileVisitor.visitLibraryClass(libraryClass);
78        }
79    }
80
81
82    // Small utility methods.
83
84    /**
85     * Returns the appropriate <code>ClassVisitor</code>.
86     */
87    private ClassVisitor classFileVisitor(Clazz clazz)
88    {
89        return classPool.getClass(clazz.getName()) != null ?
90            presentClassVisitor :
91            missingClassVisitor;
92    }
93}
94