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 * or the Apache License Version 2.0. 10 * 11 * Software distributed under the License is distributed on an "AS IS" basis, 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 * for the specific language governing rights and limitations under the 14 * License. 15 */ 16 package javassist.bytecode.annotation; 17 18 import java.io.IOException; 19 import java.lang.reflect.Method; 20 21 import javassist.ClassPool; 22 import javassist.bytecode.ConstPool; 23 24 /** 25 * Boolean constant value. 26 * 27 * @author <a href="mailto:bill@jboss.org">Bill Burke</a> 28 * @author Shigeru Chiba 29 */ 30 public class BooleanMemberValue extends MemberValue { 31 int valueIndex; 32 33 /** 34 * Constructs a boolean constant value. The initial value is specified 35 * by the constant pool entry at the given index. 36 * 37 * @param index the index of a CONSTANT_Integer_info structure. 38 */ BooleanMemberValue(int index, ConstPool cp)39 public BooleanMemberValue(int index, ConstPool cp) { 40 super('Z', cp); 41 this.valueIndex = index; 42 } 43 44 /** 45 * Constructs a boolean constant value. 46 * 47 * @param b the initial value. 48 */ BooleanMemberValue(boolean b, ConstPool cp)49 public BooleanMemberValue(boolean b, ConstPool cp) { 50 super('Z', cp); 51 setValue(b); 52 } 53 54 /** 55 * Constructs a boolean constant value. The initial value is false. 56 */ BooleanMemberValue(ConstPool cp)57 public BooleanMemberValue(ConstPool cp) { 58 super('Z', cp); 59 setValue(false); 60 } 61 62 @Override getValue(ClassLoader cl, ClassPool cp, Method m)63 Object getValue(ClassLoader cl, ClassPool cp, Method m) { 64 return Boolean.valueOf(getValue()); 65 } 66 67 @Override getType(ClassLoader cl)68 Class<?> getType(ClassLoader cl) { 69 return boolean.class; 70 } 71 72 /** 73 * Obtains the value of the member. 74 */ getValue()75 public boolean getValue() { 76 return cp.getIntegerInfo(valueIndex) != 0; 77 } 78 79 /** 80 * Sets the value of the member. 81 */ setValue(boolean newValue)82 public void setValue(boolean newValue) { 83 valueIndex = cp.addIntegerInfo(newValue ? 1 : 0); 84 } 85 86 /** 87 * Obtains the string representation of this object. 88 */ 89 @Override toString()90 public String toString() { 91 return getValue() ? "true" : "false"; 92 } 93 94 /** 95 * Writes the value. 96 */ 97 @Override write(AnnotationsWriter writer)98 public void write(AnnotationsWriter writer) throws IOException { 99 writer.constValueIndex(getValue()); 100 } 101 102 /** 103 * Accepts a visitor. 104 */ 105 @Override accept(MemberValueVisitor visitor)106 public void accept(MemberValueVisitor visitor) { 107 visitor.visitBooleanMemberValue(this); 108 } 109 } 110