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: package javassist.bytecode.annotation;
016:
017: import javassist.ClassPool;
018: import javassist.bytecode.ConstPool;
019: import java.io.IOException;
020: import java.lang.reflect.Array;
021: import java.lang.reflect.Method;
022:
023: /**
024: * Array member.
025: *
026: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
027: * @author Shigeru Chiba
028: */
029: public class ArrayMemberValue extends MemberValue {
030: MemberValue type;
031: MemberValue[] values;
032:
033: /**
034: * Constructs an array. The initial value or type are not specified.
035: */
036: public ArrayMemberValue(ConstPool cp) {
037: super ('[', cp);
038: type = null;
039: values = null;
040: }
041:
042: /**
043: * Constructs an array. The initial value is not specified.
044: *
045: * @param t the type of the array elements.
046: */
047: public ArrayMemberValue(MemberValue t, ConstPool cp) {
048: super ('[', cp);
049: type = t;
050: values = null;
051: }
052:
053: Object getValue(ClassLoader cl, ClassPool cp, Method method)
054: throws ClassNotFoundException {
055: if (values == null)
056: throw new ClassNotFoundException(
057: "no array elements found: " + method.getName());
058:
059: int size = values.length;
060: Class clazz;
061: if (type == null) {
062: clazz = method.getReturnType().getComponentType();
063: if (clazz == null || size > 0)
064: throw new ClassNotFoundException("broken array type: "
065: + method.getName());
066: } else
067: clazz = type.getType(cl);
068:
069: Object a = Array.newInstance(clazz, size);
070: for (int i = 0; i < size; i++)
071: Array.set(a, i, values[i].getValue(cl, cp, method));
072:
073: return a;
074: }
075:
076: Class getType(ClassLoader cl) throws ClassNotFoundException {
077: if (type == null)
078: throw new ClassNotFoundException("no array type specified");
079:
080: Object a = Array.newInstance(type.getType(cl), 0);
081: return a.getClass();
082: }
083:
084: /**
085: * Obtains the type of the elements.
086: *
087: * @return null if the type is not specified.
088: */
089: public MemberValue getType() {
090: return type;
091: }
092:
093: /**
094: * Obtains the elements of the array.
095: */
096: public MemberValue[] getValue() {
097: return values;
098: }
099:
100: /**
101: * Sets the elements of the array.
102: */
103: public void setValue(MemberValue[] elements) {
104: values = elements;
105: if (elements != null && elements.length > 0)
106: type = elements[0];
107: }
108:
109: /**
110: * Obtains the string representation of this object.
111: */
112: public String toString() {
113: StringBuffer buf = new StringBuffer("{");
114: if (values != null) {
115: for (int i = 0; i < values.length; i++) {
116: buf.append(values[i].toString());
117: if (i + 1 < values.length)
118: buf.append(", ");
119: }
120: }
121:
122: buf.append("}");
123: return buf.toString();
124: }
125:
126: /**
127: * Writes the value.
128: */
129: public void write(AnnotationsWriter writer) throws IOException {
130: int num = values.length;
131: writer.arrayValue(num);
132: for (int i = 0; i < num; ++i)
133: values[i].write(writer);
134: }
135:
136: /**
137: * Accepts a visitor.
138: */
139: public void accept(MemberValueVisitor visitor) {
140: visitor.visitArrayMemberValue(this);
141: }
142: }
|