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.classfile.constant;
22
23import proguard.classfile.*;
24import proguard.classfile.constant.visitor.*;
25import proguard.classfile.visitor.ClassVisitor;
26
27/**
28 * This Constant represents an invoke dynamic constant in the constant pool.
29 *
30 * @author Eric Lafortune
31 */
32public class InvokeDynamicConstant extends Constant
33{
34    public int u2bootstrapMethodAttributeIndex;
35    public int u2nameAndTypeIndex;
36
37    /**
38     * An extra field pointing to the Clazz objects referenced in the
39     * descriptor string. This field is filled out by the <code>{@link
40     * proguard.classfile.util.ClassReferenceInitializer ClassReferenceInitializer}</code>.
41     * References to primitive types are ignored.
42     */
43    public Clazz[] referencedClasses;
44
45
46    /**
47     * Creates an uninitialized InvokeDynamicConstant.
48     */
49    public InvokeDynamicConstant()
50    {
51    }
52
53
54    /**
55     * Creates a new InvokeDynamicConstant with the given bootstrap method
56     * and name-and-type indices.
57     * @param u2bootstrapMethodAttributeIndex the index of the bootstrap method
58     *                                        entry in the bootstrap methods
59     *                                        attribute.
60     * @param u2nameAndTypeIndex              the index of the name and type
61     *                                        entry in the constant pool.
62     * @param referencedClasses               the classes referenced by the
63     *                                        type.
64     */
65    public InvokeDynamicConstant(int     u2bootstrapMethodAttributeIndex,
66                                 int     u2nameAndTypeIndex,
67                                 Clazz[] referencedClasses)
68    {
69        this.u2bootstrapMethodAttributeIndex = u2bootstrapMethodAttributeIndex;
70        this.u2nameAndTypeIndex              = u2nameAndTypeIndex;
71        this.referencedClasses               = referencedClasses;
72    }
73
74
75    /**
76     * Returns the index of the bootstrap method in the bootstrap methods
77     * attribute of the class.
78     */
79    public int getBootstrapMethodAttributeIndex()
80    {
81        return u2bootstrapMethodAttributeIndex;
82    }
83
84    /**
85     * Returns the name-and-type index.
86     */
87    public int getNameAndTypeIndex()
88    {
89        return u2nameAndTypeIndex;
90    }
91
92    /**
93     * Returns the method name.
94     */
95    public String getName(Clazz clazz)
96    {
97        return clazz.getName(u2nameAndTypeIndex);
98    }
99
100    /**
101     * Returns the method type.
102     */
103    public String getType(Clazz clazz)
104    {
105        return clazz.getType(u2nameAndTypeIndex);
106    }
107
108
109    /**
110     * Lets the Clazz objects referenced in the descriptor string
111     * accept the given visitor.
112     */
113    public void referencedClassesAccept(ClassVisitor classVisitor)
114    {
115        if (referencedClasses != null)
116        {
117            for (int index = 0; index < referencedClasses.length; index++)
118            {
119                if (referencedClasses[index] != null)
120                {
121                    referencedClasses[index].accept(classVisitor);
122                }
123            }
124        }
125    }
126
127
128    /**
129     * Lets the bootstrap method handle constant accept the given visitor.
130     */
131    public void bootstrapMethodHandleAccept(Clazz clazz, ConstantVisitor constantVisitor)
132    {
133        new BootstrapMethodHandleTraveler(constantVisitor).visitInvokeDynamicConstant(clazz, this);
134    }
135
136
137    // Implementations for Constant.
138
139    public int getTag()
140    {
141        return ClassConstants.CONSTANT_InvokeDynamic;
142    }
143
144    public void accept(Clazz clazz, ConstantVisitor constantVisitor)
145    {
146        constantVisitor.visitInvokeDynamicConstant(clazz, this);
147    }
148}
149