1c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko/* 2c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * ProGuard -- shrinking, optimization, obfuscation, and preverification 3c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * of Java bytecode. 4c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * 59ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu) 6c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * 78f815f563c71645fd218977f16de8e746bec28a6Alex Vakulenko * This program is free software; you can redistribute it and/or modify it 88f815f563c71645fd218977f16de8e746bec28a6Alex Vakulenko * under the terms of the GNU General Public License as published by the Free 99ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko * Software Foundation; either version 2 of the License, or (at your option) 10c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * any later version. 11c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * 12c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * This program is distributed in the hope that it will be useful, but WITHOUT 139ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1405d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1505d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko * more details. 1605d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko * 1705d29044d14a60775ed6c51c75a414eb0cb50347Alex Vakulenko * You should have received a copy of the GNU General Public License along 188f815f563c71645fd218977f16de8e746bec28a6Alex Vakulenko * with this program; if not, write to the Free Software Foundation, Inc., 19c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko */ 219ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenkopackage proguard.classfile.visitor; 229ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko 238f815f563c71645fd218977f16de8e746bec28a6Alex Vakulenkoimport proguard.classfile.*; 24c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkoimport proguard.classfile.attribute.CodeAttribute; 25c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkoimport proguard.classfile.constant.*; 26c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkoimport proguard.classfile.constant.visitor.ConstantVisitor; 27c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkoimport proguard.classfile.instruction.*; 28c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkoimport proguard.classfile.instruction.visitor.InstructionVisitor; 299ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenkoimport proguard.classfile.util.SimplifiedVisitor; 30c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko 31c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko 32c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko/** 338f815f563c71645fd218977f16de8e746bec28a6Alex Vakulenko * This InstructionVisitor lets a given <code>ClassVisitor</code> visit all 348f815f563c71645fd218977f16de8e746bec28a6Alex Vakulenko * classes involved in any <code>.class</code> constructs that it visits. 358f815f563c71645fd218977f16de8e746bec28a6Alex Vakulenko * <p> 36c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * Note that before JDK 1.5, <code>.class</code> constructs are actually 37c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * compiled differently, using <code>Class.forName</code> constructs. 38c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * 39c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * @author Eric Lafortune 40c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko */ 41c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkopublic class DotClassClassVisitor 42c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkoextends SimplifiedVisitor 43c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenkoimplements InstructionVisitor, 44c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko ConstantVisitor 45c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko{ 46c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko private final ClassVisitor classVisitor; 47c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko 489ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko 49c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko /** 50c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * Creates a new ClassHierarchyTraveler. 51c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * @param classVisitor the <code>ClassVisitor</code> to which visits will 52c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko * be delegated. 53c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko */ 54c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko public DotClassClassVisitor(ClassVisitor classVisitor) 55c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko { 56c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko this.classVisitor = classVisitor; 57c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko } 58c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko 59c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko 60c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko // Implementations for InstructionVisitor. 61c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko 62c909a9f2ea6f99d0d3ac3efd5ae1114f576cd377Alex Vakulenko public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {} 63c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka 64c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka 659ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction) 669ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko { 679ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko byte opcode = constantInstruction.opcode; 689ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko 69c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka // Could this instruction be a .class construct? 70c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka if (opcode == InstructionConstants.OP_LDC || 71c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka opcode == InstructionConstants.OP_LDC_W) 72c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka { 73c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka clazz.constantPoolEntryAccept(constantInstruction.constantIndex, 74c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka this); 75c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka } 76c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka } 77c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka 78c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka 79c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka // Implementations for ConstantVisitor. 80c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka 81c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka public void visitAnyConstant(Clazz clazz, Constant constant) {} 82c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka 83c345858bb96cd56e3cac717cfc11ef6e8630cd9dVitaly Buka 84 public void visitClassConstant(Clazz clazz, ClassConstant classConstant) 85 { 86 // Visit the referenced class from the .class construct. 87 classConstant.referencedClassAccept(classVisitor); 88 } 89} 90