Add method to find extending interfaces of target interface
geoand opened this issue · comments
I would like to be able to (efficiently) look up all the interfaces that extends a target interface.
Say for example that I have
public interface SomeInterface {
}
and there is an interface that extends is:
public interface SomeExtendingInterface extends SomeInterface {
}
Neither index.getAllKnownSubclasses()
nor index.getAllKnownImplementors()
covers the aforementioned usecase so I am forced do the following (inefficient) lookup:
List<ClassInfo> getAllInterfacesExtending(DotName target, IndexView index) {
List<ClassInfo> result = new ArrayList<>();
Collection<ClassInfo> knownClasses = index.getKnownClasses();
for (ClassInfo clazz : knownClasses) {
if (!Modifier.isInterface(clazz.flags())) {
continue;
}
List<DotName> interfaceNames = clazz.interfaceNames();
boolean found = false;
for (DotName interfaceName : interfaceNames) {
if (interfaceName.equals(target)) {
found = true;
break;
}
}
if (found) {
result.add(clazz);
}
}
return result;
}
@Ladicek I think that this one would deserve your attention ;-)
Good point, indeed neither getAllKnownSubclasses
nor getAllKnownImplementors
can be used for this. Adding something like getAllKnownSubinterfaces
is certainly a good idea.
Here's a funny thing: getKnownDirectImplementors
returns direct subinterfaces, but getAllKnownImplementors
doesn't return interfaces at all. This is inconsistent and counter-intuitive, but I hesitate to unify that. That would be a pretty serious behavioral breakage that can't be caught during compilation.
I still intend to add getKnownDirectSubinterfaces
and getAllKnownSubinterfaces
(actually working on that right now), but I'd welcome any opinions about the above.
Sounds reasonable.
Hm, another funny thing: the javadoc of getAllKnownImplementors()
was pretty clear that it only returns classes, not interfaces, but for some reason "interfaces" was replaced with "methodParameters" in 44d77cf#diff-cf6e0f736d92b7d69934c1308f902a9c5440c9075761997bc05a6bab7e0a5c6b. The same applies to getKnownDirectImplementors()
. So it was intentional.
@n1hility can tell us more ;-).
I don't think that methodParameters
replacement was intentional, I'm fixing that as part of this work. (Thanks for pointing to the commit. I found a few more places that need fixing.)
I agree that it was intentional for getKnownDirectImplementors
to return interfaces and getAllKnownImplementors
to not return interfaces, I just find it very weird and unexpected. I feel a strong urge to fix that in one way or another, but I'm not exactly sure about it :-)
I have an implementation over in #184. It doesn't change existing behavior, though I could be persuaded otherwise :-)
Done in #184.