1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
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);
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 }