java - Unchecked cast warning when return generic implementation based on Class<> argument -
i have following setup:
@override public <t> optional<endpointprovider<t>> getprovider(class<t> providedclass) { if(providedclass.isassignablefrom(recommendationcategory.class)){ return optional.of((endpointprovider<t>) new recommendationcategoryprovider()); } return optional.empty(); } private class recommendationcategoryprovider implements endpointprovider<recommendationcategory> {
and compiler giving me unchecked cast warning. think checking providedclass.isassignablefrom(recommendationcategory.class) prevent unchecked cast warning.
is there way not have warning in setup?
this way come it. won't show warnings.
public static <t> endpointprovider<t> getprovider(class<t> klass) { if (klass.isassignablefrom(recommendationcategory.class)) { return new endpointwrapper<t>(klass.cast(new recommendationcategory())); } return null; } private static class endpointwrapper<x> implements endpointprovider<x> { public endpointwrapper(x wrapper) { } } private static class recommendationcategory { } private static interface endpointprovider<t> { }
Comments
Post a Comment