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  package org.apache.bcel.generic;
20  
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 org.apache.bcel.AbstractTest;
26  import org.apache.bcel.Const;
27  import org.apache.bcel.classfile.AnnotationElementValue;
28  import org.apache.bcel.classfile.AnnotationEntry;
29  import org.apache.bcel.classfile.ArrayElementValue;
30  import org.apache.bcel.classfile.ElementValue;
31  import org.apache.bcel.classfile.ElementValuePair;
32  import org.apache.bcel.classfile.JavaClass;
33  import org.apache.bcel.classfile.Method;
34  import org.apache.bcel.classfile.ParameterAnnotationEntry;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Checks that the runtime-visible flag is propagated when parameter annotations and nested annotation values are parsed
39   * from a class file.
40   */
41  class ParameterAnnotationVisibilityTest extends AbstractTest {
42  
43      private Method getConstructor(final JavaClass jc) {
44          for (final Method method : jc.getMethods()) {
45              if (Const.CONSTRUCTOR_NAME.equals(method.getName())) {
46                  return method;
47              }
48          }
49          return null;
50      }
51  
52      private JavaClass loadTestClass() throws ClassNotFoundException {
53          return getTestJavaClass(PACKAGE_BASE_NAME + ".data.AnnotatedWithCombinedAnnotation");
54      }
55  
56      @Test
57      void testNestedAnnotationValueIsRuntimeVisible() throws ClassNotFoundException {
58          final JavaClass jc = loadTestClass();
59          boolean found = false;
60          for (final AnnotationEntry outer : jc.getAnnotationEntries()) {
61              for (final ElementValuePair pair : outer.getElementValuePairs()) {
62                  final ElementValue value = pair.getValue();
63                  if (value instanceof ArrayElementValue) {
64                      for (final ElementValue inner : ((ArrayElementValue) value).getElementValuesArray()) {
65                          if (inner instanceof AnnotationElementValue) {
66                              found = true;
67                              assertTrue(((AnnotationElementValue) inner).getAnnotationEntry().isRuntimeVisible(),
68                                  "nested annotation value parsed from a runtime-visible annotation reported as invisible");
69                          }
70                      }
71                  }
72              }
73          }
74          assertTrue(found, "no nested annotation values found in test data");
75      }
76  
77      @Test
78      void testParameterAnnotationVisibilitySurvivesMethodGen() throws ClassNotFoundException {
79          final JavaClass jc = loadTestClass();
80          final Method init = getConstructor(jc);
81          final ConstantPoolGen cp = new ConstantPoolGen(jc.getConstantPool());
82          final MethodGen mg = new MethodGen(init, jc.getClassName(), cp);
83          mg.getAnnotationsOnParameter(1); // unpack the existing parameter annotations before rebuilding
84          final Method out = mg.getMethod();
85          assertNotNull(out.getAttribute(Const.ATTR_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS),
86              "runtime-visible parameter annotation dropped on MethodGen round-trip");
87          assertNull(out.getAttribute(Const.ATTR_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS),
88              "runtime-visible parameter annotation downgraded to invisible on MethodGen round-trip");
89      }
90  
91      @Test
92      void testParsedParameterAnnotationIsRuntimeVisible() throws ClassNotFoundException {
93          final JavaClass jc = loadTestClass();
94          final Method init = getConstructor(jc);
95          assertNotNull(init);
96          boolean found = false;
97          for (final ParameterAnnotationEntry pae : init.getParameterAnnotationEntries()) {
98              for (final AnnotationEntry ae : pae.getAnnotationEntries()) {
99                  found = true;
100                 assertTrue(ae.isRuntimeVisible(), "runtime-visible parameter annotation parsed as invisible");
101             }
102         }
103         assertTrue(found, "no parameter annotations found in test data");
104     }
105 }