Skip to content

Commit 36fdcea

Browse files
manovotnLadicek
authored andcommitted
Fix detection of Object type in AnnotationOverlayImpl methods
1 parent b4143b1 commit 36fdcea

File tree

2 files changed

+124
-5
lines changed

2 files changed

+124
-5
lines changed

core/src/main/java/org/jboss/jandex/AnnotationOverlayImpl.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public final boolean hasAnnotation(Declaration declaration, DotName name) {
6565
}
6666
}
6767

68-
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS) {
68+
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS
69+
&& declaration.asClass().superName() != null) {
6970
ClassInfo clazz = index.getClassByName(declaration.asClass().superName());
7071
while (clazz != null && !DotName.OBJECT_NAME.equals(clazz.name())) {
7172
for (AnnotationInstance annotation : getAnnotationsFor(clazz)) {
@@ -98,7 +99,8 @@ public final boolean hasAnyAnnotation(Declaration declaration, Set<DotName> name
9899
}
99100
}
100101

101-
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS) {
102+
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS
103+
&& declaration.asClass().superName() != null) {
102104
ClassInfo clazz = index.getClassByName(declaration.asClass().superName());
103105
while (clazz != null && !DotName.OBJECT_NAME.equals(clazz.name())) {
104106
for (AnnotationInstance annotation : getAnnotationsFor(clazz)) {
@@ -131,7 +133,8 @@ public final AnnotationInstance annotation(Declaration declaration, DotName name
131133
}
132134
}
133135

134-
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS) {
136+
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS
137+
&& declaration.asClass().superName() != null) {
135138
ClassInfo clazz = index.getClassByName(declaration.asClass().superName());
136139
while (clazz != null && !DotName.OBJECT_NAME.equals(clazz.name())) {
137140
for (AnnotationInstance annotation : getAnnotationsFor(clazz)) {
@@ -178,7 +181,8 @@ public final Collection<AnnotationInstance> annotationsWithRepeatable(Declaratio
178181
}
179182
}
180183

181-
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS) {
184+
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS
185+
&& declaration.asClass().superName() != null) {
182186
ClassInfo clazz = index.getClassByName(declaration.asClass().superName());
183187
while (result.isEmpty() && clazz != null && !DotName.OBJECT_NAME.equals(clazz.name())) {
184188
for (AnnotationInstance annotation : getAnnotationsFor(clazz)) {
@@ -209,7 +213,8 @@ public final Collection<AnnotationInstance> annotations(Declaration declaration)
209213

210214
Collection<AnnotationInstance> result = getAnnotationsFor(declaration);
211215

212-
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS) {
216+
if (inheritedAnnotations && declaration.kind() == AnnotationTarget.Kind.CLASS
217+
&& declaration.asClass().superName() != null) {
213218
result = new ArrayList<>(result);
214219
ClassInfo clazz = index.getClassByName(declaration.asClass().superName());
215220
while (clazz != null && !DotName.OBJECT_NAME.equals(clazz.name())) {

core/src/test/java/org/jboss/jandex/test/AnnotationOverlayTest.java

+114
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
66
import static org.junit.jupiter.api.Assertions.assertNull;
77
import static org.junit.jupiter.api.Assertions.assertTrue;
8+
import static org.junit.jupiter.api.Assertions.fail;
89

910
import java.io.IOException;
1011
import java.lang.annotation.Inherited;
1112
import java.lang.annotation.Retention;
1213
import java.lang.annotation.RetentionPolicy;
1314
import java.util.Arrays;
15+
import java.util.Collection;
16+
import java.util.Collections;
1417
import java.util.List;
1518
import java.util.Map;
19+
import java.util.Set;
1620

1721
import org.jboss.jandex.AnnotationInstance;
1822
import org.jboss.jandex.AnnotationOverlay;
@@ -23,8 +27,10 @@
2327
import org.jboss.jandex.DotName;
2428
import org.jboss.jandex.FieldInfo;
2529
import org.jboss.jandex.Index;
30+
import org.jboss.jandex.IndexView;
2631
import org.jboss.jandex.MethodInfo;
2732
import org.jboss.jandex.MethodParameterInfo;
33+
import org.jboss.jandex.ModuleInfo;
2834
import org.junit.jupiter.api.Test;
2935

3036
public class AnnotationOverlayTest {
@@ -327,4 +333,112 @@ private void assertOverlay(String expectedValues, AnnotationTransformation... tr
327333
}
328334
}
329335
}
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+
}
330444
}

0 commit comments

Comments
 (0)