Home | History | Annotate | Download | only in annotation
      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 
     16 package javassist.bytecode.annotation;
     17 
     18 import javassist.ClassPool;
     19 import javassist.bytecode.ConstPool;
     20 import java.io.IOException;
     21 import java.lang.reflect.Method;
     22 
     23 /**
     24  * Integer constant value.
     25  *
     26  * @author <a href="mailto:bill (at) jboss.org">Bill Burke</a>
     27  * @author Shigeru Chiba
     28  */
     29 public class IntegerMemberValue extends MemberValue {
     30     int valueIndex;
     31 
     32     /**
     33      * Constructs an int 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 IntegerMemberValue(int index, ConstPool cp) {
     39         super('I', cp);
     40         this.valueIndex = index;
     41     }
     42 
     43     /**
     44      * Constructs an int constant value.
     45      * Note that this constructor receives <b>the initial value
     46      * as the second parameter</b>
     47      * unlike the corresponding constructors in the sibling classes.
     48      * This is for making a difference from the constructor that receives
     49      * an index into the constant pool table as the first parameter.
     50      * Note that the index is also int type.
     51      *
     52      * @param value         the initial value.
     53      */
     54     public IntegerMemberValue(ConstPool cp, int value) {
     55         super('I', cp);
     56         setValue(value);
     57     }
     58 
     59     /**
     60      * Constructs an int constant value.  The initial value is 0.
     61      */
     62     public IntegerMemberValue(ConstPool cp) {
     63         super('I', cp);
     64         setValue(0);
     65     }
     66 
     67     Object getValue(ClassLoader cl, ClassPool cp, Method m) {
     68         return new Integer(getValue());
     69     }
     70 
     71     Class getType(ClassLoader cl) {
     72         return int.class;
     73     }
     74 
     75     /**
     76      * Obtains the value of the member.
     77      */
     78     public int getValue() {
     79         return cp.getIntegerInfo(valueIndex);
     80     }
     81 
     82     /**
     83      * Sets the value of the member.
     84      */
     85     public void setValue(int newValue) {
     86         valueIndex = cp.addIntegerInfo(newValue);
     87     }
     88 
     89     /**
     90      * Obtains the string representation of this object.
     91      */
     92     public String toString() {
     93         return Integer.toString(getValue());
     94     }
     95 
     96     /**
     97      * Writes the value.
     98      */
     99     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 }
    110