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 * Char constant value. 25 * 26 * @author <a href="mailto:bill@jboss.org">Bill Burke</a> 27 * @author Shigeru Chiba 28 */ 29 public class CharMemberValue extends MemberValue { 30 int valueIndex; 31 32 /** 33 * Constructs a char 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 */ CharMemberValue(int index, ConstPool cp)38 public CharMemberValue(int index, ConstPool cp) { 39 super('C', cp); 40 this.valueIndex = index; 41 } 42 43 /** 44 * Constructs a char constant value. 45 * 46 * @param c the initial value. 47 */ CharMemberValue(char c, ConstPool cp)48 public CharMemberValue(char c, ConstPool cp) { 49 super('C', cp); 50 setValue(c); 51 } 52 53 /** 54 * Constructs a char constant value. The initial value is '\0'. 55 */ CharMemberValue(ConstPool cp)56 public CharMemberValue(ConstPool cp) { 57 super('C', cp); 58 setValue('\0'); 59 } 60 getValue(ClassLoader cl, ClassPool cp, Method m)61 Object getValue(ClassLoader cl, ClassPool cp, Method m) { 62 return new Character(getValue()); 63 } 64 getType(ClassLoader cl)65 Class getType(ClassLoader cl) { 66 return char.class; 67 } 68 69 /** 70 * Obtains the value of the member. 71 */ getValue()72 public char getValue() { 73 return (char)cp.getIntegerInfo(valueIndex); 74 } 75 76 /** 77 * Sets the value of the member. 78 */ setValue(char newValue)79 public void setValue(char newValue) { 80 valueIndex = cp.addIntegerInfo(newValue); 81 } 82 83 /** 84 * Obtains the string representation of this object. 85 */ toString()86 public String toString() { 87 return Character.toString(getValue()); 88 } 89 90 /** 91 * Writes the value. 92 */ write(AnnotationsWriter writer)93 public void write(AnnotationsWriter writer) throws IOException { 94 writer.constValueIndex(getValue()); 95 } 96 97 /** 98 * Accepts a visitor. 99 */ accept(MemberValueVisitor visitor)100 public void accept(MemberValueVisitor visitor) { 101 visitor.visitCharMemberValue(this); 102 } 103 } 104