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 * Byte constant value.
24 *
25 * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
26 * @author Shigeru Chiba
27 */
28public class ByteMemberValue extends MemberValue {
29    int valueIndex;
30
31    /**
32     * Constructs a byte constant value.  The initial value is specified
33     * by the constant pool entry at the given index.
34     *
35     * @param index     the index of a CONSTANT_Integer_info structure.
36     */
37    public ByteMemberValue(int index, ConstPool cp) {
38        super('B', cp);
39        this.valueIndex = index;
40    }
41
42    /**
43     * Constructs a byte constant value.
44     *
45     * @param b         the initial value.
46     */
47    public ByteMemberValue(byte b, ConstPool cp) {
48        super('B', cp);
49        setValue(b);
50    }
51
52    /**
53     * Constructs a byte constant value.  The initial value is 0.
54     */
55    public ByteMemberValue(ConstPool cp) {
56        super('B', cp);
57        setValue((byte)0);
58    }
59
60    Object getValue(ClassLoader cl, ClassPool cp, Method m) {
61        return new Byte(getValue());
62    }
63
64    Class getType(ClassLoader cl) {
65        return byte.class;
66    }
67
68    /**
69     * Obtains the value of the member.
70     */
71    public byte getValue() {
72        return (byte)cp.getIntegerInfo(valueIndex);
73    }
74
75    /**
76     * Sets the value of the member.
77     */
78    public void setValue(byte newValue) {
79        valueIndex = cp.addIntegerInfo(newValue);
80    }
81
82    /**
83     * Obtains the string representation of this object.
84     */
85    public String toString() {
86        return Byte.toString(getValue());
87    }
88
89    /**
90     * Writes the value.
91     */
92    public void write(AnnotationsWriter writer) throws IOException {
93        writer.constValueIndex(getValue());
94    }
95
96    /**
97     * Accepts a visitor.
98     */
99    public void accept(MemberValueVisitor visitor) {
100        visitor.visitByteMemberValue(this);
101    }
102}
103