Class and static method Class.forName() drive me crazy

后端 未结 5 1350
梦毁少年i
梦毁少年i 2020-12-30 08:51

this code doesn\'t compile. I\'m wondering what I am doing wrong:

private static Importable getRightInstance(String s) throws Exception {
 Class

        
相关标签:
5条回答
  • 2020-12-30 09:14

    The issue is Class.forName is a static method with the following definition

    public static Class forName(String className) throws ClassNotFoundException

    Therefore it is not a bound parameter type at all and compiler would definitely throw the cast warning here as it has no way to guarantee the string name of the class would always give you the implementation of the interface.

    If you know for sure that the string name passed into the method would be an implementation of the interface you can use SuppressWarnings annotation. But I dont think ther eis any other cleaner way to use forName with generics

    0 讨论(0)
  • 2020-12-30 09:15

    where Importable is an interface and the string s is the name of an implementing class.

    The compiler can't know that, hence the error.

    Use a cast. It is easier to cast the constructed object (because that is a checked cast), than the class object itself.

    Class<?> c = Class.forName(s);
    Importable i = (Importable) c.newInstance();
    return i;
    
    0 讨论(0)
  • 2020-12-30 09:25

    Something like this might do the trick:

    Class<?> c1 = Class.forName(s);
    Class<? extends Importable> c = c1.asSubclass(Importable.class);
    return c.newInstance();
    

    Beware of a ClassCastException or NoClassDefFound if you pass in the wrong thing. As @polygenelubricants says, if you can figure out some way to avoid Class.forName then so much the better!

    0 讨论(0)
  • 2020-12-30 09:27

    Use a runtime conversion:

    Class <? extends Importable>  c
        = Class.forName (s).asSubclass (Importable.class);
    

    This will bark with an exception at runtime if s specifies a class that doesn't implement the interface.

    0 讨论(0)
  • 2020-12-30 09:31

    Try:

    Class<? extends Importable> klaz = Class.forName(s).asSubclass(Importable.class);
    

    Here are some snippets to illustrate the problems:

    Class<CharSequence> klazz = String.class; // doesn't compile!
    // "Type mismatch: cannot convert from Class<String> to Class<CharSequence>"
    

    However:

    Class<? extends CharSequence> klazz = String.class; // compiles fine!
    

    So for an interface, you definitely need the upper-bounded wildcard. The asSubclass is as suggested by doublep.

    API links

    • <U> Class<? extends U> asSubclass(Class<U> clazz)
      • Casts this Class object to represent a subclass of the class represented by the specified class object. Checks that that the cast is valid, and throws a ClassCastException if it is not. If this method succeeds, it always returns a reference to this class object.

    Related questions

    • What is the difference between <E extends Number> and <Number>?

    See also

    • Java Tutorials/Generics/Subtyping
    • More fun with wildcards
    0 讨论(0)
提交回复
热议问题