|
5 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
6 | 6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
7 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
| 8 | +import static org.junit.jupiter.api.Assertions.fail; |
8 | 9 |
|
9 | 10 | import java.io.IOException;
|
10 | 11 | import java.lang.annotation.Inherited;
|
11 | 12 | import java.lang.annotation.Retention;
|
12 | 13 | import java.lang.annotation.RetentionPolicy;
|
13 | 14 | import java.util.Arrays;
|
| 15 | +import java.util.Collection; |
| 16 | +import java.util.Collections; |
14 | 17 | import java.util.List;
|
15 | 18 | import java.util.Map;
|
| 19 | +import java.util.Set; |
16 | 20 |
|
17 | 21 | import org.jboss.jandex.AnnotationInstance;
|
18 | 22 | import org.jboss.jandex.AnnotationOverlay;
|
|
23 | 27 | import org.jboss.jandex.DotName;
|
24 | 28 | import org.jboss.jandex.FieldInfo;
|
25 | 29 | import org.jboss.jandex.Index;
|
| 30 | +import org.jboss.jandex.IndexView; |
26 | 31 | import org.jboss.jandex.MethodInfo;
|
27 | 32 | import org.jboss.jandex.MethodParameterInfo;
|
| 33 | +import org.jboss.jandex.ModuleInfo; |
28 | 34 | import org.junit.jupiter.api.Test;
|
29 | 35 |
|
30 | 36 | public class AnnotationOverlayTest {
|
@@ -327,4 +333,112 @@ private void assertOverlay(String expectedValues, AnnotationTransformation... tr
|
327 | 333 | }
|
328 | 334 | }
|
329 | 335 | }
|
| 336 | + |
| 337 | + /** |
| 338 | + * Tests that accessing annotations on the {@code Object} class does not attempt |
| 339 | + * to retrieve the (non-existing) superclass. |
| 340 | + */ |
| 341 | + @Test |
| 342 | + public void accessAnnotationsOnTheObjectClass() throws IOException { |
| 343 | + // create a trivial index and an overlay with inherited annotations |
| 344 | + Index index = Index.of(Object.class); |
| 345 | + ClassInfo clazz = index.getClassByName(Object.class); |
| 346 | + |
| 347 | + AnnotationOverlay overlay = AnnotationOverlay.builder(new MyIndexWrapper(index), Collections.emptyList()) |
| 348 | + .inheritedAnnotations() |
| 349 | + .build(); |
| 350 | + |
| 351 | + // Note that if there was a failure, it would be an assertion error from MyIndexWrapper |
| 352 | + assertFalse(overlay.hasAnnotation(clazz, MyInheritedAnnotation.class)); |
| 353 | + assertFalse(overlay.hasAnyAnnotation(clazz, MyInheritedAnnotation.class)); |
| 354 | + assertNull(overlay.annotation(clazz, MyInheritedAnnotation.class)); |
| 355 | + assertEquals(0, overlay.annotationsWithRepeatable(clazz, MyRepeatableAnnotation.class).size()); |
| 356 | + assertEquals(0, overlay.annotations(clazz).size()); |
| 357 | + } |
| 358 | + |
| 359 | + static class MyIndexWrapper implements IndexView { |
| 360 | + private final IndexView delegate; |
| 361 | + |
| 362 | + public MyIndexWrapper(IndexView delegate) { |
| 363 | + this.delegate = delegate; |
| 364 | + } |
| 365 | + |
| 366 | + @Override |
| 367 | + public Collection<ClassInfo> getKnownClasses() { |
| 368 | + return delegate.getKnownClasses(); |
| 369 | + } |
| 370 | + |
| 371 | + @Override |
| 372 | + public ClassInfo getClassByName(DotName className) { |
| 373 | + if (className == null) { |
| 374 | + fail("IndexView#getClassByName should never be invoked with null parameter!"); |
| 375 | + } |
| 376 | + return delegate.getClassByName(className); |
| 377 | + } |
| 378 | + |
| 379 | + @Override |
| 380 | + public Collection<ClassInfo> getKnownDirectSubclasses(DotName className) { |
| 381 | + return delegate.getKnownDirectSubclasses(className); |
| 382 | + } |
| 383 | + |
| 384 | + @Override |
| 385 | + public Collection<ClassInfo> getAllKnownSubclasses(DotName className) { |
| 386 | + return delegate.getAllKnownSubclasses(className); |
| 387 | + } |
| 388 | + |
| 389 | + @Override |
| 390 | + public Collection<ClassInfo> getKnownDirectSubinterfaces(DotName interfaceName) { |
| 391 | + return delegate.getKnownDirectSubinterfaces(interfaceName); |
| 392 | + } |
| 393 | + |
| 394 | + @Override |
| 395 | + public Collection<ClassInfo> getAllKnownSubinterfaces(DotName interfaceName) { |
| 396 | + return delegate.getAllKnownSubinterfaces(interfaceName); |
| 397 | + } |
| 398 | + |
| 399 | + @Override |
| 400 | + public Collection<ClassInfo> getKnownDirectImplementors(DotName interfaceName) { |
| 401 | + return delegate.getKnownDirectImplementors(interfaceName); |
| 402 | + } |
| 403 | + |
| 404 | + @Override |
| 405 | + public Collection<ClassInfo> getAllKnownImplementors(DotName interfaceName) { |
| 406 | + return delegate.getAllKnownImplementors(interfaceName); |
| 407 | + } |
| 408 | + |
| 409 | + @Override |
| 410 | + public Collection<AnnotationInstance> getAnnotations(DotName annotationName) { |
| 411 | + return delegate.getAnnotations(annotationName); |
| 412 | + } |
| 413 | + |
| 414 | + @Override |
| 415 | + public Collection<AnnotationInstance> getAnnotationsWithRepeatable(DotName annotationName, IndexView index) { |
| 416 | + return delegate.getAnnotationsWithRepeatable(annotationName, this); |
| 417 | + } |
| 418 | + |
| 419 | + @Override |
| 420 | + public Collection<ModuleInfo> getKnownModules() { |
| 421 | + return delegate.getKnownModules(); |
| 422 | + } |
| 423 | + |
| 424 | + @Override |
| 425 | + public ModuleInfo getModuleByName(DotName moduleName) { |
| 426 | + return delegate.getModuleByName(moduleName); |
| 427 | + } |
| 428 | + |
| 429 | + @Override |
| 430 | + public Collection<ClassInfo> getKnownUsers(DotName className) { |
| 431 | + return delegate.getKnownUsers(className); |
| 432 | + } |
| 433 | + |
| 434 | + @Override |
| 435 | + public Collection<ClassInfo> getClassesInPackage(DotName packageName) { |
| 436 | + return delegate.getClassesInPackage(packageName); |
| 437 | + } |
| 438 | + |
| 439 | + @Override |
| 440 | + public Set<DotName> getSubpackages(DotName packageName) { |
| 441 | + return delegate.getSubpackages(packageName); |
| 442 | + } |
| 443 | + } |
330 | 444 | }
|
0 commit comments