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.ConstantVisitor;
25
26/**
27 * This Constant represents a method handle constant in the constant pool.
28 *
29 * @author Eric Lafortune
30 */
31public class MethodHandleConstant extends Constant
32{
33    public int u1referenceKind;
34    public int u2referenceIndex;
35
36
37    /**
38     * An extra field pointing to the java.lang.invoke.MethodHandle Clazz object.
39     * This field is typically filled out by the <code>{@link
40     * proguard.classfile.util.ClassReferenceInitializer
41     * ClassReferenceInitializer}</code>.
42     */
43    public Clazz javaLangInvokeMethodHandleClass;
44
45
46    /**
47     * Creates an uninitialized MethodHandleConstant.
48     */
49    public MethodHandleConstant()
50    {
51    }
52
53
54    /**
55     * Creates a new MethodHandleConstant with the given type and method ref
56     * index.
57     * @param u1referenceKind  the reference kind.
58     * @param u2referenceIndex the index of the field ref constant, interface
59     *                         method ref constant, or method ref constant in
60     *                         the constant pool.
61     */
62    public MethodHandleConstant(int u1referenceKind, int u2referenceIndex)
63    {
64        this.u1referenceKind  = u1referenceKind;
65        this.u2referenceIndex = u2referenceIndex;
66    }
67
68
69    /**
70     * Returns the kind of reference to which this constant is pointing.
71     * @return One of
72     *         {@link ClassConstants#REF_getField        },
73     *         {@link ClassConstants#REF_getStatic       },
74     *         {@link ClassConstants#REF_putField        },
75     *         {@link ClassConstants#REF_putStatic       },
76     *         {@link ClassConstants#REF_invokeVirtual   },
77     *         {@link ClassConstants#REF_invokeStatic    },
78     *         {@link ClassConstants#REF_invokeSpecial   },
79     *         {@link ClassConstants#REF_newInvokeSpecial}, or
80     *         {@link ClassConstants#REF_invokeInterface }.
81     */
82    public int getReferenceKind()
83    {
84        return u1referenceKind;
85    }
86
87    /**
88     * Returns the field ref, interface method ref, or method ref index.
89     */
90    public int getReferenceIndex()
91    {
92        return u2referenceIndex;
93    }
94
95
96    /**
97     * Returns the class name.
98     */
99    public String getClassName(Clazz clazz)
100    {
101        return clazz.getRefClassName(u2referenceIndex);
102    }
103
104    /**
105     * Returns the method/field name.
106     */
107    public String getName(Clazz clazz)
108    {
109        return clazz.getRefName(u2referenceIndex);
110    }
111
112    /**
113     * Returns the type.
114     */
115    public String getType(Clazz clazz)
116    {
117        return clazz.getRefType(u2referenceIndex);
118    }
119
120
121    // Implementations for Constant.
122
123    public int getTag()
124    {
125        return ClassConstants.CONSTANT_MethodHandle;
126    }
127
128    public void accept(Clazz clazz, ConstantVisitor constantVisitor)
129    {
130        constantVisitor.visitMethodHandleConstant(clazz, this);
131    }
132}
133