StringReferenceInitializer.java revision db267bc191f906f55eaef21a27110cce2ec57fdf
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 */
21package proguard.classfile.util;
22
23import proguard.classfile.*;
24import proguard.classfile.constant.*;
25import proguard.classfile.constant.visitor.ConstantVisitor;
26
27/**
28 * This ConstantVisitor initializes any class references of all string constants
29 * it visits. More specifically, it fills out the references of string constant
30 * pool entries that happen to refer to a class in the program class pool or in
31 * the library class pool.
32 *
33 * @author Eric Lafortune
34 */
35public class StringReferenceInitializer
36extends      SimplifiedVisitor
37implements   ConstantVisitor
38{
39    private final ClassPool programClassPool;
40    private final ClassPool libraryClassPool;
41
42
43    /**
44     * Creates a new StringReferenceInitializer.
45     */
46    public StringReferenceInitializer(ClassPool programClassPool,
47                                      ClassPool libraryClassPool)
48    {
49        this.programClassPool = programClassPool;
50        this.libraryClassPool = libraryClassPool;
51    }
52
53
54    // Implementations for ConstantVisitor.
55
56    public void visitAnyConstant(Clazz clazz, Constant constant) {}
57
58
59    public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
60    {
61        if (stringConstant.referencedClass == null)
62        {
63            // See if we can find the referenced class.
64            stringConstant.referencedClass =
65                findClass(ClassUtil.internalClassName(stringConstant.getString(clazz)));
66        }
67    }
68
69
70    // Small utility methods.
71
72    /**
73     * Returns the class with the given name, either for the program class pool
74     * or from the library class pool, or <code>null</code> if it can't be found.
75     */
76    private Clazz findClass(String name)
77    {
78        // First look for the class in the program class pool.
79        Clazz clazz = programClassPool.getClass(name);
80
81        // Otherwise look for the class in the library class pool.
82        if (clazz == null)
83        {
84            clazz = libraryClassPool.getClass(name);
85        }
86
87        return clazz;
88    }
89}