You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArrayMemberValue.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. package javassist.bytecode.annotation;
  17. import javassist.ClassPool;
  18. import javassist.bytecode.ConstPool;
  19. import java.io.IOException;
  20. import java.lang.reflect.Array;
  21. import java.lang.reflect.Method;
  22. /**
  23. * Array member.
  24. *
  25. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  26. * @author Shigeru Chiba
  27. */
  28. public class ArrayMemberValue extends MemberValue {
  29. MemberValue type;
  30. MemberValue[] values;
  31. /**
  32. * Constructs an array. The initial value or type are not specified.
  33. */
  34. public ArrayMemberValue(ConstPool cp) {
  35. super('[', cp);
  36. type = null;
  37. values = null;
  38. }
  39. /**
  40. * Constructs an array. The initial value is not specified.
  41. *
  42. * @param t the type of the array elements.
  43. */
  44. public ArrayMemberValue(MemberValue t, ConstPool cp) {
  45. super('[', cp);
  46. type = t;
  47. values = null;
  48. }
  49. Object getValue(ClassLoader cl, ClassPool cp, Method method)
  50. throws ClassNotFoundException
  51. {
  52. if (values == null)
  53. throw new ClassNotFoundException(
  54. "no array elements found: " + method.getName());
  55. int size = values.length;
  56. Class clazz;
  57. if (type == null) {
  58. clazz = method.getReturnType().getComponentType();
  59. if (clazz == null || size > 0)
  60. throw new ClassNotFoundException("broken array type: "
  61. + method.getName());
  62. }
  63. else
  64. clazz = type.getType(cl);
  65. Object a = Array.newInstance(clazz, size);
  66. for (int i = 0; i < size; i++)
  67. Array.set(a, i, values[i].getValue(cl, cp, method));
  68. return a;
  69. }
  70. Class getType(ClassLoader cl) throws ClassNotFoundException {
  71. if (type == null)
  72. throw new ClassNotFoundException("no array type specified");
  73. Object a = Array.newInstance(type.getType(cl), 0);
  74. return a.getClass();
  75. }
  76. /**
  77. * Obtains the type of the elements.
  78. *
  79. * @return null if the type is not specified.
  80. */
  81. public MemberValue getType() {
  82. return type;
  83. }
  84. /**
  85. * Obtains the elements of the array.
  86. */
  87. public MemberValue[] getValue() {
  88. return values;
  89. }
  90. /**
  91. * Sets the elements of the array.
  92. */
  93. public void setValue(MemberValue[] elements) {
  94. values = elements;
  95. if (elements != null && elements.length > 0)
  96. type = elements[0];
  97. }
  98. /**
  99. * Obtains the string representation of this object.
  100. */
  101. public String toString() {
  102. StringBuffer buf = new StringBuffer("{");
  103. if (values != null) {
  104. for (int i = 0; i < values.length; i++) {
  105. buf.append(values[i].toString());
  106. if (i + 1 < values.length)
  107. buf.append(", ");
  108. }
  109. }
  110. buf.append("}");
  111. return buf.toString();
  112. }
  113. /**
  114. * Writes the value.
  115. */
  116. public void write(AnnotationsWriter writer) throws IOException {
  117. int num = values == null ? 0 : values.length;
  118. writer.arrayValue(num);
  119. for (int i = 0; i < num; ++i)
  120. values[i].write(writer);
  121. }
  122. /**
  123. * Accepts a visitor.
  124. */
  125. public void accept(MemberValueVisitor visitor) {
  126. visitor.visitArrayMemberValue(this);
  127. }
  128. }