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.

AnnotationGenTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.ArrayList;
  16. import java.util.Collection;
  17. import java.util.List;
  18. import java.util.Vector;
  19. import org.aspectj.apache.bcel.Constants;
  20. import org.aspectj.apache.bcel.classfile.Attribute;
  21. import org.aspectj.apache.bcel.classfile.ConstantPool;
  22. import org.aspectj.apache.bcel.classfile.Utility;
  23. import org.aspectj.apache.bcel.classfile.annotation.AnnotationGen;
  24. import org.aspectj.apache.bcel.classfile.annotation.ElementValue;
  25. import org.aspectj.apache.bcel.classfile.annotation.NameValuePair;
  26. import org.aspectj.apache.bcel.classfile.annotation.RuntimeAnnos;
  27. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos;
  28. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos;
  29. import org.aspectj.apache.bcel.classfile.annotation.SimpleElementValue;
  30. import org.aspectj.apache.bcel.generic.ClassGen;
  31. import org.aspectj.apache.bcel.generic.ObjectType;
  32. public class AnnotationGenTest extends BcelTestCase {
  33. @Override
  34. protected void setUp() throws Exception {
  35. super.setUp();
  36. }
  37. private ClassGen createClassGen(String classname) {
  38. return new ClassGen(classname, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
  39. }
  40. /**
  41. * Programmatically construct an mutable annotation (AnnotationGen) object.
  42. */
  43. public void testConstructMutableAnnotation() {
  44. // Create the containing class
  45. ClassGen cg = createClassGen("HelloWorld");
  46. ConstantPool cp = cg.getConstantPool();
  47. // Create the simple primitive value '4' of type 'int'
  48. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_INT, cp, 4);
  49. // Give it a name, call it 'id'
  50. NameValuePair nvGen = new NameValuePair("id", evg, cp);
  51. // Check it looks right
  52. assertTrue("Should include string 'id=4' but says: " + nvGen.toString(), nvGen.toString().contains("id=4"));
  53. ObjectType t = new ObjectType("SimpleAnnotation");
  54. List<NameValuePair> elements = new ArrayList<NameValuePair>();
  55. elements.add(nvGen);
  56. // Build an annotation of type 'SimpleAnnotation' with 'id=4' as the only value :)
  57. AnnotationGen a = new AnnotationGen(t, elements, true, cp);
  58. // Check we can save and load it ok
  59. checkSerialize(a, cp);
  60. }
  61. public void testVisibleInvisibleAnnotationGen() {
  62. // Create the containing class
  63. ClassGen cg = createClassGen("HelloWorld");
  64. ConstantPool cp = cg.getConstantPool();
  65. // Create the simple primitive value '4' of type 'int'
  66. SimpleElementValue evg = new SimpleElementValue(ElementValue.PRIMITIVE_INT, cp, 4);
  67. // Give it a name, call it 'id'
  68. NameValuePair nvGen = new NameValuePair("id", evg, cp);
  69. // Check it looks right
  70. assertTrue("Should include string 'id=4' but says: " + nvGen.toString(), nvGen.toString().contains("id=4"));
  71. ObjectType t = new ObjectType("SimpleAnnotation");
  72. List<NameValuePair> elements = new ArrayList<NameValuePair>();
  73. elements.add(nvGen);
  74. // Build a RV annotation of type 'SimpleAnnotation' with 'id=4' as the only value :)
  75. AnnotationGen a = new AnnotationGen(t, elements, true, cp);
  76. Vector<AnnotationGen> v = new Vector<AnnotationGen>();
  77. v.add(a);
  78. Collection<RuntimeAnnos> attributes = Utility.getAnnotationAttributes(cp, v);
  79. boolean foundRV = false;
  80. for (Attribute attribute : attributes) {
  81. if (attribute instanceof RuntimeVisAnnos) {
  82. assertTrue(((RuntimeAnnos) attribute).areVisible());
  83. foundRV = true;
  84. }
  85. }
  86. assertTrue("Should have seen a RuntimeVisibleAnnotation", foundRV);
  87. // Build a RIV annotation of type 'SimpleAnnotation' with 'id=4' as the only value :)
  88. AnnotationGen a2 = new AnnotationGen(t, elements, false, cp);
  89. Vector<AnnotationGen> v2 = new Vector<AnnotationGen>();
  90. v2.add(a2);
  91. Collection<RuntimeAnnos> attributes2 = Utility.getAnnotationAttributes(cp, v2);
  92. boolean foundRIV = false;
  93. for (Attribute attribute : attributes2) {
  94. // for (int i = 0; i < attributes2.length; i++) {
  95. // Attribute attribute = attributes2[i];
  96. if (attribute instanceof RuntimeInvisAnnos) {
  97. assertFalse(((RuntimeAnnos) attribute).areVisible());
  98. foundRIV = true;
  99. }
  100. }
  101. assertTrue("Should have seen a RuntimeInvisibleAnnotation", foundRIV);
  102. }
  103. // //
  104. // Helper methods
  105. private void checkSerialize(AnnotationGen a, ConstantPool cpg) {
  106. try {
  107. String beforeName = a.getTypeName();
  108. List<NameValuePair> beforeValues = a.getValues();
  109. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  110. DataOutputStream dos = new DataOutputStream(baos);
  111. a.dump(dos);
  112. dos.flush();
  113. dos.close();
  114. byte[] bs = baos.toByteArray();
  115. ByteArrayInputStream bais = new ByteArrayInputStream(bs);
  116. DataInputStream dis = new DataInputStream(bais);
  117. AnnotationGen annAfter = AnnotationGen.read(dis, cpg, a.isRuntimeVisible());
  118. dis.close();
  119. String afterName = annAfter.getTypeName();
  120. List<NameValuePair> afterValues = annAfter.getValues();
  121. if (!beforeName.equals(afterName)) {
  122. fail("Deserialization failed: before type='" + beforeName + "' after type='" + afterName + "'");
  123. }
  124. if (a.getValues().size() != annAfter.getValues().size()) {
  125. fail("Different numbers of element name value pairs?? " + a.getValues().size() + "!=" + annAfter.getValues().size());
  126. }
  127. for (int i = 0; i < a.getValues().size(); i++) {
  128. NameValuePair beforeElement = a.getValues().get(i);
  129. NameValuePair afterElement = annAfter.getValues().get(i);
  130. if (!beforeElement.getNameString().equals(afterElement.getNameString())) {
  131. fail("Different names?? " + beforeElement.getNameString() + "!=" + afterElement.getNameString());
  132. }
  133. }
  134. } catch (IOException ioe) {
  135. fail("Unexpected exception whilst checking serialization: " + ioe);
  136. }
  137. }
  138. @Override
  139. protected void tearDown() throws Exception {
  140. super.tearDown();
  141. }
  142. }