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