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.

ElementValueGenTest.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM All rights reserved. This program and the accompanying
  3. * materials are made available under the terms of the Eclipse Public License
  4. * v1.0 which accompanies this distribution and is available at
  5. * http://www.eclipse.org/legal/epl-v10.html
  6. *
  7. * Contributors: Andy Clement - initial implementation
  8. ******************************************************************************/
  9. package org.aspectj.apache.bcel.classfile.tests;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.DataInputStream;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import java.util.Collections;
  16. import org.aspectj.apache.bcel.Constants;
  17. import org.aspectj.apache.bcel.classfile.ConstantPool;
  18. import org.aspectj.apache.bcel.classfile.annotation.AnnotationElementValue;
  19. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  20. import org.aspectj.apache.bcel.classfile.annotation.ClassElementValue;
  21. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  22. import org.aspectj.apache.bcel.classfile.annotation.EnumElementValue;
  23. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  24. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
  25. import org.aspectj.apache.bcel.generic.ClassGen;
  26. import org.aspectj.apache.bcel.generic.ObjectType;
  27. public class ElementValueGenTest extends BcelTestCase {
  28. protected void setUp() throws Exception {
  29. super.setUp();
  30. }
  31. private ClassGen createClassGen(String classname) {
  32. return new ClassGen(classname, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
  33. }
  34. // //
  35. // Create primitive element values
  36. public void testCreateIntegerElementValue() {
  37. ClassGen cg = createClassGen("HelloWorld");
  38. ConstantPool cp = cg.getConstantPool();
  39. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_INT, cp, 555);
  40. // Creation of an element like that should leave a new entry in the cpool
  41. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + cp.lookupInteger(555),
  42. evg.getIndex() == cp.lookupInteger(555));
  43. checkSerialize(evg, cp);
  44. }
  45. public void testCreateFloatElementValue() {
  46. ClassGen cg = createClassGen("HelloWorld");
  47. ConstantPool cp = cg.getConstantPool();
  48. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_FLOAT, cp, 111.222f);
  49. // Creation of an element like that should leave a new entry in the cpool
  50. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + cp.lookupFloat(111.222f),
  51. evg.getIndex() == cp.lookupFloat(111.222f));
  52. checkSerialize(evg, cp);
  53. }
  54. public void testCreateDoubleElementValue() {
  55. ClassGen cg = createClassGen("HelloWorld");
  56. ConstantPool cp = cg.getConstantPool();
  57. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_DOUBLE, cp, 333.44);
  58. // Creation of an element like that should leave a new entry in the cpool
  59. int idx = cp.lookupDouble(333.44);
  60. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx);
  61. checkSerialize(evg, cp);
  62. }
  63. public void testCreateLongElementValue() {
  64. ClassGen cg = createClassGen("HelloWorld");
  65. ConstantPool cp = cg.getConstantPool();
  66. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_LONG, cp, 3334455L);
  67. // Creation of an element like that should leave a new entry in the cpool
  68. int idx = cp.lookupLong(3334455L);
  69. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx);
  70. checkSerialize(evg, cp);
  71. }
  72. public void testCreateCharElementValue() {
  73. ClassGen cg = createClassGen("HelloWorld");
  74. ConstantPool cp = cg.getConstantPool();
  75. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR, cp, (char) 't');
  76. // Creation of an element like that should leave a new entry in the cpool
  77. int idx = cp.lookupInteger((char) 't');
  78. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx);
  79. checkSerialize(evg, cp);
  80. }
  81. public void testCreateByteElementValue() {
  82. ClassGen cg = createClassGen("HelloWorld");
  83. ConstantPool cp = cg.getConstantPool();
  84. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_CHAR, cp, (byte) 'z');
  85. // Creation of an element like that should leave a new entry in the cpool
  86. int idx = cp.lookupInteger((byte) 'z');
  87. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx);
  88. checkSerialize(evg, cp);
  89. }
  90. public void testCreateBooleanElementValue() {
  91. ClassGen cg = createClassGen("HelloWorld");
  92. ConstantPool cp = cg.getConstantPool();
  93. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_BOOLEAN, cp, true);
  94. // Creation of an element like that should leave a new entry in the cpool
  95. int idx = cp.lookupInteger(1); // 1 == true
  96. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx);
  97. checkSerialize(evg, cp);
  98. }
  99. public void testCreateShortElementValue() {
  100. ClassGen cg = createClassGen("HelloWorld");
  101. ConstantPool cp = cg.getConstantPool();
  102. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_SHORT, cp, (short) 42);
  103. // Creation of an element like that should leave a new entry in the cpool
  104. int idx = cp.lookupInteger(42);
  105. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + idx, evg.getIndex() == idx);
  106. checkSerialize(evg, cp);
  107. }
  108. // //
  109. // Create string element values
  110. public void testCreateStringElementValue() {
  111. // Create HelloWorld
  112. ClassGen cg = createClassGen("HelloWorld");
  113. ConstantPool cp = cg.getConstantPool();
  114. SimpleElementValue evg = new SimpleElementValue(ElementValue.STRING, cp, "hello");
  115. // Creation of an element like that should leave a new entry in the cpool
  116. assertTrue("Should have the same index in the constantpool but " + evg.getIndex() + "!=" + cp.lookupUtf8("hello"),
  117. evg.getIndex() == cp.lookupUtf8("hello"));
  118. checkSerialize(evg, cp);
  119. }
  120. // //
  121. // Create enum element value
  122. public void testCreateEnumElementValue() {
  123. ClassGen cg = createClassGen("HelloWorld");
  124. ConstantPool cp = cg.getConstantPool();
  125. ObjectType enumType = new ObjectType("SimpleEnum"); // Supports rainbow :)
  126. EnumElementValue evg = new EnumElementValue(enumType, "Red", cp);
  127. // Creation of an element like that should leave a new entry in the cpool
  128. assertTrue("The new ElementValue value index should match the contents of the constantpool but " + evg.getValueIndex()
  129. + "!=" + cp.lookupUtf8("Red"), evg.getValueIndex() == cp.lookupUtf8("Red"));
  130. // BCELBUG: Should the class signature or class name be in the constant pool? (see note in ConstantPool)
  131. // assertTrue("The new ElementValue type index should match the contents of the constantpool but "+
  132. // evg.getTypeIndex()+"!="+cp.lookupClass(enumType.getSignature()),
  133. // evg.getTypeIndex()==cp.lookupClass(enumType.getSignature()));
  134. checkSerialize(evg, cp);
  135. }
  136. public void testCreateMarkerAnnotationElementValue() {
  137. ClassGen cg = createClassGen("HelloWorld");
  138. ConstantPool cp = cg.getConstantPool();
  139. ObjectType annoType = new ObjectType("SimpleMarkerAnnotation");
  140. AnnotationGen annoGen = new AnnotationGen(annoType, Collections.<NameValuePair> emptyList(), true, cp);
  141. AnnotationElementValue evg = new AnnotationElementValue(annoGen, cp);
  142. checkSerialize(evg, cp);
  143. }
  144. // //
  145. // Create class element value
  146. public void testCreateClassElementValue() {
  147. ClassGen cg = createClassGen("HelloWorld");
  148. ConstantPool cp = cg.getConstantPool();
  149. ObjectType classType = new ObjectType("java.lang.Integer");
  150. ClassElementValue evg = new ClassElementValue(classType, cp);
  151. assertTrue("Unexpected value for contained class: '" + evg.getClassString() + "'",
  152. evg.getClassString().contains("Integer"));
  153. checkSerialize(evg, cp);
  154. }
  155. // //
  156. // Helper methods
  157. private void checkSerialize(ElementValue evgBefore, ConstantPool cpg) {
  158. try {
  159. String beforeValue = evgBefore.stringifyValue();
  160. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  161. DataOutputStream dos = new DataOutputStream(baos);
  162. evgBefore.dump(dos);
  163. dos.flush();
  164. dos.close();
  165. byte[] bs = baos.toByteArray();
  166. ByteArrayInputStream bais = new ByteArrayInputStream(bs);
  167. DataInputStream dis = new DataInputStream(bais);
  168. ElementValue evgAfter = ElementValue.readElementValue(dis, cpg);
  169. dis.close();
  170. String afterValue = evgAfter.stringifyValue();
  171. if (!beforeValue.equals(afterValue)) {
  172. fail("Deserialization failed: before='" + beforeValue + "' after='" + afterValue + "'");
  173. }
  174. } catch (IOException ioe) {
  175. fail("Unexpected exception whilst checking serialization: " + ioe);
  176. }
  177. }
  178. protected void tearDown() throws Exception {
  179. super.tearDown();
  180. }
  181. }