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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode.annotation;
  16. import javassist.bytecode.ConstPool;
  17. import java.io.IOException;
  18. /**
  19. * Array member.
  20. *
  21. * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
  22. * @author Shigeru Chiba
  23. */
  24. public class ArrayMemberValue extends MemberValue {
  25. MemberValue type;
  26. MemberValue[] values;
  27. /**
  28. * Constructs an array. The initial value or type are not specified.
  29. */
  30. public ArrayMemberValue(ConstPool cp) {
  31. super('[', cp);
  32. type = null;
  33. values = null;
  34. }
  35. /**
  36. * Constructs an array. The initial value is not specified.
  37. *
  38. * @param t the type of the array elements.
  39. */
  40. public ArrayMemberValue(MemberValue t, ConstPool cp) {
  41. super('[', cp);
  42. type = t;
  43. values = null;
  44. }
  45. /**
  46. * Obtains the type of the elements.
  47. *
  48. * @return null if the type is not specified.
  49. */
  50. public MemberValue getType() {
  51. return type;
  52. }
  53. /**
  54. * Obtains the elements of the array.
  55. */
  56. public MemberValue[] getValue() {
  57. return values;
  58. }
  59. /**
  60. * Sets the elements of the array.
  61. */
  62. public void setValue(MemberValue[] elements) {
  63. values = elements;
  64. if (elements != null && elements.length > 0)
  65. type = elements[0];
  66. }
  67. /**
  68. * Obtains the string representation of this object.
  69. */
  70. public String toString() {
  71. StringBuffer buf = new StringBuffer("{");
  72. if (values != null) {
  73. for (int i = 0; i < values.length; i++) {
  74. buf.append(values[i].toString());
  75. if (i + 1 < values.length)
  76. buf.append(", ");
  77. }
  78. }
  79. buf.append("}");
  80. return buf.toString();
  81. }
  82. void write(AnnotationsWriter writer) throws IOException {
  83. int num = values.length;
  84. writer.arrayValue(num);
  85. for (int i = 0; i < num; ++i)
  86. values[i].write(writer);
  87. }
  88. /**
  89. * Accepts a visitor.
  90. */
  91. public void accept(MemberValueVisitor visitor) {
  92. visitor.visitArrayMemberValue(this);
  93. }
  94. }