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 */
21package proguard.optimize;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.BootstrapMethodInfo;
25import proguard.classfile.attribute.visitor.BootstrapMethodInfoVisitor;
26import proguard.classfile.constant.*;
27import proguard.classfile.constant.visitor.ConstantVisitor;
28import proguard.classfile.util.SimplifiedVisitor;
29import proguard.classfile.visitor.MemberVisitor;
30import proguard.optimize.info.*;
31import proguard.optimize.peephole.VariableShrinker;
32
33/**
34 * This BootstrapMethodInfoVisitor removes unused constant arguments from
35 * bootstrap method entries that it visits.
36 *
37 * @see ParameterUsageMarker
38 * @see VariableUsageMarker
39 * @see VariableShrinker
40 * @author Eric Lafortune
41 */
42public class BootstrapMethodArgumentShrinker
43extends      SimplifiedVisitor
44implements   BootstrapMethodInfoVisitor,
45             ConstantVisitor,
46             MemberVisitor
47{
48    private long usedParameters;
49
50
51    // Implementations for BootstrapMethodInfoVisitor.
52
53    public void visitBootstrapMethodInfo(Clazz clazz, BootstrapMethodInfo bootstrapMethodInfo)
54    {
55        // Check which method parameters are used.
56        usedParameters = -1L;
57        clazz.constantPoolEntryAccept(bootstrapMethodInfo.u2methodHandleIndex, this);
58
59        // Remove the unused arguments.
60        int   methodArgumentCount = bootstrapMethodInfo.u2methodArgumentCount;
61        int[] methodArguments     = bootstrapMethodInfo.u2methodArguments;
62
63        int newArgumentIndex = 0;
64
65        for (int argumentIndex = 0; argumentIndex < methodArgumentCount; argumentIndex++)
66        {
67            if (argumentIndex >= 64 ||
68                (usedParameters & (1L << argumentIndex)) != 0L)
69            {
70                methodArguments[newArgumentIndex++] = methodArguments[argumentIndex];
71            }
72        }
73
74        // Update the number of arguments.
75        bootstrapMethodInfo.u2methodArgumentCount = newArgumentIndex;
76    }
77
78
79    // Implementations for ConstantVisitor.
80
81    public void visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant)
82    {
83        // Check the referenced bootstrap method.
84        clazz.constantPoolEntryAccept(methodHandleConstant.u2referenceIndex, this);
85    }
86
87
88    public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant)
89    {
90        // Check the referenced class member itself.
91        refConstant.referencedMemberAccept(this);
92    }
93
94
95    // Implementations for MemberVisitor.
96
97    public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod) {}
98
99
100    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
101    {
102        usedParameters = ParameterUsageMarker.getUsedParameters(programMethod);
103    }
104}
105