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 $
3
3
* ProGuard -- shrinking, optimization, and obfuscation of Java class files.
5
* Copyright (c) 2002-2004 Eric Lafortune (eric@graphics.cornell.edu)
5
* Copyright (c) 2002-2005 Eric Lafortune (eric@graphics.cornell.edu)
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;
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;
42
43
* @author Eric Lafortune
44
class ClassFileClassForNameReferenceInitializer
45
implements InstructionVisitor,
45
public class ClassFileClassForNameReferenceInitializer
46
implements InstructionVisitor,
48
private ClassPool programClassPool;
49
private ClassPool programClassPool;
50
private ClassPool libraryClassPool;
52
private ClassNameListMatcher noteExceptionMatcher;
51
54
// Counter for notes.
52
private int noteCount;
55
private int noteCount;
54
57
// Fields to remember the previous StringCpInfo and MethodRefCpInfo objects
55
58
// while visiting all instructions (to find Class.forName, class$, and
66
69
* Creates a new ClassFileClassForNameReferenceInitializer that prints notes.
68
public ClassFileClassForNameReferenceInitializer(ClassPool programClassPool)
71
public ClassFileClassForNameReferenceInitializer(ClassPool programClassPool,
72
ClassPool libraryClassPool)
70
this(programClassPool, true);
74
this(programClassPool, libraryClassPool, true, null);
75
79
* Creates a new ClassFileClassForNameReferenceInitializer that optionally
80
* prints notes, with optional class specifications for which never to
78
public ClassFileClassForNameReferenceInitializer(ClassPool programClassPool,
83
public ClassFileClassForNameReferenceInitializer(ClassPool programClassPool,
84
ClassPool libraryClassPool,
86
ClassNameListMatcher noteExceptionMatcher)
81
this.programClassPool = programClassPool;
88
this.programClassPool = programClassPool;
89
this.libraryClassPool = libraryClassPool;
91
this.noteExceptionMatcher = noteExceptionMatcher;
96
105
// Implementations for InstructionVisitor.
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) {}
107
public void visitSimpleInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, SimpleInstruction simpleInstruction)
109
// Nothing interesting; just forget any stored indices.
110
clearConstantPoolIndices();
114
public void visitBranchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, BranchInstruction branchInstruction)
116
// Nothing interesting; just forget any stored indices.
117
clearConstantPoolIndices();
121
public void visitTableSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, TableSwitchInstruction tableSwitchInstruction)
123
// Nothing interesting; just forget any stored indices.
124
clearConstantPoolIndices();
128
public void visitLookUpSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, LookUpSwitchInstruction lookUpSwitchInstruction)
130
// Nothing interesting; just forget any stored indices.
131
clearConstantPoolIndices();
104
135
public void visitVariableInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, VariableInstruction variableInstruction)
106
138
// Just ignore generic instructions and reset the constant pool indices.
107
139
switch (variableInstruction.opcode)
204
ldcStringCpIndex = -1;
205
invokestaticMethodRefCpIndex = -1;
206
invokevirtualMethodRefCpIndex = -1;
241
// We've handled the special case; forget about the indices.
242
clearConstantPoolIndices();
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();
257
293
")Class.forName(variable).newInstance()'");
298
// Small utility methods.
301
* Clears all references to the constant pool.
303
private void clearConstantPoolIndices()
305
ldcStringCpIndex = -1;
306
invokestaticMethodRefCpIndex = -1;
307
invokevirtualMethodRefCpIndex = -1;
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.
315
private ClassFile findClass(String name)
317
// First look for the class in the program class pool.
318
ClassFile classFile = programClassPool.getClass(name);
320
// Otherwise look for the class in the library class pool.
321
if (classFile == null)
323
classFile = libraryClassPool.getClass(name);