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