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.

ElementValue.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* *******************************************************************
  2. * Copyright (c) 2004, 2013 IBM, VMware
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement - initial implementation {date}
  11. * ******************************************************************/
  12. package org.aspectj.apache.bcel.classfile.annotation;
  13. import java.io.DataInputStream;
  14. import java.io.DataOutputStream;
  15. import java.io.IOException;
  16. import org.aspectj.apache.bcel.classfile.ConstantPool;
  17. public abstract class ElementValue {
  18. public static final int STRING = 's';
  19. public static final int ENUM_CONSTANT = 'e';
  20. public static final int CLASS = 'c';
  21. public static final int ANNOTATION = '@';
  22. public static final int ARRAY = '[';
  23. public static final int PRIMITIVE_INT = 'I';
  24. public static final int PRIMITIVE_BYTE = 'B';
  25. public static final int PRIMITIVE_CHAR = 'C';
  26. public static final int PRIMITIVE_DOUBLE = 'D';
  27. public static final int PRIMITIVE_FLOAT = 'F';
  28. public static final int PRIMITIVE_LONG = 'J';
  29. public static final int PRIMITIVE_SHORT = 'S';
  30. public static final int PRIMITIVE_BOOLEAN = 'Z';
  31. protected int type;
  32. protected ConstantPool cpool;
  33. protected ElementValue(int type, ConstantPool cpool) {
  34. this.type = type;
  35. this.cpool = cpool;
  36. }
  37. public int getElementValueType() {
  38. return type;
  39. }
  40. public abstract String stringifyValue();
  41. public abstract void dump(DataOutputStream dos) throws IOException;
  42. public static ElementValue readElementValue(DataInputStream dis, ConstantPool cpGen) throws IOException {
  43. int type = dis.readUnsignedByte();
  44. switch (type) {
  45. case 'B': // byte
  46. return new SimpleElementValue(PRIMITIVE_BYTE, dis.readUnsignedShort(), cpGen);
  47. case 'C': // char
  48. return new SimpleElementValue(PRIMITIVE_CHAR, dis.readUnsignedShort(), cpGen);
  49. case 'D': // double
  50. return new SimpleElementValue(PRIMITIVE_DOUBLE, dis.readUnsignedShort(), cpGen);
  51. case 'F': // float
  52. return new SimpleElementValue(PRIMITIVE_FLOAT, dis.readUnsignedShort(), cpGen);
  53. case 'I': // int
  54. return new SimpleElementValue(PRIMITIVE_INT, dis.readUnsignedShort(), cpGen);
  55. case 'J': // long
  56. return new SimpleElementValue(PRIMITIVE_LONG, dis.readUnsignedShort(), cpGen);
  57. case 'S': // short
  58. return new SimpleElementValue(PRIMITIVE_SHORT, dis.readUnsignedShort(), cpGen);
  59. case 'Z': // boolean
  60. return new SimpleElementValue(PRIMITIVE_BOOLEAN, dis.readUnsignedShort(), cpGen);
  61. case 's': // String
  62. return new SimpleElementValue(STRING, dis.readUnsignedShort(), cpGen);
  63. case 'e': // Enum constant
  64. return new EnumElementValue(dis.readUnsignedShort(), dis.readUnsignedShort(), cpGen);
  65. case 'c': // Class
  66. return new ClassElementValue(dis.readUnsignedShort(), cpGen);
  67. // FIXME should this be true here? or should it be the value for the containing annotation?
  68. case '@': // Annotation
  69. return new AnnotationElementValue(ANNOTATION, AnnotationGen.read(dis, cpGen, true), cpGen);
  70. case '[': // Array
  71. int numArrayVals = dis.readUnsignedShort();
  72. ElementValue[] evalues = new ElementValue[numArrayVals];
  73. for (int j = 0; j < numArrayVals; j++) {
  74. evalues[j] = ElementValue.readElementValue(dis, cpGen);
  75. }
  76. return new ArrayElementValue(ARRAY, evalues, cpGen);
  77. default:
  78. throw new RuntimeException("Unexpected element value kind in annotation: " + type);
  79. }
  80. }
  81. protected ConstantPool getConstantPool() {
  82. return cpool;
  83. }
  84. /**
  85. * Creates an (modifiable) ElementValueGen copy of an (immutable) ElementValue - constant pool is assumed correct.
  86. */
  87. public static ElementValue copy(ElementValue value, ConstantPool cpool, boolean copyPoolEntries) {
  88. switch (value.getElementValueType()) {
  89. case 'B': // byte
  90. case 'C': // char
  91. case 'D': // double
  92. case 'F': // float
  93. case 'I': // int
  94. case 'J': // long
  95. case 'S': // short
  96. case 'Z': // boolean
  97. case 's': // String
  98. return new SimpleElementValue((SimpleElementValue) value, cpool, copyPoolEntries);
  99. case 'e': // Enum constant
  100. return new EnumElementValue((EnumElementValue) value, cpool, copyPoolEntries);
  101. case '@': // Annotation
  102. return new AnnotationElementValue((AnnotationElementValue) value, cpool, copyPoolEntries);
  103. case '[': // Array
  104. return new ArrayElementValue((ArrayElementValue) value, cpool, copyPoolEntries);
  105. case 'c': // Class
  106. return new ClassElementValue((ClassElementValue) value, cpool, copyPoolEntries);
  107. default:
  108. throw new RuntimeException("Not implemented yet! (" + value.getElementValueType() + ")");
  109. }
  110. }
  111. }