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.

SimpleElementValue.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.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.ConstantDouble;
  17. import org.aspectj.apache.bcel.classfile.ConstantFloat;
  18. import org.aspectj.apache.bcel.classfile.ConstantInteger;
  19. import org.aspectj.apache.bcel.classfile.ConstantLong;
  20. import org.aspectj.apache.bcel.classfile.ConstantPool;
  21. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  22. public class SimpleElementValue extends ElementValue {
  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, assumes the one at the supplied index
  30. * is correct.
  31. */
  32. protected SimpleElementValue(int type, int idx, ConstantPool cpGen) {
  33. super(type, cpGen);
  34. this.idx = idx;
  35. }
  36. public SimpleElementValue(int type, ConstantPool cpGen, int value) {
  37. super(type, cpGen);
  38. idx = cpGen.addInteger(value);
  39. }
  40. public SimpleElementValue(int type, ConstantPool cpGen, long value) {
  41. super(type, cpGen);
  42. idx = cpGen.addLong(value);
  43. }
  44. public SimpleElementValue(int type, ConstantPool cpGen, double value) {
  45. super(type, cpGen);
  46. idx = cpGen.addDouble(value);
  47. }
  48. public SimpleElementValue(int type, ConstantPool cpGen, float value) {
  49. super(type, cpGen);
  50. idx = cpGen.addFloat(value);
  51. }
  52. public SimpleElementValue(int type, ConstantPool cpGen, short value) {
  53. super(type, cpGen);
  54. idx = cpGen.addInteger(value);
  55. }
  56. public SimpleElementValue(int type, ConstantPool cpGen, byte value) {
  57. super(type, cpGen);
  58. idx = cpGen.addInteger(value);
  59. }
  60. public SimpleElementValue(int type, ConstantPool cpGen, char value) {
  61. super(type, cpGen);
  62. idx = cpGen.addInteger(value);
  63. }
  64. public SimpleElementValue(int type, ConstantPool cpGen, boolean value) {
  65. super(type, cpGen);
  66. if (value) {
  67. idx = cpGen.addInteger(1);
  68. } else {
  69. idx = cpGen.addInteger(0);
  70. }
  71. }
  72. public SimpleElementValue(int type, ConstantPool cpGen, String value) {
  73. super(type, cpGen);
  74. idx = cpGen.addUtf8(value);
  75. }
  76. public byte getValueByte() {
  77. if (type != PRIMITIVE_BYTE) {
  78. throw new RuntimeException("Dont call getValueByte() on a non BYTE ElementValue");
  79. }
  80. ConstantInteger c = (ConstantInteger) cpool.getConstant(idx, Constants.CONSTANT_Integer);
  81. return (byte) c.getIntValue();
  82. }
  83. public char getValueChar() {
  84. if (type != PRIMITIVE_CHAR) {
  85. throw new RuntimeException("Dont call getValueChar() on a non CHAR ElementValue");
  86. }
  87. ConstantInteger c = (ConstantInteger) cpool.getConstant(idx, Constants.CONSTANT_Integer);
  88. return (char) c.getIntValue();
  89. }
  90. public long getValueLong() {
  91. if (type != PRIMITIVE_LONG) {
  92. throw new RuntimeException("Dont call getValueLong() on a non LONG ElementValue");
  93. }
  94. ConstantLong j = (ConstantLong) cpool.getConstant(idx);
  95. return j.getValue();
  96. }
  97. public float getValueFloat() {
  98. if (type != PRIMITIVE_FLOAT) {
  99. throw new RuntimeException("Dont call getValueFloat() on a non FLOAT ElementValue");
  100. }
  101. ConstantFloat f = (ConstantFloat) cpool.getConstant(idx);
  102. return f.getValue();
  103. }
  104. public double getValueDouble() {
  105. if (type != PRIMITIVE_DOUBLE) {
  106. throw new RuntimeException("Dont call getValueDouble() on a non DOUBLE ElementValue");
  107. }
  108. ConstantDouble d = (ConstantDouble) cpool.getConstant(idx);
  109. return d.getValue();
  110. }
  111. public boolean getValueBoolean() {
  112. if (type != PRIMITIVE_BOOLEAN) {
  113. throw new RuntimeException("Dont call getValueBoolean() on a non BOOLEAN ElementValue");
  114. }
  115. ConstantInteger bo = (ConstantInteger) cpool.getConstant(idx);
  116. return (bo.getValue() != 0);
  117. }
  118. public short getValueShort() {
  119. if (type != PRIMITIVE_SHORT) {
  120. throw new RuntimeException("Dont call getValueShort() on a non SHORT ElementValue");
  121. }
  122. ConstantInteger s = (ConstantInteger) cpool.getConstant(idx);
  123. return (short) s.getIntValue();
  124. }
  125. /**
  126. * The boolean controls whether we copy info from the 'old' constant pool to the 'new'. You need to use this ctor if the
  127. * annotation is being copied from one file to another.
  128. */
  129. public SimpleElementValue(SimpleElementValue value, ConstantPool cpool, boolean copyPoolEntries) {
  130. super(value.getElementValueType(), cpool);
  131. if (!copyPoolEntries) {
  132. // J5ASSERT: Could assert value.stringifyValue() is the same as
  133. // cpool.getConstant(SimpleElementValuevalue.getIndex())
  134. idx = value.getIndex();
  135. } else {
  136. switch (value.getElementValueType()) {
  137. case STRING:
  138. idx = cpool.addUtf8(value.getValueString());
  139. break;
  140. case PRIMITIVE_INT:
  141. idx = cpool.addInteger(value.getValueInt());
  142. break;
  143. case PRIMITIVE_BYTE:
  144. idx = cpool.addInteger(value.getValueByte());
  145. break;
  146. case PRIMITIVE_CHAR:
  147. idx = cpool.addInteger(value.getValueChar());
  148. break;
  149. case PRIMITIVE_LONG:
  150. idx = cpool.addLong(value.getValueLong());
  151. break;
  152. case PRIMITIVE_FLOAT:
  153. idx = cpool.addFloat(value.getValueFloat());
  154. break;
  155. case PRIMITIVE_DOUBLE:
  156. idx = cpool.addDouble(value.getValueDouble());
  157. break;
  158. case PRIMITIVE_BOOLEAN:
  159. if (value.getValueBoolean()) {
  160. idx = cpool.addInteger(1);
  161. } else {
  162. idx = cpool.addInteger(0);
  163. }
  164. break;
  165. case PRIMITIVE_SHORT:
  166. idx = cpool.addInteger(value.getValueShort());
  167. break;
  168. default:
  169. throw new RuntimeException("SimpleElementValueGen class does not know how " + "to copy this type " + type);
  170. }
  171. }
  172. }
  173. public int getIndex() {
  174. return idx;
  175. }
  176. public String getValueString() {
  177. if (type != STRING) {
  178. throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue");
  179. }
  180. ConstantUtf8 c = (ConstantUtf8) cpool.getConstant(idx);
  181. return c.getValue();
  182. }
  183. public int getValueInt() {
  184. if (type != PRIMITIVE_INT) {
  185. throw new RuntimeException("Dont call getValueString() on a non STRING ElementValue");
  186. }
  187. ConstantInteger c = (ConstantInteger) cpool.getConstant(idx);
  188. return c.getValue();
  189. }
  190. // Whatever kind of value it is, return it as a string
  191. @Override
  192. public String stringifyValue() {
  193. switch (type) {
  194. case PRIMITIVE_INT:
  195. ConstantInteger c = (ConstantInteger) cpool.getConstant(idx);
  196. return Integer.toString(c.getValue());
  197. case PRIMITIVE_LONG:
  198. ConstantLong j = (ConstantLong) cpool.getConstant(idx);
  199. return Long.toString(j.getValue());
  200. case PRIMITIVE_DOUBLE:
  201. ConstantDouble d = (ConstantDouble) cpool.getConstant(idx);
  202. return d.getValue().toString();
  203. case PRIMITIVE_FLOAT:
  204. ConstantFloat f = (ConstantFloat) cpool.getConstant(idx);
  205. return Float.toString(f.getValue());
  206. case PRIMITIVE_SHORT:
  207. ConstantInteger s = (ConstantInteger) cpool.getConstant(idx);
  208. return Integer.toString(s.getValue());
  209. case PRIMITIVE_BYTE:
  210. ConstantInteger b = (ConstantInteger) cpool.getConstant(idx);
  211. return Integer.toString(b.getValue());
  212. case PRIMITIVE_CHAR:
  213. ConstantInteger ch = (ConstantInteger) cpool.getConstant(idx);
  214. return Character.toString((char) ch.getIntValue());
  215. case PRIMITIVE_BOOLEAN:
  216. ConstantInteger bo = (ConstantInteger) cpool.getConstant(idx);
  217. if (bo.getValue() == 0) {
  218. return "false";
  219. } else {
  220. return "true";
  221. }
  222. case STRING:
  223. ConstantUtf8 cu8 = (ConstantUtf8) cpool.getConstant(idx);
  224. return cu8.getValue();
  225. default:
  226. throw new RuntimeException("SimpleElementValueGen class does not know how to stringify type " + type);
  227. }
  228. }
  229. @Override
  230. public String toString() {
  231. StringBuilder s = new StringBuilder();
  232. switch (type) {
  233. case PRIMITIVE_INT:
  234. ConstantInteger c = (ConstantInteger) cpool.getConstant(idx);
  235. s.append("(int)").append(c.getValue());
  236. break;
  237. case PRIMITIVE_LONG:
  238. ConstantLong j = (ConstantLong) cpool.getConstant(idx);
  239. s.append("(long)").append(j.getValue());
  240. break;
  241. case PRIMITIVE_DOUBLE:
  242. ConstantDouble d = (ConstantDouble) cpool.getConstant(idx);
  243. s.append("(double)").append(d.getValue().toString());
  244. break;
  245. case PRIMITIVE_FLOAT:
  246. ConstantFloat f = (ConstantFloat) cpool.getConstant(idx);
  247. s.append("(float)").append(f.getValue());
  248. break;
  249. case PRIMITIVE_SHORT:
  250. ConstantInteger ci = (ConstantInteger) cpool.getConstant(idx);
  251. s.append("(short)").append(ci.getValue());
  252. break;
  253. case PRIMITIVE_BYTE:
  254. ConstantInteger b = (ConstantInteger) cpool.getConstant(idx);
  255. s.append("(byte)").append(b.getValue());
  256. break;
  257. case PRIMITIVE_CHAR:
  258. ConstantInteger ch = (ConstantInteger) cpool.getConstant(idx);
  259. s.append("(char)").append((char) ch.getIntValue());
  260. break;
  261. case PRIMITIVE_BOOLEAN:
  262. ConstantInteger bo = (ConstantInteger) cpool.getConstant(idx);
  263. s.append("(boolean)");
  264. if (bo.getValue() == 0) {
  265. s.append("false");
  266. } else {
  267. s.append("true");
  268. }
  269. break;
  270. case STRING:
  271. ConstantUtf8 cu8 = (ConstantUtf8) cpool.getConstant(idx);
  272. s.append("(string)").append(cu8.getValue());
  273. break;
  274. default:
  275. throw new RuntimeException("SimpleElementValueGen class does not know how to stringify type " + type);
  276. }
  277. return s.toString();
  278. }
  279. @Override
  280. public void dump(DataOutputStream dos) throws IOException {
  281. dos.writeByte(type); // u1 kind of value
  282. switch (type) {
  283. case PRIMITIVE_INT:
  284. case PRIMITIVE_BYTE:
  285. case PRIMITIVE_CHAR:
  286. case PRIMITIVE_FLOAT:
  287. case PRIMITIVE_LONG:
  288. case PRIMITIVE_BOOLEAN:
  289. case PRIMITIVE_SHORT:
  290. case PRIMITIVE_DOUBLE:
  291. case STRING:
  292. dos.writeShort(idx);
  293. break;
  294. default:
  295. throw new RuntimeException("SimpleElementValueGen doesnt know how to write out type " + type);
  296. }
  297. }
  298. }