1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel.generic;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.DataInputStream;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.apache.bcel.AbstractTestCase;
33 import org.apache.bcel.Const;
34 import org.apache.bcel.classfile.Annotations;
35 import org.apache.bcel.classfile.Attribute;
36 import org.apache.bcel.classfile.RuntimeInvisibleAnnotations;
37 import org.apache.bcel.classfile.RuntimeVisibleAnnotations;
38 import org.junit.jupiter.api.Test;
39
40 public class AnnotationGenTestCase extends AbstractTestCase {
41 private void checkSerialize(final AnnotationEntryGen a, final ConstantPoolGen cpg) throws IOException {
42 final String beforeName = a.getTypeName();
43 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
44 try (DataOutputStream dos = new DataOutputStream(baos)) {
45 a.dump(dos);
46 dos.flush();
47 }
48 final byte[] bs = baos.toByteArray();
49 final ByteArrayInputStream bais = new ByteArrayInputStream(bs);
50 final AnnotationEntryGen annAfter;
51 try (DataInputStream dis = new DataInputStream(bais)) {
52 annAfter = AnnotationEntryGen.read(dis, cpg, a.isRuntimeVisible());
53 }
54 final String afterName = annAfter.getTypeName();
55 assertEquals(beforeName, afterName, "Deserialization failed");
56 assertEquals(a.getValues().size(), annAfter.getValues().size(), "Different numbers of element name value pairs??");
57 for (int i = 0; i < a.getValues().size(); i++) {
58 final ElementValuePairGen beforeElement = a.getValues().get(i);
59 final ElementValuePairGen afterElement = annAfter.getValues().get(i);
60 assertEquals(beforeElement.getNameString(), afterElement.getNameString(), "Different names??");
61 }
62 }
63
64 private ClassGen createClassGen(final String className) {
65 return new ClassGen(className, "java.lang.Object", "<generated>", Const.ACC_PUBLIC | Const.ACC_SUPER, null);
66 }
67
68
69
70
71 @Test
72 public void testConstructMutableAnnotation() throws IOException {
73
74 final ClassGen cg = createClassGen("HelloWorld");
75 final ConstantPoolGen cp = cg.getConstantPool();
76
77 final SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.PRIMITIVE_INT, cp, 4);
78
79 final ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp);
80
81 assertTrue(nvGen.toString().contains("id=4"), "Should include string 'id=4' but says: " + nvGen.toString());
82 final ObjectType t = new ObjectType("SimpleAnnotation");
83 final List<ElementValuePairGen> elements = new ArrayList<>();
84 elements.add(nvGen);
85
86
87 final AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp);
88
89 checkSerialize(a, cp);
90 }
91
92 @Test
93 public void testVisibleInvisibleAnnotationGen() {
94
95 final ClassGen cg = createClassGen("HelloWorld");
96 final ConstantPoolGen cp = cg.getConstantPool();
97
98 final SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.PRIMITIVE_INT, cp, 4);
99
100 final ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp);
101
102 assertTrue(nvGen.toString().contains("id=4"), "Should include string 'id=4' but says: " + nvGen.toString());
103 final ObjectType t = new ObjectType("SimpleAnnotation");
104 final List<ElementValuePairGen> elements = new ArrayList<>();
105 elements.add(nvGen);
106
107
108 final AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp);
109 final List<AnnotationEntryGen> v = new ArrayList<>();
110 v.add(a);
111 final Attribute[] attributes = AnnotationEntryGen.getAnnotationAttributes(cp, v.toArray(AnnotationEntryGen.EMPTY_ARRAY));
112 boolean foundRV = false;
113 for (final Attribute attribute : attributes) {
114 if (attribute instanceof RuntimeVisibleAnnotations) {
115 assertTrue(((Annotations) attribute).isRuntimeVisible());
116 foundRV = true;
117 }
118 }
119 assertTrue(foundRV, "Should have seen a RuntimeVisibleAnnotation");
120
121
122 final AnnotationEntryGen a2 = new AnnotationEntryGen(t, elements, false, cp);
123 final List<AnnotationEntryGen> v2 = new ArrayList<>();
124 v2.add(a2);
125 final Attribute[] attributes2 = AnnotationEntryGen.getAnnotationAttributes(cp, v2.toArray(AnnotationEntryGen.EMPTY_ARRAY));
126 boolean foundRIV = false;
127 for (final Attribute attribute : attributes2) {
128 if (attribute instanceof RuntimeInvisibleAnnotations) {
129 assertFalse(((Annotations) attribute).isRuntimeVisible());
130 foundRIV = true;
131 }
132 }
133 assertTrue(foundRIV, "Should have seen a RuntimeInvisibleAnnotation");
134 }
135 }