View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Programmatically construct an mutable annotation (AnnotationGen) object.
72       */
73      @Test
74      void testConstructMutableAnnotation() throws IOException {
75          // Create the containing class
76          final ClassGen cg = createClassGen("HelloWorld");
77          final ConstantPoolGen cp = cg.getConstantPool();
78          // Create the simple primitive value '4' of type 'int'
79          final SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.PRIMITIVE_INT, cp, 4);
80          // Give it a name, call it 'id'
81          final ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp);
82          // Check it looks right
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          // Build an annotation of type 'SimpleAnnotation' with 'id=4' as the
88          // only value :)
89          final AnnotationEntryGen a = new AnnotationEntryGen(t, elements, true, cp);
90          // Check we can save and load it ok
91          checkSerialize(a, cp);
92      }
93  
94      @Test
95      void testVisibleInvisibleAnnotationGen() {
96          // Create the containing class
97          final ClassGen cg = createClassGen("HelloWorld");
98          final ConstantPoolGen cp = cg.getConstantPool();
99          // Create the simple primitive value '4' of type 'int'
100         final SimpleElementValueGen evg = new SimpleElementValueGen(ElementValueGen.PRIMITIVE_INT, cp, 4);
101         // Give it a name, call it 'id'
102         final ElementValuePairGen nvGen = new ElementValuePairGen("id", evg, cp);
103         // Check it looks right
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         // Build a RV annotation of type 'SimpleAnnotation' with 'id=4' as the
109         // only value :)
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         // Build a RIV annotation of type 'SimpleAnnotation' with 'id=4' as the
123         // only value :)
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 }