为了账号安全,请及时绑定邮箱和手机立即绑定

如何处理通用接口实现并避免@SuppressWarnings?

如何处理通用接口实现并避免@SuppressWarnings?

慕妹3146593 2023-03-23 16:27:42
我正在尝试对不同的服务实现进行通用处理,但我经常收到“由于原始类型而导致未检查的调用”错误。我尝试了几种实现,但不太明白这里的问题是什么。我有以下模型:public abstract class Fruit {  public List<String> vitamins;  public String originCountry;  // getters and setters omitted}具体实现如下:public class Kiwi extends Fruit {}public class Pineapple extends Fruit {}我有以下界面:public interface FruitCheckService<T extends Fruit> {  List<String> compareVitaminsFromDifferentCountries(T firstFruit, T secondFruit);  Class<T> getImplementation();}对于上面给出的 2 个模型的实现:@Servicepublic class KiwiCheckServiceImpl implements FruitCheckService<Kiwi> {  @Override  public List<String> compareVitaminsFromDifferentCountries(Kiwi firstFruit, Kiwi secondFruit) {    // some implementation    return new ArrayList<>();  }  @Override  public Class<Kiwi> getImplementation() {    return Kiwi.class;  }}还有菠萝:@Servicepublic class PineappleCheckServiceImpl implements FruitCheckService<Pineapple> {  @Override  public List<String> compareVitaminsFromDifferentCountries(Pineapple firstFruit, Pineapple secondFruit) {    // some implementation    return new ArrayList<>();  }  @Override  public Class<Pineapple> getImplementation() {    return Pineapple.class;  }}我有以下类正在使用不同的 bean 进行操作:@Servicepublic class FruitServices {  private Map<Class, FruitCheckService> beansMap;  @Autowired  public FruitServices(List<FruitCheckService> fruitCheckServices) {    beansMap = new HashMap<>();    fruitCheckServices      .forEach(        fruitCheckService -> {          Class implementation = fruitCheckService.getImplementation();          beansMap.put(implementation, fruitCheckService);        }      );  }  public FruitCheckService getFruitCheckService(Class clazz) {    return beansMap.get(clazz);  }}
查看完整描述

1 回答

?
POPMUISE

TA贡献1765条经验 获得超5个赞

问题是您Class在服务定义中使用原始类型。要解决此问题,您可以使用有界通配符类型 ?,如下所示:


class FruitServices {


  private Map<Class<? extends Fruit>, FruitCheckService<? extends Fruit>> beansMap;



  public FruitServices(List<FruitCheckService<? extends Fruit>> fruitCheckServices) {


    beansMap = new HashMap<>();


    fruitCheckServices

      .forEach(

        fruitCheckService -> {

          Class<? extends Fruit> implementation = fruitCheckService.getImplementation();

          beansMap.put(implementation, fruitCheckService);

        }

      );

  }


  public FruitCheckService<? extends Fruit> getFruitCheckService(Class<? extends Fruit> clazz) {

    return beansMap.get(clazz);

  }

}

对于课堂来说,BusinessService事情会变得更加棘手。首先,有必要制定compareVitamins一个通用方法来确保两个水果属于同一类型。但即使那样,我也不相信你可以避免 unchecked cast from the result,getFruitCheckService因为,由于FruitServices必须处理不同类型水果的服务,你不能为 的返回值提供精确的类型参数getFruitCheckService。


class BusinessService {


    private FruitServices fruitServices;


    public <T extends Fruit> void compareVitamins(T one, T two) {


        @SuppressWarnings("unchecked")

        FruitCheckService<T> fruitCheckService = 

            (FruitCheckService<T>) fruitServices.getFruitCheckService(one.getClass());


        List<String> result = fruitCheckService.compareVitaminsFromDifferentCountries(one, two);

    }

}


查看完整回答
反对 回复 2023-03-23
  • 1 回答
  • 0 关注
  • 72 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信