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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -