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.

EnumElementValueGen.java 3.1KB

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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement - initial implementation {date}
  11. * ******************************************************************/
  12. package org.aspectj.apache.bcel.generic.annotation;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import org.aspectj.apache.bcel.classfile.ConstantClass;
  16. import org.aspectj.apache.bcel.classfile.ConstantString;
  17. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  18. import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
  19. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  20. import org.aspectj.apache.bcel.generic.ObjectType;
  21. public class EnumElementValueGen extends ElementValueGen {
  22. // For enum types, these two indices point to the type and value
  23. private int typeIdx;
  24. private int valueIdx;
  25. /**
  26. * This ctor assumes the constant pool already contains the right type and value -
  27. * as indicated by typeIdx and valueIdx. This ctor is used for deserialization
  28. */
  29. protected EnumElementValueGen(int typeIdx,int valueIdx,ConstantPoolGen cpool) {
  30. super(ElementValueGen.ENUM_CONSTANT,cpool);
  31. if (type != ENUM_CONSTANT)
  32. throw new RuntimeException("Only element values of type enum can be built with this ctor");
  33. this.typeIdx = typeIdx;
  34. this.valueIdx= valueIdx;
  35. }
  36. public EnumElementValueGen(ObjectType t,String value,ConstantPoolGen cpool) {
  37. super(ElementValueGen.ENUM_CONSTANT,cpool);
  38. typeIdx = cpool.addClass(t);
  39. valueIdx= cpool.addString(value);
  40. }
  41. public EnumElementValueGen(EnumElementValue value, ConstantPoolGen cpool) {
  42. super(ENUM_CONSTANT,cpool);
  43. typeIdx = value.getTypeIndex();
  44. valueIdx = value.getValueIndex();
  45. }
  46. public void dump(DataOutputStream dos) throws IOException {
  47. dos.writeByte(type); // u1 type of value (ENUM_CONSTANT == 'e')
  48. dos.writeShort(typeIdx); // u2
  49. dos.writeShort(valueIdx); // u2
  50. }
  51. public String stringifyValue() {
  52. ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx);
  53. return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
  54. }
  55. // BCELBUG: Should we need to call utility.signatureToString() on the output here?
  56. public String getEnumTypeString() {
  57. ConstantClass cu8 = (ConstantClass)getConstantPool().getConstant(typeIdx);
  58. return ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
  59. // return Utility.signatureToString(cu8.getBytes());
  60. }
  61. public String getEnumValueString() {
  62. ConstantString cu8 = (ConstantString)getConstantPool().getConstant(valueIdx);
  63. return ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
  64. }
  65. public int getValueIndex() { return valueIdx;}
  66. public int getTypeIndex() { return typeIdx; }
  67. }