Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Generic For Parameterized Class : Unbounded Wildcards vs Raw Type

I have a parameterized interface RestHandler.

public interface RestHandler<T> {
   blah blah blah...
}

And I need to create a class from config using Class.forName. Now I come up with three version , which ALL compiles successfully.

Version 1:

@SuppressWarnings("unchecked")
public static <T> RestHandler<T> create(final String handlerImplFqcn) throws ClassNotFoundException, 
                                                                    IllegalAccessException, 
                                                                    InstantiationException {
    Class<?> handlerClass = Class.forName(handlerImplFqcn);
    return (RestHandler<T>) handlerClass.newInstance();
}

Version 2:

public static RestHandler<?> create(final String handlerImplFqcn) throws ClassNotFoundException, 
                                                                    IllegalAccessException, 
                                                                    InstantiationException {
    @SuppressWarnings("rawtypes")
    Class handlerClass = Class.forName(handlerImplFqcn);
    return (RestHandler<?>) handlerClass.newInstance();
}

Version 3:

public static RestHandler<?> create(final String handlerImplFqcn) throws ClassNotFoundException, 
                                                                    IllegalAccessException, 
                                                                    InstantiationException {
    Class<?> handlerClass = Class.forName(handlerImplFqcn);
    return (RestHandler<?>) handlerClass.newInstance();
}

My question is , why they ALL work and which one would be best practice ?

like image 552
Levin Kwong Avatar asked Feb 28 '26 22:02

Levin Kwong


1 Answers

Version 1, re-written to this:

public static <T extends RestHandler<?>> RestHandler<T> create(final String handlerImplFqcn) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ClassCastException {
    Class<T> handlerClass = (Class<T>) Class.forName(handlerImplFqcn);
    return (RestHandler<T>) handlerClass.newInstance();
}
like image 160
Dave Avatar answered Mar 03 '26 12:03

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!