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.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.File;
26 import java.io.IOException;
27
28 import org.apache.bcel.AbstractTestCase;
29 import org.apache.bcel.Const;
30 import org.apache.bcel.classfile.AnnotationEntry;
31 import org.apache.bcel.classfile.ElementValuePair;
32 import org.apache.bcel.classfile.Field;
33 import org.apache.bcel.classfile.JavaClass;
34 import org.apache.bcel.util.SyntheticRepository;
35 import org.junit.jupiter.api.Test;
36
37 public class FieldAnnotationsTestCase extends AbstractTestCase {
38
39 public void checkAnnotatedField(final JavaClass clazz, final String fieldname, final String AnnotationEntryName, final String AnnotationEntryElementName,
40 final String AnnotationEntryElementValue) {
41 final Field[] fields = clazz.getFields();
42 for (final Field f : fields) {
43 final AnnotationEntry[] fieldAnnotationEntrys = f.getAnnotationEntries();
44 if (f.getName().equals(fieldname)) {
45 checkAnnotationEntry(fieldAnnotationEntrys[0], AnnotationEntryName, AnnotationEntryElementName, AnnotationEntryElementValue);
46 assertNotNull(f.getAttribute(Const.ATTR_RUNTIME_VISIBLE_ANNOTATIONS));
47 assertNull(f.getAttribute(Const.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS));
48 }
49 }
50 }
51
52 private void checkAnnotationEntry(final AnnotationEntry a, final String name, final String elementname, final String elementvalue) {
53 assertEquals(name, a.getAnnotationType(), "Wrong AnnotationEntry name");
54 assertEquals(1, a.getElementValuePairs().length, "Wrong number of AnnotationEntry elements");
55 final ElementValuePair envp = a.getElementValuePairs()[0];
56 assertEquals(envp.getNameString(), elementname, "Wrong element name");
57 assertEquals(envp.getValue().stringifyValue(), elementvalue, "Wrong element value");
58 }
59
60
61
62
63 @Test
64 public void testFieldAnnotationEntrys() throws ClassNotFoundException {
65 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnnotatedFields");
66
67 checkAnnotatedField(clazz, "i", "L" + PACKAGE_BASE_SIG + "/data/SimpleAnnotation;", "id", "1");
68 checkAnnotatedField(clazz, "s", "L" + PACKAGE_BASE_SIG + "/data/SimpleAnnotation;", "id", "2");
69 }
70
71
72
73
74 @Test
75 public void testFieldAnnotationEntrysReadWrite() throws ClassNotFoundException, IOException {
76 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnnotatedFields");
77 checkAnnotatedField(clazz, "i", "L" + PACKAGE_BASE_SIG + "/data/SimpleAnnotation;", "id", "1");
78 checkAnnotatedField(clazz, "s", "L" + PACKAGE_BASE_SIG + "/data/SimpleAnnotation;", "id", "2");
79
80 final File tfile = createTestdataFile("AnnotatedFields.class");
81 clazz.dump(tfile);
82 final SyntheticRepository repos2 = createRepos(".");
83 repos2.loadClass("AnnotatedFields");
84 checkAnnotatedField(clazz, "i", "L" + PACKAGE_BASE_SIG + "/data/SimpleAnnotation;", "id", "1");
85 checkAnnotatedField(clazz, "s", "L" + PACKAGE_BASE_SIG + "/data/SimpleAnnotation;", "id", "2");
86 assertTrue(tfile.delete());
87 }
88
89
90
91
92 @Test
93 public void testFieldAnnotationModification() throws ClassNotFoundException {
94 final boolean dbg = false;
95 final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnnotatedFields");
96 final ClassGen clg = new ClassGen(clazz);
97 Field f = clg.getFields()[0];
98 if (dbg) {
99 System.err.println("Field in freshly constructed class is: " + f);
100 }
101 if (dbg) {
102 System.err.println("AnnotationEntrys on field are: " + dumpAnnotationEntries(f.getAnnotationEntries()));
103 }
104 final AnnotationEntryGen fruitBasedAnnotationEntry = createFruitAnnotationEntry(clg.getConstantPool(), "Tomato", false);
105 final FieldGen fg = new FieldGen(f, clg.getConstantPool());
106 if (dbg) {
107 System.err.println("Adding AnnotationEntry to the field");
108 }
109 fg.addAnnotationEntry(fruitBasedAnnotationEntry);
110 if (dbg) {
111 System.err.println("FieldGen (mutable field) is " + fg);
112 }
113 if (dbg) {
114 System.err.println("with AnnotationEntrys: " + dumpAnnotationEntries(fg.getAnnotationEntries()));
115 }
116 if (dbg) {
117 System.err.println("Replacing original field with new field that has extra AnnotationEntry");
118 }
119 clg.removeField(f);
120 clg.addField(fg.getField());
121 f = clg.getFields()[1];
122
123
124 if (dbg) {
125 System.err.println("Field now looks like this: " + f);
126 }
127 if (dbg) {
128 System.err.println("With AnnotationEntrys: " + dumpAnnotationEntries(f.getAnnotationEntries()));
129 }
130 assertEquals(2, f.getAnnotationEntries().length, "Wrong number of AnnotationEntries");
131 }
132 }