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.6KB

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