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.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       * Programmatically construct an mutable annotation (AnnotationGen) object.
70       */
71      @Test
72      public void testConstructMutableAnnotation() throws IOException {
73          // Create the containing class
74          final ClassGen cg = createClassGen("HelloWorld");
75          final ConstantPoolGen cp = cg.getConstantPool();
76          // Create the simple primitive value '4' of type 'int'
77          final SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.PRIMITIVE_INT, cp, 4);
78          // Give it a name, call it 'id'
79          final ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp);
80          // Check it looks right
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          // Build an annotation of type 'SimpleAnnotation' with 'id=4' as the
86          // only value :)
87          final AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp);
88          // Check we can save and load it ok
89          checkSerialize(a, cp);
90      }
91  
92      @Test
93      public void testVisibleInvisibleAnnotationGen() {
94          // Create the containing class
95          final ClassGen cg = createClassGen("HelloWorld");
96          final ConstantPoolGen cp = cg.getConstantPool();
97          // Create the simple primitive value '4' of type 'int'
98          final SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.PRIMITIVE_INT, cp, 4);
99          // Give it a name, call it 'id'
100         final ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp);
101         // Check it looks right
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         // Build a RV annotation of type 'SimpleAnnotation' with 'id=4' as the
107         // only value :)
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         // Build a RIV annotation of type 'SimpleAnnotation' with 'id=4' as the
121         // only value :)
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 }