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.visitor.BootstrapMethodInfoVisitor;
26 
27 /**
28  * This BootstrapMethodInfoVisitor adds all bootstrap methods that it visits to
29  * the given target bootstrap methods attribute.
30  */
31 public class BootstrapMethodInfoAdder
32 implements   BootstrapMethodInfoVisitor
33 {
34     private final ConstantAdder                   constantAdder;
35     private final BootstrapMethodsAttributeEditor bootstrapMethodsAttributeEditor;
36 
37     private int bootstrapMethodIndex;
38 
39 
40     /**
41      * Creates a new BootstrapMethodInfoAdder that will copy bootstrap methods
42      * into the given bootstrap methods attribute.
43      */
BootstrapMethodInfoAdder(ProgramClass targetClass, BootstrapMethodsAttribute targetBootstrapMethodsAttribute)44     public BootstrapMethodInfoAdder(ProgramClass              targetClass,
45                                     BootstrapMethodsAttribute targetBootstrapMethodsAttribute)
46     {
47         this.constantAdder                   = new ConstantAdder(targetClass);
48         this.bootstrapMethodsAttributeEditor = new BootstrapMethodsAttributeEditor(targetBootstrapMethodsAttribute);
49     }
50 
51 
52     /**
53      * Returns the index of the most recently added bootstrap method.
54      */
getBootstrapMethodIndex()55     public int getBootstrapMethodIndex()
56     {
57         return bootstrapMethodIndex;
58     }
59 
60 
61     // Implementations for BootstrapMethodInfoVisitor.
62 
visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo)63     public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo)
64     {
65         // Copy the method arguments.
66         int   methodArgumentCount = bootstrapMethodInfo.u2methodArgumentCount;
67         int[] methodArguments     = bootstrapMethodInfo.u2methodArguments;
68         int[] newMethodArguments  = new int[methodArgumentCount];
69 
70         for (int index = 0; index < methodArgumentCount; index++)
71         {
72             newMethodArguments[index] =
73                 constantAdder.addConstant(clazz, methodArguments[index]);
74         }
75 
76         // Create a new bootstrap method.
77         BootstrapMethodInfo newBootstrapMethodInfo =
78             new BootstrapMethodInfo(constantAdder.addConstant(clazz, bootstrapMethodInfo.u2methodHandleIndex),
79                                     methodArgumentCount,
80                                     newMethodArguments);
81 
82         // Add it to the target.
83         bootstrapMethodIndex =
84             bootstrapMethodsAttributeEditor.addBootstrapMethodInfo(newBootstrapMethodInfo);
85     }
86 }