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