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.

SimpleElementValueGen.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.ConstantDouble;
  16. import org.aspectj.apache.bcel.classfile.ConstantFloat;
  17. import org.aspectj.apache.bcel.classfile.ConstantInteger;
  18. import org.aspectj.apache.bcel.classfile.ConstantLong;
  19. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  20. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
  21. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  22. public class SimpleElementValueGen extends ElementValueGen {
  23. // For primitive types and string type, this points to the value entry in the cpGen
  24. // For 'class' this points to the class entry in the cpGen
  25. private int idx;
  26. // ctors for each supported type... type could be inferred but for now lets
  27. // force it to be passed
  28. /**
  29. * Protected ctor used for deserialization, doesn't *put* an entry in the constant pool,
  30. * assumes the one at the supplied index is correct.
  31. */
  32. protected SimpleElementValueGen(int type,int idx,ConstantPoolGen cpGen) {
  33. super(type,cpGen);
  34. this.idx = idx;
  35. }
  36. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,int value) {
  37. super(type,cpGen);
  38. idx = cpGen.addInteger(value);
  39. }
  40. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,long value) {
  41. super(type,cpGen);
  42. idx = cpGen.addLong(value);
  43. }
  44. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,double value) {
  45. super(type,cpGen);
  46. idx = cpGen.addDouble(value);
  47. }
  48. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,float value) {
  49. super(type,cpGen);
  50. idx = cpGen.addFloat(value);
  51. }
  52. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,short value) {
  53. super(type,cpGen);
  54. idx = cpGen.addInteger(value);
  55. }
  56. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,byte value) {
  57. super(type,cpGen);
  58. idx = cpGen.addInteger(value);
  59. }
  60. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,char value) {
  61. super(type,cpGen);
  62. idx = cpGen.addInteger(value);
  63. }
  64. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,boolean value) {
  65. super(type,cpGen);
  66. if (value) idx = cpGen.addInteger(1);
  67. else idx = cpGen.addInteger(0);
  68. }
  69. public SimpleElementValueGen(int type,ConstantPoolGen cpGen,String value) {
  70. super(type,cpGen);
  71. idx = cpGen.addUtf8(value);
  72. }
  73. public SimpleElementValueGen(SimpleElementValue value,ConstantPoolGen cpool) {
  74. super(value.getElementValueType(),cpool);
  75. // J5ASSERT: Could assert value.stringifyValue() is the same as
  76. // cpool.getConstant(SimpleElementValuevalue.getIndex())
  77. idx = value.getIndex();
  78. }
  79. public int getIndex() {
  80. return idx;
  81. }
  82. public String getValueString() {
  83. if (type != STRING)
  84. throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue");
  85. ConstantUtf8 c = (ConstantUtf8)cpGen.getConstant(idx);
  86. return c.getBytes();
  87. }
  88. public int getValueInt() {
  89. if (type != PRIMITIVE_INT)
  90. throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue");
  91. ConstantInteger c = (ConstantInteger)cpGen.getConstant(idx);
  92. return c.getBytes();
  93. }
  94. // Whatever kind of value it is, return it as a string
  95. public String stringifyValue() {
  96. switch (type) {
  97. case PRIMITIVE_INT:
  98. ConstantInteger c = (ConstantInteger)cpGen.getConstant(idx);
  99. return Integer.toString(c.getBytes());
  100. case PRIMITIVE_LONG:
  101. ConstantLong j = (ConstantLong)cpGen.getConstant(idx);
  102. return Long.toString(j.getBytes());
  103. case PRIMITIVE_DOUBLE:
  104. ConstantDouble d = (ConstantDouble)cpGen.getConstant(idx);
  105. return Double.toString(d.getBytes());
  106. case PRIMITIVE_FLOAT:
  107. ConstantFloat f = (ConstantFloat)cpGen.getConstant(idx);
  108. return Float.toString(f.getBytes());
  109. case PRIMITIVE_SHORT:
  110. ConstantInteger s = (ConstantInteger)cpGen.getConstant(idx);
  111. return Integer.toString(s.getBytes());
  112. case PRIMITIVE_BYTE:
  113. ConstantInteger b = (ConstantInteger)cpGen.getConstant(idx);
  114. return Integer.toString(b.getBytes());
  115. case PRIMITIVE_CHAR:
  116. ConstantInteger ch = (ConstantInteger)cpGen.getConstant(idx);
  117. return Integer.toString(ch.getBytes());
  118. case PRIMITIVE_BOOLEAN:
  119. ConstantInteger bo = (ConstantInteger)cpGen.getConstant(idx);
  120. if (bo.getBytes() == 0) return "false";
  121. if (bo.getBytes() != 0) return "true";
  122. case STRING:
  123. ConstantUtf8 cu8 = (ConstantUtf8)cpGen.getConstant(idx);
  124. return cu8.getBytes();
  125. default:
  126. throw new RuntimeException("SimpleElementValueGen class does not know how to stringify type "+type);
  127. }
  128. }
  129. public void dump(DataOutputStream dos) throws IOException {
  130. dos.writeByte(type); // u1 kind of value
  131. switch (type) {
  132. case PRIMITIVE_INT:
  133. case PRIMITIVE_BYTE:
  134. case PRIMITIVE_CHAR:
  135. case PRIMITIVE_FLOAT:
  136. case PRIMITIVE_LONG:
  137. case PRIMITIVE_BOOLEAN:
  138. case PRIMITIVE_SHORT:
  139. case PRIMITIVE_DOUBLE:
  140. case STRING:
  141. dos.writeShort(idx);
  142. break;
  143. default:
  144. throw new RuntimeException("SimpleElementValueGen doesnt know how to write out type "+type);
  145. }
  146. }
  147. }