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