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.attribute.annotation;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.annotation.visitor.ElementValueVisitor;
25import proguard.classfile.visitor.*;
26
27/**
28 * This ElementValue represents an enumeration constant element value.
29 *
30 * @author Eric Lafortune
31 */
32public class EnumConstantElementValue extends ElementValue
33{
34    public int u2typeNameIndex;
35    public int u2constantNameIndex;
36
37    /**
38     * An extra field pointing to the Clazz objects referenced in the
39     * type name string. This field is typically filled out by the <code>{@link
40     * proguard.classfile.util.ClassReferenceInitializer
41     * ClassReferenceInitializer}</code>.
42     * References to primitive types are ignored.
43     */
44    public Clazz[] referencedClasses;
45
46    /**
47     * An extra field optionally pointing to the referenced enum Field object.
48     * This field is typically filled out by the <code>{@link
49     * proguard.classfile.util.ClassReferenceInitializer
50     * ClassReferenceInitializer}</code>.
51     */
52    public Field referencedField;
53
54
55    /**
56     * Creates an uninitialized EnumConstantElementValue.
57     */
58    public EnumConstantElementValue()
59    {
60    }
61
62
63    /**
64     * Creates an initialized EnumConstantElementValue.
65     */
66    public EnumConstantElementValue(int u2elementNameIndex,
67                                    int u2typeNameIndex,
68                                    int u2constantNameIndex)
69    {
70        super(u2elementNameIndex);
71
72        this.u2typeNameIndex     = u2typeNameIndex;
73        this.u2constantNameIndex = u2constantNameIndex;
74    }
75
76
77    /**
78     * Returns the enumeration type name.
79     */
80    public String getTypeName(Clazz clazz)
81    {
82        return clazz.getString(u2typeNameIndex);
83    }
84
85
86    /**
87     * Returns the constant name.
88     */
89    public String getConstantName(Clazz clazz)
90    {
91        return clazz.getString(u2constantNameIndex);
92    }
93
94
95    /**
96     * Applies the given visitor to all referenced classes.
97     */
98    public void referencedClassesAccept(ClassVisitor classVisitor)
99    {
100        if (referencedClasses != null)
101        {
102            for (int index = 0; index < referencedClasses.length; index++)
103            {
104                Clazz referencedClass = referencedClasses[index];
105                if (referencedClass != null)
106                {
107                    referencedClass.accept(classVisitor);
108                }
109            }
110        }
111    }
112
113
114    /**
115     * Applies the given visitor to the referenced field.
116     */
117    public void referencedFieldAccept(MemberVisitor memberVisitor)
118    {
119        if (referencedField != null)
120        {
121            referencedField.accept(referencedClasses[0],
122                                   memberVisitor);
123        }
124    }
125
126
127    // Implementations for ElementValue.
128
129    public char getTag()
130    {
131        return ClassConstants.ELEMENT_VALUE_ENUM_CONSTANT;
132    }
133
134    public void accept(Clazz clazz, Annotation annotation, ElementValueVisitor elementValueVisitor)
135    {
136        elementValueVisitor.visitEnumConstantElementValue(clazz, annotation, this);
137    }
138}
139