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.

ElementValueGen.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 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.generic.annotation;
  13. import java.io.DataInputStream;
  14. import java.io.DataOutputStream;
  15. import java.io.IOException;
  16. import org.aspectj.apache.bcel.classfile.annotation.AnnotationElementValue;
  17. import org.aspectj.apache.bcel.classfile.annotation.ArrayElementValue;
  18. import org.aspectj.apache.bcel.classfile.annotation.ClassElementValue;
  19. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  20. import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
  21. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
  22. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  23. public abstract class ElementValueGen {
  24. protected int type;
  25. protected ConstantPoolGen cpGen;
  26. protected ElementValueGen(int type,ConstantPoolGen cpGen) {
  27. this.type = type;
  28. this.cpGen = cpGen;
  29. }
  30. /**
  31. * Subtypes return an immutable variant of the ElementValueGen
  32. */
  33. public abstract ElementValue getElementValue();
  34. public int getElementValueType() {
  35. return type;
  36. }
  37. public abstract String stringifyValue();
  38. public abstract void dump(DataOutputStream dos) throws IOException;
  39. public static final int STRING = 's';
  40. public static final int ENUM_CONSTANT = 'e';
  41. public static final int CLASS = 'c';
  42. public static final int ANNOTATION = '@';
  43. public static final int ARRAY = '[';
  44. public static final int PRIMITIVE_INT = 'I';
  45. public static final int PRIMITIVE_BYTE = 'B';
  46. public static final int PRIMITIVE_CHAR = 'C';
  47. public static final int PRIMITIVE_DOUBLE = 'D';
  48. public static final int PRIMITIVE_FLOAT = 'F';
  49. public static final int PRIMITIVE_LONG = 'J';
  50. public static final int PRIMITIVE_SHORT = 'S';
  51. public static final int PRIMITIVE_BOOLEAN= 'Z';
  52. public static ElementValueGen readElementValue(DataInputStream dis,ConstantPoolGen cpGen) throws IOException {
  53. int type= dis.readUnsignedByte();
  54. switch (type) {
  55. case 'B': // byte
  56. return new SimpleElementValueGen(PRIMITIVE_BYTE,dis.readUnsignedShort(),cpGen);
  57. case 'C': // char
  58. return new SimpleElementValueGen(PRIMITIVE_CHAR,dis.readUnsignedShort(),cpGen);
  59. case 'D': // double
  60. return new SimpleElementValueGen(PRIMITIVE_DOUBLE,dis.readUnsignedShort(),cpGen);
  61. case 'F': // float
  62. return new SimpleElementValueGen(PRIMITIVE_FLOAT,dis.readUnsignedShort(),cpGen);
  63. case 'I': // int
  64. return new SimpleElementValueGen(PRIMITIVE_INT,dis.readUnsignedShort(),cpGen);
  65. case 'J': // long
  66. return new SimpleElementValueGen(PRIMITIVE_LONG,dis.readUnsignedShort(),cpGen);
  67. case 'S': // short
  68. return new SimpleElementValueGen(PRIMITIVE_SHORT,dis.readUnsignedShort(),cpGen);
  69. case 'Z': // boolean
  70. return new SimpleElementValueGen(PRIMITIVE_BOOLEAN,dis.readUnsignedShort(),cpGen);
  71. case 's': // String
  72. return new SimpleElementValueGen(STRING,dis.readUnsignedShort(),cpGen);
  73. case 'e': // Enum constant
  74. return new EnumElementValueGen(dis.readUnsignedShort(),dis.readUnsignedShort(),cpGen);
  75. case 'c': // Class
  76. return new ClassElementValueGen(dis.readUnsignedShort(),cpGen);
  77. //
  78. // case '@': // Annotation
  79. // return new AnnotationElementValueGen(ANNOTATION,Annotation.read(dis,cpGen),cpGen);
  80. //
  81. // case '[': // Array
  82. // int numArrayVals = dis.readUnsignedShort();
  83. // List arrayVals = new ArrayList();
  84. // ElementValue[] evalues = new ElementValue[numArrayVals];
  85. // for (int j=0;j<numArrayVals;j++) {
  86. // evalues[j] = ElementValue.readElementValue(dis,cpGen);
  87. // }
  88. // return new ArrayElementValue(ARRAY,evalues,cpGen);
  89. default:
  90. throw new RuntimeException("Unexpected element value kind in annotation: "+type);
  91. }
  92. }
  93. protected ConstantPoolGen getConstantPool() {
  94. return cpGen;
  95. }
  96. /**
  97. * Creates an (modifiable) ElementValueGen copy of an (immutable) ElementValue - constant pool is assumed correct.
  98. */
  99. public static ElementValueGen copy(ElementValue value,ConstantPoolGen cpool,boolean copyPoolEntries) {
  100. switch (value.getElementValueType()) {
  101. case 'B': // byte
  102. case 'C': // char
  103. case 'D': // double
  104. case 'F': // float
  105. case 'I': // int
  106. case 'J': // long
  107. case 'S': // short
  108. case 'Z': // boolean
  109. case 's': // String
  110. return new SimpleElementValueGen((SimpleElementValue)value,cpool,copyPoolEntries);
  111. case 'e': // Enum constant
  112. return new EnumElementValueGen((EnumElementValue)value,cpool,copyPoolEntries);
  113. case '@': // Annotation
  114. return new AnnotationElementValueGen((AnnotationElementValue)value,cpool,copyPoolEntries);
  115. case '[': // Array
  116. return new ArrayElementValueGen((ArrayElementValue)value,cpool,copyPoolEntries);
  117. case 'c': // Class
  118. return new ClassElementValueGen((ClassElementValue)value,cpool,copyPoolEntries);
  119. default:
  120. throw new RuntimeException("Not implemented yet! ("+value.getElementValueType()+")");
  121. }
  122. }
  123. }