001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 2004 Bill Burke. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist.bytecode.annotation;
017:
018: import javassist.ClassPool;
019: import javassist.bytecode.ConstPool;
020: import java.io.IOException;
021: import java.lang.reflect.Method;
022:
023: /**
024: * Integer constant value.
025: *
026: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
027: * @author Shigeru Chiba
028: */
029: public class IntegerMemberValue extends MemberValue {
030: int valueIndex;
031:
032: /**
033: * Constructs an int constant value. The initial value is specified
034: * by the constant pool entry at the given index.
035: *
036: * @param index the index of a CONSTANT_Integer_info structure.
037: */
038: public IntegerMemberValue(int index, ConstPool cp) {
039: super ('I', cp);
040: this .valueIndex = index;
041: }
042:
043: /**
044: * Constructs an int constant value.
045: * Note that this constructor receives <b>the initial value
046: * as the second parameter</b>
047: * unlike the corresponding constructors in the sibling classes.
048: * This is for making a difference from the constructor that receives
049: * an index into the constant pool table as the first parameter.
050: * Note that the index is also int type.
051: *
052: * @param value the initial value.
053: */
054: public IntegerMemberValue(ConstPool cp, int value) {
055: super ('I', cp);
056: setValue(value);
057: }
058:
059: /**
060: * Constructs an int constant value. The initial value is 0.
061: */
062: public IntegerMemberValue(ConstPool cp) {
063: super ('I', cp);
064: setValue(0);
065: }
066:
067: Object getValue(ClassLoader cl, ClassPool cp, Method m) {
068: return new Integer(getValue());
069: }
070:
071: Class getType(ClassLoader cl) {
072: return int.class;
073: }
074:
075: /**
076: * Obtains the value of the member.
077: */
078: public int getValue() {
079: return cp.getIntegerInfo(valueIndex);
080: }
081:
082: /**
083: * Sets the value of the member.
084: */
085: public void setValue(int newValue) {
086: valueIndex = cp.addIntegerInfo(newValue);
087: }
088:
089: /**
090: * Obtains the string representation of this object.
091: */
092: public String toString() {
093: return Integer.toString(getValue());
094: }
095:
096: /**
097: * Writes the value.
098: */
099: public void write(AnnotationsWriter writer) throws IOException {
100: writer.constValueIndex(getValue());
101: }
102:
103: /**
104: * Accepts a visitor.
105: */
106: public void accept(MemberValueVisitor visitor) {
107: visitor.visitIntegerMemberValue(this);
108: }
109: }
|