169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/* 269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit. 369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 2004 Bill Burke. All Rights Reserved. 469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version 669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with 769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License. Alternatively, the contents of this file may be used under 869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later. 969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis, 1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the 1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License. 1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.bytecode.annotation; 1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.ClassPool; 1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.ConstPool; 2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.IOException; 2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.lang.reflect.Method; 2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/** 2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Integer constant value. 2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @author <a href="mailto:bill@jboss.org">Bill Burke</a> 2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @author Shigeru Chiba 2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class IntegerMemberValue extends MemberValue { 3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int valueIndex; 3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Constructs an int constant value. The initial value is specified 3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * by the constant pool entry at the given index. 3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param index the index of a CONSTANT_Integer_info structure. 3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public IntegerMemberValue(int index, ConstPool cp) { 3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal super('I', cp); 4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal this.valueIndex = index; 4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Constructs an int constant value. 4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Note that this constructor receives <b>the initial value 4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * as the second parameter</b> 4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * unlike the corresponding constructors in the sibling classes. 4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * This is for making a difference from the constructor that receives 4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * an index into the constant pool table as the first parameter. 5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Note that the index is also int type. 5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @param value the initial value. 5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public IntegerMemberValue(ConstPool cp, int value) { 5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal super('I', cp); 5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal setValue(value); 5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Constructs an int constant value. The initial value is 0. 6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public IntegerMemberValue(ConstPool cp) { 6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal super('I', cp); 6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal setValue(0); 6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal Object getValue(ClassLoader cl, ClassPool cp, Method m) { 6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return new Integer(getValue()); 6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal Class getType(ClassLoader cl) { 7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return int.class; 7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Obtains the value of the member. 7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public int getValue() { 7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return cp.getIntegerInfo(valueIndex); 8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Sets the value of the member. 8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public void setValue(int newValue) { 8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal valueIndex = cp.addIntegerInfo(newValue); 8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Obtains the string representation of this object. 9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public String toString() { 9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return Integer.toString(getValue()); 9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Writes the value. 9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public void write(AnnotationsWriter writer) throws IOException { 10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal writer.constValueIndex(getValue()); 10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /** 10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Accepts a visitor. 10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public void accept(MemberValueVisitor visitor) { 10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal visitor.visitIntegerMemberValue(this); 10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal} 110