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 */
15package javassist.bytecode.annotation;
16
17import javassist.ClassPool;
18import javassist.bytecode.ConstPool;
19import java.io.IOException;
20import java.lang.reflect.Method;
21
22/**
23 * Nested annotation.
24 *
25 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
26 * @author Shigeru Chiba
27 */
28public class AnnotationMemberValue extends MemberValue {
29    Annotation value;
30
31    /**
32     * Constructs an annotation member.  The initial value is not specified.
33     */
34    public AnnotationMemberValue(ConstPool cp) {
35        this(null, cp);
36    }
37
38    /**
39     * Constructs an annotation member.  The initial value is specified by
40     * the first parameter.
41     */
42    public AnnotationMemberValue(Annotation a, ConstPool cp) {
43        super('@', cp);
44        value = a;
45    }
46
47    Object getValue(ClassLoader cl, ClassPool cp, Method m)
48        throws ClassNotFoundException
49    {
50        return AnnotationImpl.make(cl, getType(cl), cp, value);
51    }
52
53    Class getType(ClassLoader cl) throws ClassNotFoundException {
54        if (value == null)
55            throw new ClassNotFoundException("no type specified");
56        else
57            return loadClass(cl, value.getTypeName());
58    }
59
60    /**
61     * Obtains the value.
62     */
63    public Annotation getValue() {
64        return value;
65    }
66
67    /**
68     * Sets the value of this member.
69     */
70    public void setValue(Annotation newValue) {
71        value = newValue;
72    }
73
74    /**
75     * Obtains the string representation of this object.
76     */
77    public String toString() {
78        return value.toString();
79    }
80
81    /**
82     * Writes the value.
83     */
84    public void write(AnnotationsWriter writer) throws IOException {
85        writer.annotationValue();
86        value.write(writer);
87    }
88
89    /**
90     * Accepts a visitor.
91     */
92    public void accept(MemberValueVisitor visitor) {
93        visitor.visitAnnotationMemberValue(this);
94    }
95}
96