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 package javassist.bytecode.annotation;
     16 
     17 import javassist.ClassPool;
     18 import javassist.bytecode.ConstPool;
     19 import java.io.IOException;
     20 import java.lang.reflect.Method;
     21 
     22 /**
     23  * Boolean constant value.
     24  *
     25  * @author <a href="mailto:bill (at) jboss.org">Bill Burke</a>
     26  * @author Shigeru Chiba
     27  */
     28 public class BooleanMemberValue extends MemberValue {
     29     int valueIndex;
     30 
     31     /**
     32      * Constructs a boolean 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 BooleanMemberValue(int index, ConstPool cp) {
     38         super('Z', cp);
     39         this.valueIndex = index;
     40     }
     41 
     42     /**
     43      * Constructs a boolean constant value.
     44      *
     45      * @param b         the initial value.
     46      */
     47     public BooleanMemberValue(boolean b, ConstPool cp) {
     48         super('Z', cp);
     49         setValue(b);
     50     }
     51 
     52     /**
     53      * Constructs a boolean constant value.  The initial value is false.
     54      */
     55     public BooleanMemberValue(ConstPool cp) {
     56         super('Z', cp);
     57         setValue(false);
     58     }
     59 
     60     Object getValue(ClassLoader cl, ClassPool cp, Method m) {
     61         return new Boolean(getValue());
     62     }
     63 
     64     Class getType(ClassLoader cl) {
     65         return boolean.class;
     66     }
     67 
     68     /**
     69      * Obtains the value of the member.
     70      */
     71     public boolean getValue() {
     72         return cp.getIntegerInfo(valueIndex) != 0;
     73     }
     74 
     75     /**
     76      * Sets the value of the member.
     77      */
     78     public void setValue(boolean newValue) {
     79         valueIndex = cp.addIntegerInfo(newValue ? 1 : 0);
     80     }
     81 
     82     /**
     83      * Obtains the string representation of this object.
     84      */
     85     public String toString() {
     86         return getValue() ? "true" : "false";
     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.visitBooleanMemberValue(this);
    101     }
    102 }
    103