0

I have a code like below where the Factory interface is implemented by two sub classes, in spring xml the bean defines itself as an object of one of the class(OrangeFactory in below example) while in getBean it tries to cast as AppleFactory, shouldn't getBean throw Class cast Exception during runtime in this case?

But Strangely it doesn't - what am I missing here?

public interface Factory<K, V> {
    V get(K key);
}
public class AppleFactory implements Factory<String, Map<FCName, Boolean>> {
   
 @Override
    public Map<FCName, Boolean> get(String identifier) {
           //doSomething and
      return <some map>;
 }
}
public class OrangeFactory<K, V> implements Factory<K, V> {

 @Override
    public V get(K key) {
         //doSomething and
    return <some map>;
 }
}

and XML:

<bean id="appleFactory" class="OrangeFactory">  
</bean>

Below method should throw ClassCast Exception during runtime? - But it seem to work fine...

public Factory<String, Map<FCName, Boolean>> getAppleFactory() {
        return getBean("appleFactory", AppleFactory.class);
}
Golu
  • 39
  • 5
  • Why do you think, it "should throw..."? (If you have more information, please share! If not: i don't agree!) – xerx593 Oct 16 '22 at 08:36
  • You're most probably experiencing [type erasure](https://stackoverflow.com/q/339699/5515060) and some type inference shenanigans. The `getBean` method returns some type `T` which the compiler resolves as `Factory>` and because `AppleFactory` is a subtype of the former it still works. Also the `getBean` method (probably) does an unchecked cast (i.e. `return (T) someObj;`) which is just ignored at runtime – Lino Oct 16 '22 at 08:42
  • The code you've shown doesn't contain any casts. – tgdavies Oct 16 '22 at 09:19
  • @tgdavies , is it not trying to cast the `appleFactory` object (which is of type OrangeFactory in the xml) to `AppleFactory.class` in the getBean method? – Golu Oct 16 '22 at 19:58
  • @xerx593 , what more you need? and why you don't agree it show throw Classcast for the above code - can you elaborate/explain a bit? Will help me understand. – Golu Oct 16 '22 at 19:59
  • i don't agree, because (with this information): Why should it throw!??? – xerx593 Oct 16 '22 at 20:35
  • `String` is (obviously) legal as `K` ..and `Map` legal `V`, please elaborate: why not – xerx593 Oct 16 '22 at 20:38
  • `AppleFactory` is just confusing..in OP – xerx593 Oct 16 '22 at 20:39
  • Have a look at the source of getBean and see whether there's a cast. – tgdavies Oct 16 '22 at 20:59
  • @tgdavies this is ths source - it is trying to cast the object from xml into T in this case OrangeFactory object to AppleFactory object? So should throw ClasscastException as both are separate classes (and OrangeFactory is not a child of AppleFactory)? Below is the code ------> `public T getBean(String beanId, Class clazz) { try { return (T) springContext.getBean(beanId); } catch (Exception e) { throw new RuntimeException("Exception while loading beanId=" + beanId, e); } }` – Golu Oct 17 '22 at 02:39

0 Answers0