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 javassist.bytecode.Descriptor;
021: import java.io.IOException;
022: import java.lang.reflect.Method;
023:
024: /**
025: * Class value.
026: *
027: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
028: * @author Shigeru Chiba
029: */
030: public class ClassMemberValue extends MemberValue {
031: int valueIndex;
032:
033: /**
034: * Constructs a class value. The initial value is specified
035: * by the constant pool entry at the given index.
036: *
037: * @param index the index of a CONSTANT_Utf8_info structure.
038: */
039: public ClassMemberValue(int index, ConstPool cp) {
040: super ('c', cp);
041: this .valueIndex = index;
042: }
043:
044: /**
045: * Constructs a class value.
046: *
047: * @param className the initial value.
048: */
049: public ClassMemberValue(String className, ConstPool cp) {
050: super ('c', cp);
051: setValue(className);
052: }
053:
054: /**
055: * Constructs a class value.
056: * The initial value is java.lang.Class.
057: */
058: public ClassMemberValue(ConstPool cp) {
059: super ('c', cp);
060: setValue("java.lang.Class");
061: }
062:
063: Object getValue(ClassLoader cl, ClassPool cp, Method m)
064: throws ClassNotFoundException {
065: final String classname = getValue();
066: if (classname.equals("void"))
067: return void.class;
068: else if (classname.equals("int"))
069: return int.class;
070: else if (classname.equals("byte"))
071: return byte.class;
072: else if (classname.equals("long"))
073: return long.class;
074: else if (classname.equals("double"))
075: return double.class;
076: else if (classname.equals("float"))
077: return float.class;
078: else if (classname.equals("char"))
079: return char.class;
080: else if (classname.equals("short"))
081: return short.class;
082: else if (classname.equals("boolean"))
083: return boolean.class;
084: else
085: return loadClass(cl, classname);
086: }
087:
088: Class getType(ClassLoader cl) throws ClassNotFoundException {
089: return loadClass(cl, "java.lang.Class");
090: }
091:
092: /**
093: * Obtains the value of the member.
094: *
095: * @return fully-qualified class name.
096: */
097: public String getValue() {
098: String v = cp.getUtf8Info(valueIndex);
099: return Descriptor.toClassName(v);
100: }
101:
102: /**
103: * Sets the value of the member.
104: *
105: * @param newClassName fully-qualified class name.
106: */
107: public void setValue(String newClassName) {
108: String setTo = Descriptor.of(newClassName);
109: valueIndex = cp.addUtf8Info(setTo);
110: }
111:
112: /**
113: * Obtains the string representation of this object.
114: */
115: public String toString() {
116: return "<" + getValue() + " class>";
117: }
118:
119: /**
120: * Writes the value.
121: */
122: public void write(AnnotationsWriter writer) throws IOException {
123: writer.classInfoIndex(cp.getUtf8Info(valueIndex));
124: }
125:
126: /**
127: * Accepts a visitor.
128: */
129: public void accept(MemberValueVisitor visitor) {
130: visitor.visitClassMemberValue(this);
131: }
132: }
|