[weld-dev] Proxies.TypeInfo.getSuperInterface() question

Matej Novotny manovotn at redhat.com
Fri Mar 6 07:24:28 EST 2020


Hi,

so I think (I wasn't there at the time so I cannot really remember it :)) the name `getSuperInterface` or `getSuperClass`on `Proxies.TypeInfo` class comes from the fact that these method return you a class/interfaces that is a direct superclass for the proxy class you are creating.
E.g. this is used in places like `ProxyFactory` where you would use `typeInfo.getSuperInterface()` as a means of saying "give me what will be the direct superclass of the proxy I am now creating".

You want your proxies to be based on the most concrete type of the bean for apparent reasons - you want the proxy to match all possible injection points, so it has to fit into all types, which is exactly what the most specific type will do.
So in your 2 interface scenario with Square and Rectangle, you will always want to base proxy on Square so that the proxy has both types; therefore these methods will return you `Square` - a super interface for the proxy you are doing.

That's the way I read the code at least, your mileage may vary :)
Matej

----- Original Message -----
> From: "Laird Nelson" <ljnelson at gmail.com>
> To: weld-dev at lists.jboss.org
> Sent: Friday, March 6, 2020 2:51:02 AM
> Subject: [weld-dev] Proxies.TypeInfo.getSuperInterface() question
> 
> 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/10a1d11af8c815a2a4a8fc5a4061698215e602b0/impl/src/main/java/org/jboss/weld/util/Proxies.java#L86
> .
> 
> 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
> 
> _______________________________________________
> weld-dev mailing list
> weld-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/weld-dev



More information about the weld-dev mailing list