1/*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 2004 Bill Burke. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License.  Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16package javassist.bytecode.annotation;
17
18import java.io.IOException;
19import java.lang.reflect.Method;
20
21import javassist.ClassPool;
22import javassist.bytecode.ConstPool;
23import javassist.bytecode.Descriptor;
24
25/**
26 * Enum constant value.
27 *
28 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
29 * @author Shigeru Chiba
30 */
31public class EnumMemberValue extends MemberValue {
32    int typeIndex, valueIndex;
33
34    /**
35     * Constructs an enum constant value.  The initial value is specified
36     * by the constant pool entries at the given indexes.
37     *
38     * @param type      the index of a CONSTANT_Utf8_info structure
39     *                  representing the enum type.
40     * @param value     the index of a CONSTANT_Utf8_info structure.
41     *                  representing the enum value.
42     */
43    public EnumMemberValue(int type, int value, ConstPool cp) {
44        super('e', cp);
45        this.typeIndex = type;
46        this.valueIndex = value;
47    }
48
49    /**
50     * Constructs an enum constant value.
51     * The initial value is not specified.
52     */
53    public EnumMemberValue(ConstPool cp) {
54        super('e', cp);
55        typeIndex = valueIndex = 0;
56    }
57
58    Object getValue(ClassLoader cl, ClassPool cp, Method m)
59        throws ClassNotFoundException
60    {
61        try {
62            return getType(cl).getField(getValue()).get(null);
63        }
64        catch (NoSuchFieldException e) {
65            throw new ClassNotFoundException(getType() + "." + getValue());
66        }
67        catch (IllegalAccessException e) {
68            throw new ClassNotFoundException(getType() + "." + getValue());
69        }
70    }
71
72    Class getType(ClassLoader cl) throws ClassNotFoundException {
73        return loadClass(cl, getType());
74    }
75
76    /**
77     * Obtains the enum type name.
78     *
79     * @return a fully-qualified type name.
80     */
81    public String getType() {
82        return Descriptor.toClassName(cp.getUtf8Info(typeIndex));
83    }
84
85    /**
86     * Changes the enum type name.
87     *
88     * @param typename a fully-qualified type name.
89     */
90    public void setType(String typename) {
91        typeIndex = cp.addUtf8Info(Descriptor.of(typename));
92    }
93
94    /**
95     * Obtains the name of the enum constant value.
96     */
97    public String getValue() {
98        return cp.getUtf8Info(valueIndex);
99    }
100
101    /**
102     * Changes the name of the enum constant value.
103     */
104    public void setValue(String name) {
105        valueIndex = cp.addUtf8Info(name);
106    }
107
108    public String toString() {
109        return getType() + "." + getValue();
110    }
111
112    /**
113     * Writes the value.
114     */
115    public void write(AnnotationsWriter writer) throws IOException {
116        writer.enumConstValue(cp.getUtf8Info(typeIndex), getValue());
117    }
118
119    /**
120     * Accepts a visitor.
121     */
122    public void accept(MemberValueVisitor visitor) {
123        visitor.visitEnumMemberValue(this);
124    }
125}
126