1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 Eric Lafortune (eric@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.editor;
22 
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.*;
25 import proguard.classfile.attribute.annotation.*;
26 import proguard.classfile.attribute.annotation.visitor.*;
27 import proguard.classfile.attribute.preverification.*;
28 import proguard.classfile.attribute.preverification.visitor.*;
29 import proguard.classfile.attribute.visitor.*;
30 import proguard.classfile.constant.*;
31 import proguard.classfile.constant.visitor.ConstantVisitor;
32 import proguard.classfile.instruction.*;
33 import proguard.classfile.instruction.visitor.InstructionVisitor;
34 import proguard.classfile.util.SimplifiedVisitor;
35 import proguard.classfile.visitor.*;
36 
37 /**
38  * This ConstantVisitor remaps all possible indices of bootstrap methods
39  * of the constants that it visits, based on a given index map.
40  *
41  * @author Eric Lafortune
42  */
43 public class BootstrapMethodRemapper
44 extends      SimplifiedVisitor
45 implements   ConstantVisitor
46 {
47     private int[] constantIndexMap;
48 
49 
50     /**
51      * Sets the given mapping of old constant pool entry indexes to their new
52      * indexes.
53      */
setConstantIndexMap(int[] constantIndexMap)54     public void setConstantIndexMap(int[] constantIndexMap)
55     {
56         this.constantIndexMap = constantIndexMap;
57     }
58 
59 
60     // Implementations for ConstantVisitor.
61 
visitAnyConstant(Clazz clazz, Constant constant)62     public void visitAnyConstant(Clazz clazz, Constant constant) {}
63 
64 
visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant)65     public void visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant)
66     {
67         invokeDynamicConstant.u2bootstrapMethodAttributeIndex =
68             remapConstantIndex(invokeDynamicConstant.u2bootstrapMethodAttributeIndex);
69     }
70 
71 
72     // Small utility methods.
73 
74     /**
75      * Returns the new bootstrap method index of the entry at the
76      * given index.
77      */
remapConstantIndex(int constantIndex)78     private int remapConstantIndex(int constantIndex)
79     {
80         int remappedConstantIndex = constantIndexMap[constantIndex];
81         if (remappedConstantIndex < 0)
82         {
83             throw new IllegalArgumentException("Can't remap constant index ["+constantIndex+"]");
84         }
85 
86         return remappedConstantIndex;
87     }
88 }
89