View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // helper methods
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       * Check field AnnotationEntrys are retrievable.
62       */
63      @Test
64      public void testFieldAnnotationEntrys() throws ClassNotFoundException {
65          final JavaClass clazz = getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnnotatedFields");
66          // TODO L...;?
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       * Check field AnnotationEntrys (de)serialize ok.
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          // Write it out
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       * Check we can load in a class, modify its field AnnotationEntrys, save it, reload it and everything is correct.
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]; // there are two fields in the class, removing
122                                 // and readding has changed the order
123         // so this time index [1] is the 'int i' field
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 }