01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * 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: */
21: package proguard.classfile.visitor;
22:
23: import proguard.classfile.*;
24: import proguard.classfile.attribute.CodeAttribute;
25: import proguard.classfile.constant.*;
26: import proguard.classfile.constant.visitor.ConstantVisitor;
27: import proguard.classfile.instruction.*;
28: import proguard.classfile.instruction.visitor.InstructionVisitor;
29: import proguard.classfile.util.SimplifiedVisitor;
30:
31: /**
32: * This InstructionVisitor lets a given <code>ClassVisitor</code> visit all
33: * classes involved in any <code>.class</code> constructs that it visits.
34: * <p>
35: * Note that before JDK 1.5, <code>.class</code> constructs are actually
36: * compiled differently, using <code>Class.forName</code> constructs.
37: *
38: * @see ClassForNameClassVisitor
39: *
40: * @author Eric Lafortune
41: */
42: public class DotClassClassVisitor extends SimplifiedVisitor implements
43: InstructionVisitor, ConstantVisitor {
44: private final ClassVisitor classVisitor;
45:
46: /**
47: * Creates a new ClassHierarchyTraveler.
48: * @param classVisitor the <code>ClassVisitor</code> to which visits will
49: * be delegated.
50: */
51: public DotClassClassVisitor(ClassVisitor classVisitor) {
52: this .classVisitor = classVisitor;
53: }
54:
55: // Implementations for InstructionVisitor.
56:
57: public void visitAnyInstruction(Clazz clazz, Method method,
58: CodeAttribute codeAttribute, int offset,
59: Instruction instruction) {
60: }
61:
62: public void visitConstantInstruction(Clazz clazz, Method method,
63: CodeAttribute codeAttribute, int offset,
64: ConstantInstruction constantInstruction) {
65: byte opcode = constantInstruction.opcode;
66:
67: // Could this instruction be a .class construct?
68: if (opcode == InstructionConstants.OP_LDC
69: || opcode == InstructionConstants.OP_LDC_W) {
70: clazz.constantPoolEntryAccept(
71: constantInstruction.constantIndex, this );
72: }
73: }
74:
75: // Implementations for ConstantVisitor.
76:
77: public void visitAnyConstant(Clazz clazz, Constant constant) {
78: }
79:
80: public void visitClassConstant(Clazz clazz,
81: ClassConstant classConstant) {
82: // Visit the referenced class from the .class construct.
83: classConstant.referencedClassAccept(classVisitor);
84: }
85: }
|