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.

ArrayElementValue.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Andy Clement - initial implementation {date}
  11. * ******************************************************************/
  12. package org.aspectj.apache.bcel.classfile.annotation;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import org.aspectj.apache.bcel.classfile.ConstantPool;
  16. public class ArrayElementValue extends ElementValue {
  17. private static final ElementValue[] NO_VALUES = new ElementValue[0];
  18. // J5TODO: Should we make this an array or a list? A list would be easier to modify ...
  19. private ElementValue[] evalues = NO_VALUES;
  20. public ElementValue[] getElementValuesArray() {
  21. return evalues;
  22. }
  23. public int getElementValuesArraySize() {
  24. return evalues.length;
  25. }
  26. public ArrayElementValue(ConstantPool cp) {
  27. super(ARRAY, cp);
  28. }
  29. public ArrayElementValue(int type, ElementValue[] datums, ConstantPool cpool) {
  30. super(type, cpool);
  31. if (type != ARRAY)
  32. throw new RuntimeException("Only element values of type array can be built with this ctor");
  33. this.evalues = datums;
  34. }
  35. public ArrayElementValue(ArrayElementValue value, ConstantPool cpool, boolean copyPoolEntries) {
  36. super(ARRAY, cpool);
  37. evalues = new ElementValue[value.getElementValuesArraySize()];
  38. ElementValue[] in = value.getElementValuesArray();
  39. for (int i = 0; i < in.length; i++) {
  40. evalues[i] = ElementValue.copy(in[i], cpool, copyPoolEntries);
  41. }
  42. }
  43. @Override
  44. public void dump(DataOutputStream dos) throws IOException {
  45. dos.writeByte(type); // u1 type of value (ARRAY == '[')
  46. dos.writeShort(evalues.length);
  47. for (ElementValue evalue : evalues) {
  48. evalue.dump(dos);
  49. }
  50. }
  51. @Override
  52. public String stringifyValue() {
  53. StringBuilder sb = new StringBuilder();
  54. sb.append("[");
  55. for (int i = 0; i < evalues.length; i++) {
  56. ElementValue element = evalues[i];
  57. sb.append(element.stringifyValue());
  58. if ((i + 1) < evalues.length)
  59. sb.append(",");
  60. }
  61. sb.append("]");
  62. return sb.toString();
  63. }
  64. public void addElement(ElementValue gen) {
  65. ElementValue[] old = evalues;
  66. evalues = new ElementValue[evalues.length + 1];
  67. System.arraycopy(old, 0, evalues, 0, old.length);
  68. evalues[old.length] = gen;
  69. }
  70. }