Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SimpleElementValueGen.java 7.2KB

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