I am reading Weld code to understand deeply how proxies work.
While doing this, I ran across the Proxies.TypeInfo class. It has a method
called getSuperInterface().
This method iterates over a set of interfaces and selects the most specific
one (somewhat surprisingly to me; I would have expected the most general
one).
The method is very short so here it is in its entirety, reformatted:
public Class<?> getSuperInterface() {
if (interfaces.isEmpty()) {
return null;
}
Iterator<Class<?>> it = interfaces.iterator();
Class<?> superclass = it.next();
while (it.hasNext()) {
Class<?> clazz = it.next();
if (superclass.isAssignableFrom(clazz)) { // XXX Is this backwards?
superclass = clazz;
}
}
return superclass;
}
For example, if there are exactly two interfaces in the set, Square.class
and Rectangle.class, and Square extends Rectangle, it would seem that
Square.class will be selected.
(My reading: Pretend Square.class is the first item in the iterator. It
gets assigned to superclass. Then clazz gets the next (and last) item,
Rectangle.class. Then we check to see if (effectively)
Square.class.isAssignableFrom(Rectangle.class), which will return false.
So superclass remains Square.class and is returned.
(Or pretend that Rectangle.class was the first item in the iterator. It
gets assigned to superclass. Then clazz gets Square.class. Then we check
to see if Rectangle.class.isAssignableFrom(Square.class), which returns true,
so superclass gets reassigned to Square.class, and is then returned. Hence
Square.class, the most *specific* subtype in the set, is returned in both
cases.)
The reason this surprised me is the name of this method implies that it
should select the most *general*, e.g. Rectangle.class in my example.
That's usually what "super" means.
See
https://github.com/weld/core/blob/10a1d11af8c815a2a4a8fc5a4061698215e602b...
.
Is that line "backwards"? Or is it just a badly-named method (should it
have been named getMostSpecificInterface())? Or…?
I understand this code is old and works and so my apologies if I'm being
stupid in some way. I just like to understand things.
Best,
Laird