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