1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2014 Eric Lafortune (eric (at) 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 */ 21 package proguard.classfile.constant.visitor; 22 23 import proguard.classfile.Clazz; 24 import proguard.classfile.attribute.*; 25 import proguard.classfile.attribute.visitor.*; 26 import proguard.classfile.constant.*; 27 import proguard.classfile.util.SimplifiedVisitor; 28 29 /** 30 * This ConstantVisitor and BootstrapMethodInfoVisitor travels from any invoke 31 * dynamic constants or bootstrap method info entries that it visits to their 32 * bootstrap method handle constants, and applies a given constant visitor. 33 * 34 * @author Eric Lafortune 35 */ 36 public class BootstrapMethodHandleTraveler 37 extends SimplifiedVisitor 38 implements ConstantVisitor, 39 AttributeVisitor, 40 BootstrapMethodInfoVisitor 41 { 42 private ConstantVisitor bootstrapMethodHandleVisitor; 43 44 // Field serving as a method argument. 45 int bootstrapMethodAttributeIndex; 46 47 48 /** 49 * Creates a new BootstrapMethodHandleVisitor that will delegate to the 50 * given constant visitor. 51 */ 52 public BootstrapMethodHandleTraveler(ConstantVisitor bootstrapMethodHandleVisitor) 53 { 54 this.bootstrapMethodHandleVisitor = bootstrapMethodHandleVisitor; 55 } 56 57 58 // Implementations for ConstantVisitor. 59 60 public void visitAnyConstant(Clazz clazz, Constant constant) {} 61 62 63 public void visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant) 64 { 65 // Pass the method index. 66 bootstrapMethodAttributeIndex = 67 invokeDynamicConstant.u2bootstrapMethodAttributeIndex; 68 69 // Delegate to the bootstrap method. 70 clazz.attributesAccept(this); 71 } 72 73 74 // Implementations for AttributeVisitor. 75 76 public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} 77 78 79 public void visitBootstrapMethodsAttribute(Clazz clazz, BootstrapMethodsAttribute bootstrapMethodsAttribute) 80 { 81 // Check bootstrap methods. 82 bootstrapMethodsAttribute.bootstrapMethodEntryAccept(clazz, 83 bootstrapMethodAttributeIndex, 84 this); 85 } 86 87 88 // Implementations for BootstrapMethodInfoVisitor. 89 90 public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo) 91 { 92 // Check bootstrap method. 93 clazz.constantPoolEntryAccept(bootstrapMethodInfo.u2methodHandleIndex, 94 bootstrapMethodHandleVisitor); 95 } 96 } 97