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    *      https://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  package org.apache.commons.lang3.external;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.lang.annotation.Annotation;
25  import java.lang.annotation.Retention;
26  import java.lang.annotation.RetentionPolicy;
27  import java.lang.reflect.InvocationTargetException;
28  
29  import org.apache.commons.lang3.AnnotationUtils;
30  import org.apache.commons.lang3.exception.UncheckedException;
31  import org.junit.jupiter.api.Test;
32  import org.junitpioneer.jupiter.SetSystemProperty;
33  
34  /**
35   * Regression test for <a href="https://issues.apache.org/jira/browse/LANG-1815">LANG-1815</a>.
36   * <p>
37   * Verifies that {@link AnnotationUtils} can reflectively read package-private annotation members
38   * for {@code equals}, {@code hashCode}, and {@code toString}, and that reflective invocation failures
39   * are not treated as inequality.
40   * </p>
41   *
42   * <h2>Important</h2>
43   * <p>
44   * This test relies on reflective access rules that differ depending on the caller's package.
45   * To reproduce the original bug, this class <strong>must remain outside</strong> the
46   * {@code org.apache.commons.lang3} package.
47   * </p>
48   * <p>
49   * Do <strong>not</strong> move this class into {@code org.apache.commons.lang3},
50   * otherwise the test may no longer exercise the failing scenario from LANG-1815.
51   * </p>
52   */
53  public class AnnotationEqualsTest {
54      /**
55       * Declares an annotation accessible without suppressing reflective access checks.
56       */
57      @Retention(RetentionPolicy.RUNTIME)
58      public @interface PublicTag {
59          /**
60           * Gets the tag value.
61           *
62           * @return the tag value
63           */
64          String value();
65      }
66  
67      @Retention(RetentionPolicy.RUNTIME)
68      @interface Tag {
69          String value();
70      }
71  
72      static class ThrowingTag implements Tag {
73          @Override
74          public Class<? extends Annotation> annotationType() {
75              return Tag.class;
76          }
77  
78          @Override
79          public String value() {
80              throw new IllegalArgumentException("boom");
81          }
82      }
83  
84      @PublicTag("value")
85      @Tag("value")
86      private final Object a = new Object();
87  
88      @PublicTag("value")
89      @Tag("value")
90      private final Object b = new Object();
91  
92      @Test
93      @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
94      void equalsRejectsPackagePrivateAnnotationsWithoutForcedAccess() throws Exception {
95          final Tag tagA = getClass().getDeclaredField("a").getAnnotation(Tag.class);
96          final Tag tagB = getClass().getDeclaredField("b").getAnnotation(Tag.class);
97          final IllegalStateException ex = assertThrows(IllegalStateException.class, () -> AnnotationUtils.equals(tagA, tagB));
98          assertInstanceOf(IllegalAccessException.class, ex.getCause());
99      }
100 
101     @Test
102     void equalsWorksOnPackagePrivateAnnotations() throws Exception {
103         final Tag tagA = getClass().getDeclaredField("a").getAnnotation(Tag.class);
104         final Tag tagB = getClass().getDeclaredField("b").getAnnotation(Tag.class);
105         assertTrue(AnnotationUtils.equals(tagA, tagB));
106     }
107 
108     @Test
109     @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
110     void equalsWorksOnPublicAnnotationsWithoutForcedAccess() throws Exception {
111         final PublicTag tagA = getClass().getDeclaredField("a").getAnnotation(PublicTag.class);
112         final PublicTag tagB = getClass().getDeclaredField("b").getAnnotation(PublicTag.class);
113         assertTrue(AnnotationUtils.equals(tagA, tagB));
114     }
115 
116     @Test
117     void equalsWrapsReflectiveOperationException() throws Exception {
118         final Tag tagA = new ThrowingTag();
119         final Tag tagB = getClass().getDeclaredField("b").getAnnotation(Tag.class);
120 
121         final IllegalStateException ex =
122                 assertThrows(IllegalStateException.class, () -> AnnotationUtils.equals(tagA, tagB));
123         assertInstanceOf(InvocationTargetException.class, ex.getCause());
124         assertEquals("boom", ((InvocationTargetException) ex.getCause()).getTargetException().getMessage());
125     }
126 
127     @Test
128     @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
129     void hashCodeRejectsPackagePrivateAnnotationsWithoutForcedAccess() throws Exception {
130         final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
131         final UncheckedException ex = assertThrows(UncheckedException.class, () -> AnnotationUtils.hashCode(tag));
132         assertInstanceOf(IllegalAccessException.class, ex.getCause());
133     }
134 
135     @Test
136     void hashCodeWorksOnPackagePrivateAnnotations() throws Exception {
137         final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
138         assertEquals(tag.hashCode(), AnnotationUtils.hashCode(tag));
139     }
140 
141     @Test
142     @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
143     void hashCodeWorksOnPublicAnnotationsWithoutForcedAccess() throws Exception {
144         final PublicTag tag = getClass().getDeclaredField("a").getAnnotation(PublicTag.class);
145         assertEquals(tag.hashCode(), AnnotationUtils.hashCode(tag));
146     }
147 
148     @Test
149     @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
150     void toStringRejectsPackagePrivateAnnotationsWithoutForcedAccess() throws Exception {
151         final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
152         final UncheckedException ex = assertThrows(UncheckedException.class, () -> AnnotationUtils.toString(tag));
153         assertInstanceOf(IllegalAccessException.class, ex.getCause());
154     }
155 
156     @Test
157     void toStringWorksOnPackagePrivateAnnotations() throws Exception {
158         final Tag tag = getClass().getDeclaredField("a").getAnnotation(Tag.class);
159         final String text = AnnotationUtils.toString(tag);
160         assertTrue(text.contains("value=value"), text);
161     }
162 
163     @Test
164     @SetSystemProperty(key = "AbstractReflection.forceAccessible", value = "false")
165     void toStringWorksOnPublicAnnotationsWithoutForcedAccess() throws Exception {
166         final PublicTag tag = getClass().getDeclaredField("a").getAnnotation(PublicTag.class);
167         assertEquals("@" + PublicTag.class.getName() + "(value=value)", AnnotationUtils.toString(tag));
168     }
169 
170 }