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.

EnumElementValue.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Constants;
  16. import org.aspectj.apache.bcel.classfile.ConstantPool;
  17. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  18. import org.aspectj.apache.bcel.generic.ObjectType;
  19. public class EnumElementValue extends ElementValue {
  20. // For enum types, these two indices point to the type and value
  21. private int typeIdx;
  22. private int valueIdx;
  23. /**
  24. * This ctor assumes the constant pool already contains the right type and value - as indicated by typeIdx and valueIdx. This
  25. * ctor is used for deserialization
  26. */
  27. protected EnumElementValue(int typeIdx, int valueIdx, ConstantPool cpool) {
  28. super(ElementValue.ENUM_CONSTANT, cpool);
  29. if (type != ENUM_CONSTANT) {
  30. throw new RuntimeException("Only element values of type enum can be built with this ctor");
  31. }
  32. this.typeIdx = typeIdx;
  33. this.valueIdx = valueIdx;
  34. }
  35. // /**
  36. // * Return immutable variant of this EnumElementValue
  37. // */
  38. // public ElementValueGen getElementValue() {
  39. // System.err.println("Duplicating value: "+getEnumTypeString()+":"+getEnumValueString());
  40. // return new EnumElementValueGen(type,typeIdx,valueIdx,cpGen);
  41. // }
  42. public EnumElementValue(ObjectType t, String value, ConstantPool cpool) {
  43. super(ElementValue.ENUM_CONSTANT, cpool);
  44. typeIdx = cpool.addUtf8(t.getSignature());// was addClass(t);
  45. valueIdx = cpool.addUtf8(value);// was addString(value);
  46. }
  47. public EnumElementValue(EnumElementValue value, ConstantPool cpool, boolean copyPoolEntries) {
  48. super(ENUM_CONSTANT, cpool);
  49. if (copyPoolEntries) {
  50. typeIdx = cpool.addUtf8(value.getEnumTypeString());// was addClass(value.getEnumTypeString());
  51. valueIdx = cpool.addUtf8(value.getEnumValueString()); // was addString(value.getEnumValueString());
  52. } else {
  53. typeIdx = value.getTypeIndex();
  54. valueIdx = value.getValueIndex();
  55. }
  56. }
  57. @Override
  58. public void dump(DataOutputStream dos) throws IOException {
  59. dos.writeByte(type); // u1 type of value (ENUM_CONSTANT == 'e')
  60. dos.writeShort(typeIdx); // u2
  61. dos.writeShort(valueIdx); // u2
  62. }
  63. /**
  64. * return signature and value, something like Lp/Color;RED
  65. */
  66. @Override
  67. public String stringifyValue() {
  68. StringBuilder sb = new StringBuilder();
  69. ConstantUtf8 cu8 = (ConstantUtf8) cpool.getConstant(typeIdx, Constants.CONSTANT_Utf8);
  70. sb.append(cu8.getValue());
  71. cu8 = (ConstantUtf8) cpool.getConstant(valueIdx, Constants.CONSTANT_Utf8);
  72. sb.append(cu8.getValue());
  73. return sb.toString();
  74. }
  75. public String toString() {
  76. StringBuilder s = new StringBuilder("E(");
  77. s.append(getEnumTypeString()).append(" ").append(getEnumValueString()).append(")");
  78. return s.toString();
  79. }
  80. // BCELBUG: Should we need to call utility.signatureToString() on the output here?
  81. public String getEnumTypeString() {
  82. // Constant cc = getConstantPool().getConstant(typeIdx);
  83. // ConstantClass cu8 = (ConstantClass)getConstantPool().getConstant(typeIdx);
  84. // return ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
  85. return ((ConstantUtf8) getConstantPool().getConstant(typeIdx)).getValue();
  86. // return Utility.signatureToString(cu8.getBytes());
  87. }
  88. public String getEnumValueString() {
  89. return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getValue();
  90. // ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx);
  91. // return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
  92. }
  93. public int getValueIndex() {
  94. return valueIdx;
  95. }
  96. public int getTypeIndex() {
  97. return typeIdx;
  98. }
  99. }