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 java.io.IOException;
019: import java.lang.reflect.Method;
020:
021: import javassist.ClassPool;
022: import javassist.bytecode.ConstPool;
023: import javassist.bytecode.Descriptor;
024:
025: /**
026: * Enum constant value.
027: *
028: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
029: * @author Shigeru Chiba
030: */
031: public class EnumMemberValue extends MemberValue {
032: int typeIndex, valueIndex;
033:
034: /**
035: * Constructs an enum constant value. The initial value is specified
036: * by the constant pool entries at the given indexes.
037: *
038: * @param type the index of a CONSTANT_Utf8_info structure
039: * representing the enum type.
040: * @param value the index of a CONSTANT_Utf8_info structure.
041: * representing the enum value.
042: */
043: public EnumMemberValue(int type, int value, ConstPool cp) {
044: super ('e', cp);
045: this .typeIndex = type;
046: this .valueIndex = value;
047: }
048:
049: /**
050: * Constructs an enum constant value.
051: * The initial value is not specified.
052: */
053: public EnumMemberValue(ConstPool cp) {
054: super ('e', cp);
055: typeIndex = valueIndex = 0;
056: }
057:
058: Object getValue(ClassLoader cl, ClassPool cp, Method m)
059: throws ClassNotFoundException {
060: try {
061: return getType(cl).getField(getValue()).get(null);
062: } catch (NoSuchFieldException e) {
063: throw new ClassNotFoundException(getType() + "."
064: + getValue());
065: } catch (IllegalAccessException e) {
066: throw new ClassNotFoundException(getType() + "."
067: + getValue());
068: }
069: }
070:
071: Class getType(ClassLoader cl) throws ClassNotFoundException {
072: return loadClass(cl, getType());
073: }
074:
075: /**
076: * Obtains the enum type name.
077: *
078: * @return a fully-qualified type name.
079: */
080: public String getType() {
081: return Descriptor.toClassName(cp.getUtf8Info(typeIndex));
082: }
083:
084: /**
085: * Changes the enum type name.
086: *
087: * @param typename a fully-qualified type name.
088: */
089: public void setType(String typename) {
090: typeIndex = cp.addUtf8Info(Descriptor.of(typename));
091: }
092:
093: /**
094: * Obtains the name of the enum constant value.
095: */
096: public String getValue() {
097: return cp.getUtf8Info(valueIndex);
098: }
099:
100: /**
101: * Changes the name of the enum constant value.
102: */
103: public void setValue(String name) {
104: valueIndex = cp.addUtf8Info(name);
105: }
106:
107: public String toString() {
108: return getType() + "." + getValue();
109: }
110:
111: /**
112: * Writes the value.
113: */
114: public void write(AnnotationsWriter writer) throws IOException {
115: writer.enumConstValue(cp.getUtf8Info(typeIndex), getValue());
116: }
117:
118: /**
119: * Accepts a visitor.
120: */
121: public void accept(MemberValueVisitor visitor) {
122: visitor.visitEnumMemberValue(this);
123: }
124: }
|