~ubuntu-branches/ubuntu/maverick/proguard/maverick

« back to all changes in this revision

Viewing changes to src/proguard/classfile/util/ClassFileClassForNameReferenceInitializer.java

  • Committer: Bazaar Package Importer
  • Author(s): Sam Clegg
  • Date: 2005-11-13 09:42:59 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051113094259-432zf4yyw4890mmn
Tags: 3.4-1
* New upstream release (Closes: #338355)
* debian/control: bump standards version
* debian/copyright: update FSF address
* increase java stack size for proguard and proguardgui

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: ClassFileClassForNameReferenceInitializer.java,v 1.14 2004/12/11 16:35:23 eric Exp $
 
1
/* $Id: ClassFileClassForNameReferenceInitializer.java,v 1.17 2005/07/17 14:47:15 eric Exp $
2
2
 *
3
3
 * ProGuard -- shrinking, optimization, and obfuscation of Java class files.
4
4
 *
5
 
 * Copyright (c) 2002-2004 Eric Lafortune (eric@graphics.cornell.edu)
 
5
 * Copyright (c) 2002-2005 Eric Lafortune (eric@graphics.cornell.edu)
6
6
 *
7
7
 * This program is free software; you can redistribute it and/or modify it
8
8
 * under the terms of the GNU General Public License as published by the Free
21
21
package proguard.classfile.util;
22
22
 
23
23
import proguard.classfile.*;
24
 
import proguard.classfile.attribute.*;
 
24
import proguard.classfile.attribute.CodeAttrInfo;
25
25
import proguard.classfile.instruction.*;
26
 
import proguard.classfile.visitor.*;
 
26
import proguard.classfile.visitor.CpInfoVisitor;
 
27
import proguard.util.ClassNameListMatcher;
27
28
 
28
29
 
29
30
/**
41
42
 *
42
43
 * @author Eric Lafortune
43
44
 */
44
 
class      ClassFileClassForNameReferenceInitializer
45
 
implements InstructionVisitor,
46
 
           CpInfoVisitor
 
45
public class ClassFileClassForNameReferenceInitializer
 
46
  implements InstructionVisitor,
 
47
             CpInfoVisitor
47
48
{
48
 
    private ClassPool programClassPool;
49
 
    private boolean   note;
 
49
    private ClassPool            programClassPool;
 
50
    private ClassPool            libraryClassPool;
 
51
    private boolean              note;
 
52
    private ClassNameListMatcher noteExceptionMatcher;
50
53
 
51
54
    // Counter for notes.
52
 
    private int       noteCount;
 
55
    private int noteCount;
53
56
 
54
57
    // Fields to remember the previous StringCpInfo and MethodRefCpInfo objects
55
58
    // while visiting all instructions (to find Class.forName, class$, and
65
68
    /**
66
69
     * Creates a new ClassFileClassForNameReferenceInitializer that prints notes.
67
70
     */
68
 
    public ClassFileClassForNameReferenceInitializer(ClassPool programClassPool)
 
71
    public ClassFileClassForNameReferenceInitializer(ClassPool programClassPool,
 
72
                                                     ClassPool libraryClassPool)
69
73
    {
70
 
        this(programClassPool, true);
 
74
        this(programClassPool, libraryClassPool, true, null);
71
75
    }
72
76
 
73
77
 
74
78
    /**
75
79
     * Creates a new ClassFileClassForNameReferenceInitializer that optionally
76
 
     * prints notes.
 
80
     * prints notes, with optional class specifications for which never to
 
81
     * print notes.
77
82
     */
78
 
    public ClassFileClassForNameReferenceInitializer(ClassPool programClassPool,
79
 
                                                     boolean   note)
 
83
    public ClassFileClassForNameReferenceInitializer(ClassPool            programClassPool,
 
84
                                                     ClassPool            libraryClassPool,
 
85
                                                     boolean              note,
 
86
                                                     ClassNameListMatcher noteExceptionMatcher)
80
87
    {
81
 
        this.programClassPool = programClassPool;
82
 
        this.note             = note;
 
88
        this.programClassPool     = programClassPool;
 
89
        this.libraryClassPool     = libraryClassPool;
 
90
        this.note                 = note;
 
91
        this.noteExceptionMatcher = noteExceptionMatcher;
83
92
    }
84
93
 
85
94
 
95
104
 
96
105
    // Implementations for InstructionVisitor.
97
106
 
98
 
    public void visitSimpleInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, SimpleInstruction simpleInstruction) {}
99
 
    public void visitBranchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, BranchInstruction branchInstruction) {}
100
 
    public void visitTableSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, TableSwitchInstruction tableSwitchInstruction) {}
101
 
    public void visitLookUpSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, LookUpSwitchInstruction lookUpSwitchInstruction) {}
102
 
 
103
 
 
 
107
    public void visitSimpleInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, SimpleInstruction simpleInstruction)
 
108
    {
 
109
        // Nothing interesting; just forget any stored indices.
 
110
        clearConstantPoolIndices();
 
111
    }
 
112
    
 
113
    
 
114
    public void visitBranchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, BranchInstruction branchInstruction)
 
115
    {
 
116
        // Nothing interesting; just forget any stored indices.
 
117
        clearConstantPoolIndices();
 
118
    }
 
119
    
 
120
    
 
121
    public void visitTableSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, TableSwitchInstruction tableSwitchInstruction)
 
122
    {
 
123
        // Nothing interesting; just forget any stored indices.
 
124
        clearConstantPoolIndices();
 
125
    }
 
126
    
 
127
    
 
128
    public void visitLookUpSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
 
129
    {
 
130
        // Nothing interesting; just forget any stored indices.
 
131
        clearConstantPoolIndices();
 
132
    }
 
133
    
 
134
    
104
135
    public void visitVariableInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, VariableInstruction variableInstruction)
105
136
    {
 
137
 
106
138
        // Just ignore generic instructions and reset the constant pool indices.
107
139
        switch (variableInstruction.opcode)
108
140
        {
139
171
                    // class$(String, boolean).
140
172
                    ldcStringCpIndex = currentCpIndex;
141
173
                }
 
174
                else
 
175
                {
 
176
                    ldcStringCpIndex = -1;
 
177
                }
 
178
 
142
179
                invokestaticMethodRefCpIndex  = -1;
143
180
                invokevirtualMethodRefCpIndex = -1;
144
181
                break;
201
238
                    }
202
239
                }
203
240
 
204
 
                ldcStringCpIndex              = -1;
205
 
                invokestaticMethodRefCpIndex  = -1;
206
 
                invokevirtualMethodRefCpIndex = -1;
 
241
                // We've handled the special case; forget about the indices.
 
242
                clearConstantPoolIndices();
207
243
                break;
208
244
 
209
245
            default:
210
 
                // Nothing interesting; just forget about previous indices.
211
 
                ldcStringCpIndex              = -1;
212
 
                invokestaticMethodRefCpIndex  = -1;
213
 
                invokevirtualMethodRefCpIndex = -1;
 
246
                // Nothing interesting; just forget any stored indices.
 
247
                clearConstantPoolIndices();
214
248
                break;
215
249
        }
216
250
    }
238
272
        String externalClassName = stringCpInfo.getString(classFile);
239
273
        String internalClassName = ClassUtil.internalClassName(externalClassName);
240
274
 
241
 
        stringCpInfo.referencedClassFile = programClassPool.getClass(internalClassName);
 
275
        stringCpInfo.referencedClassFile = findClass(internalClassName);
242
276
    }
243
277
 
244
278
 
247
281
     */
248
282
    public void visitClassCpInfo(ClassFile classFile, ClassCpInfo classCpInfo)
249
283
    {
250
 
        if (note)
 
284
        if (note &&
 
285
            (noteExceptionMatcher == null ||
 
286
             !noteExceptionMatcher.matches(classCpInfo.getName(classFile))))
251
287
        {
252
288
            noteCount++;
253
289
            System.err.println("Note: " +
257
293
                               ")Class.forName(variable).newInstance()'");
258
294
        }
259
295
    }
 
296
 
 
297
 
 
298
    // Small utility methods.
 
299
    
 
300
    /**
 
301
     * Clears all references to the constant pool.
 
302
     */
 
303
    private void clearConstantPoolIndices()
 
304
    {
 
305
        ldcStringCpIndex              = -1;
 
306
        invokestaticMethodRefCpIndex  = -1;
 
307
        invokevirtualMethodRefCpIndex = -1;
 
308
    }
 
309
    
 
310
    
 
311
    /**
 
312
     * Returns the class with the given name, either for the program class pool
 
313
     * or from the library class pool, or <code>null</code> if it can't be found.
 
314
     */
 
315
    private ClassFile findClass(String name)
 
316
    {
 
317
        // First look for the class in the program class pool.
 
318
        ClassFile classFile = programClassPool.getClass(name);
 
319
 
 
320
        // Otherwise look for the class in the library class pool.
 
321
        if (classFile == null)
 
322
        {
 
323
            classFile = libraryClassPool.getClass(name);
 
324
        }
 
325
 
 
326
        return classFile;
 
327
    }
260
328
}