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;
21
22import java.io.IOException;
23import java.lang.reflect.Array;
24import java.lang.reflect.Method;
25
26/**
27 * The value of a member declared in an annotation.
28 *
29 * @see Annotation#getMemberValue(String)
30 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
31 * @author Shigeru Chiba
32 */
33public abstract class MemberValue {
34    ConstPool cp;
35    char tag;
36
37    MemberValue(char tag, ConstPool cp) {
38        this.cp = cp;
39        this.tag = tag;
40    }
41
42    /**
43     * Returns the value.  If the value type is a primitive type, the
44     * returned value is boxed.
45     */
46    abstract Object getValue(ClassLoader cl, ClassPool cp, Method m)
47        throws ClassNotFoundException;
48
49    abstract Class getType(ClassLoader cl) throws ClassNotFoundException;
50
51    static Class loadClass(ClassLoader cl, String classname)
52        throws ClassNotFoundException, NoSuchClassError
53    {
54        try {
55            return Class.forName(convertFromArray(classname), true, cl);
56        }
57        catch (LinkageError e) {
58            throw new NoSuchClassError(classname, e);
59        }
60    }
61
62    private static String convertFromArray(String classname)
63    {
64        int index = classname.indexOf("[]");
65        if (index != -1) {
66            String rawType = classname.substring(0, index);
67            StringBuffer sb = new StringBuffer(Descriptor.of(rawType));
68            while (index != -1) {
69                sb.insert(0, "[");
70                index = classname.indexOf("[]", index + 1);
71            }
72            return sb.toString().replace('/', '.');
73        }
74        return classname;
75    }
76
77    /**
78     * Accepts a visitor.
79     */
80    public abstract void accept(MemberValueVisitor visitor);
81
82    /**
83     * Writes the value.
84     */
85    public abstract void write(AnnotationsWriter w) throws IOException;
86}
87
88
89