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 javassist.ClassPool;
19import javassist.bytecode.ConstPool;
20import javassist.bytecode.Descriptor;
21import java.io.IOException;
22import java.lang.reflect.Method;
23
24/**
25 * Class value.
26 *
27 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
28 * @author Shigeru Chiba
29 */
30public class ClassMemberValue extends MemberValue {
31    int valueIndex;
32
33    /**
34     * Constructs a class value.  The initial value is specified
35     * by the constant pool entry at the given index.
36     *
37     * @param index the index of a CONSTANT_Utf8_info structure.
38     */
39    public ClassMemberValue(int index, ConstPool cp) {
40        super('c', cp);
41        this.valueIndex = index;
42    }
43
44    /**
45     * Constructs a class value.
46     *
47     * @param className         the initial value.
48     */
49    public ClassMemberValue(String className, ConstPool cp) {
50        super('c', cp);
51        setValue(className);
52    }
53
54    /**
55     * Constructs a class value.
56     * The initial value is java.lang.Class.
57     */
58    public ClassMemberValue(ConstPool cp) {
59        super('c', cp);
60        setValue("java.lang.Class");
61    }
62
63    Object getValue(ClassLoader cl, ClassPool cp, Method m)
64            throws ClassNotFoundException {
65        final String classname = getValue();
66        if (classname.equals("void"))
67            return void.class;
68        else if (classname.equals("int"))
69            return int.class;
70        else if (classname.equals("byte"))
71            return byte.class;
72        else if (classname.equals("long"))
73            return long.class;
74        else if (classname.equals("double"))
75            return double.class;
76        else if (classname.equals("float"))
77            return float.class;
78        else if (classname.equals("char"))
79            return char.class;
80        else if (classname.equals("short"))
81            return short.class;
82        else if (classname.equals("boolean"))
83            return boolean.class;
84        else
85            return loadClass(cl, classname);
86    }
87
88    Class getType(ClassLoader cl) throws ClassNotFoundException {
89        return loadClass(cl, "java.lang.Class");
90    }
91
92    /**
93     * Obtains the value of the member.
94     *
95     * @return fully-qualified class name.
96     */
97    public String getValue() {
98        String v = cp.getUtf8Info(valueIndex);
99        return Descriptor.toClassName(v);
100    }
101
102    /**
103     * Sets the value of the member.
104     *
105     * @param newClassName      fully-qualified class name.
106     */
107    public void setValue(String newClassName) {
108        String setTo = Descriptor.of(newClassName);
109        valueIndex = cp.addUtf8Info(setTo);
110    }
111
112    /**
113     * Obtains the string representation of this object.
114     */
115    public String toString() {
116        return "<" + getValue() + " class>";
117    }
118
119    /**
120     * Writes the value.
121     */
122    public void write(AnnotationsWriter writer) throws IOException {
123        writer.classInfoIndex(cp.getUtf8Info(valueIndex));
124    }
125
126    /**
127     * Accepts a visitor.
128     */
129    public void accept(MemberValueVisitor visitor) {
130        visitor.visitClassMemberValue(this);
131    }
132}
133