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: package javassist.bytecode.annotation;
016:
017: import javassist.ClassPool;
018: import javassist.bytecode.ConstPool;
019: import java.io.IOException;
020: import java.lang.reflect.Method;
021:
022: /**
023: * Boolean constant value.
024: *
025: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
026: * @author Shigeru Chiba
027: */
028: public class BooleanMemberValue extends MemberValue {
029: int valueIndex;
030:
031: /**
032: * Constructs a boolean constant value. The initial value is specified
033: * by the constant pool entry at the given index.
034: *
035: * @param index the index of a CONSTANT_Integer_info structure.
036: */
037: public BooleanMemberValue(int index, ConstPool cp) {
038: super ('Z', cp);
039: this .valueIndex = index;
040: }
041:
042: /**
043: * Constructs a boolean constant value.
044: *
045: * @param b the initial value.
046: */
047: public BooleanMemberValue(boolean b, ConstPool cp) {
048: super ('Z', cp);
049: setValue(b);
050: }
051:
052: /**
053: * Constructs a boolean constant value. The initial value is false.
054: */
055: public BooleanMemberValue(ConstPool cp) {
056: super ('Z', cp);
057: setValue(false);
058: }
059:
060: Object getValue(ClassLoader cl, ClassPool cp, Method m) {
061: return new Boolean(getValue());
062: }
063:
064: Class getType(ClassLoader cl) {
065: return boolean.class;
066: }
067:
068: /**
069: * Obtains the value of the member.
070: */
071: public boolean getValue() {
072: return cp.getIntegerInfo(valueIndex) != 0;
073: }
074:
075: /**
076: * Sets the value of the member.
077: */
078: public void setValue(boolean newValue) {
079: valueIndex = cp.addIntegerInfo(newValue ? 1 : 0);
080: }
081:
082: /**
083: * Obtains the string representation of this object.
084: */
085: public String toString() {
086: return getValue() ? "true" : "false";
087: }
088:
089: /**
090: * Writes the value.
091: */
092: public void write(AnnotationsWriter writer) throws IOException {
093: writer.constValueIndex(getValue());
094: }
095:
096: /**
097: * Accepts a visitor.
098: */
099: public void accept(MemberValueVisitor visitor) {
100: visitor.visitBooleanMemberValue(this);
101: }
102: }
|