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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM All rights reserved. This program and the accompanying
  3. * materials are made available under the terms of the Common Public License
  4. * v1.0 which accompanies this distribution and is available at
  5. * http://www.eclipse.org/legal/cpl-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 org.aspectj.apache.bcel.Constants;
  18. import org.aspectj.apache.bcel.generic.ClassGen;
  19. import org.aspectj.apache.bcel.generic.ConstantPoolGen;
  20. import org.aspectj.apache.bcel.generic.ObjectType;
  21. import org.aspectj.apache.bcel.generic.annotation.AnnotationGen;
  22. import org.aspectj.apache.bcel.generic.annotation.ElementNameValuePairGen;
  23. import org.aspectj.apache.bcel.generic.annotation.ElementValueGen;
  24. import org.aspectj.apache.bcel.generic.annotation.SimpleElementValueGen;
  25. public class AnnotationGenTest extends BcelTestCase {
  26. protected void setUp() throws Exception {
  27. super.setUp();
  28. }
  29. private ClassGen createClassGen(String classname) {
  30. return new ClassGen(classname, "java.lang.Object",
  31. "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null);
  32. }
  33. /**
  34. * Programmatically construct an mutable annotation (AnnotationGen) object.
  35. */
  36. public void testConstructMutableAnnotation() {
  37. // Create the containing class
  38. ClassGen cg = createClassGen("HelloWorld");
  39. ConstantPoolGen cp = cg.getConstantPool();
  40. // Create the simple primitive value '4' of type 'int'
  41. SimpleElementValueGen evg =
  42. new SimpleElementValueGen(ElementValueGen.PRIMITIVE_INT,cp,4);
  43. // Give it a name, call it 'id'
  44. ElementNameValuePairGen nvGen = new ElementNameValuePairGen("id",evg,cp);
  45. // Check it looks right
  46. assertTrue("Should include string 'id=4' but says: "+nvGen.toString(),
  47. nvGen.toString().indexOf("id=4")!=-1);
  48. ObjectType t = new ObjectType("SimpleAnnotation");
  49. List elements = new ArrayList();
  50. elements.add(nvGen);
  51. // Build an annotation of type 'SimpleAnnotation' with 'id=4' as the only value :)
  52. AnnotationGen a = new AnnotationGen(t,elements,true,cp);
  53. // Check we can save and load it ok
  54. checkSerialize(a,cp);
  55. }
  56. ////
  57. // Helper methods
  58. private void checkSerialize(AnnotationGen a,ConstantPoolGen cpg) {
  59. try {
  60. String beforeName = a.getTypeName();
  61. List beforeValues = a.getValues();
  62. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  63. DataOutputStream dos = new DataOutputStream(baos);
  64. a.dump(dos);
  65. dos.flush();
  66. dos.close();
  67. byte[] bs = baos.toByteArray();
  68. ByteArrayInputStream bais = new ByteArrayInputStream(bs);
  69. DataInputStream dis = new DataInputStream(bais);
  70. AnnotationGen annAfter = AnnotationGen.read(dis,cpg,a.isRuntimeVisible());
  71. dis.close();
  72. String afterName = annAfter.getTypeName();
  73. List afterValues = annAfter.getValues();
  74. if (!beforeName.equals(afterName)) {
  75. fail("Deserialization failed: before type='"+beforeName+"' after type='"+afterName+"'");
  76. }
  77. if (a.getValues().size()!=annAfter.getValues().size()) {
  78. fail("Different numbers of element name value pairs?? "+a.getValues().size()+"!="+annAfter.getValues().size());
  79. }
  80. for (int i=0;i<a.getValues().size();i++) {
  81. ElementNameValuePairGen beforeElement = (ElementNameValuePairGen) a.getValues().get(i);
  82. ElementNameValuePairGen afterElement = (ElementNameValuePairGen) annAfter.getValues().get(i);
  83. if (!beforeElement.getNameString().equals(afterElement.getNameString())) {
  84. fail("Different names?? "+beforeElement.getNameString()+"!="+afterElement.getNameString());
  85. }
  86. }
  87. } catch (IOException ioe) {
  88. fail("Unexpected exception whilst checking serialization: "+ioe);
  89. }
  90. }
  91. protected void tearDown() throws Exception {
  92. super.tearDown();
  93. }
  94. }