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

我正在尝试对不同的服务实现进行通用处理,但我经常收到“由于原始类型而导致未检查的调用”错误。


我尝试了几种实现,但不太明白这里的问题是什么。


我有以下模型:


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 个模型的实现:


@Service

public 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;

  }

}

还有菠萝:


@Service

public 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 进行操作:


@Service

public 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);

  }

}


慕妹3146593
浏览 73回答 1
1回答

POPMUISE

问题是您Class在服务定义中使用原始类型。要解决此问题,您可以使用有界通配符类型 ?,如下所示:class FruitServices {&nbsp; private Map<Class<? extends Fruit>, FruitCheckService<? extends Fruit>> beansMap;&nbsp; public FruitServices(List<FruitCheckService<? extends Fruit>> fruitCheckServices) {&nbsp; &nbsp; beansMap = new HashMap<>();&nbsp; &nbsp; fruitCheckServices&nbsp; &nbsp; &nbsp; .forEach(&nbsp; &nbsp; &nbsp; &nbsp; fruitCheckService -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<? extends Fruit> implementation = fruitCheckService.getImplementation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beansMap.put(implementation, fruitCheckService);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; );&nbsp; }&nbsp; public FruitCheckService<? extends Fruit> getFruitCheckService(Class<? extends Fruit> clazz) {&nbsp; &nbsp; return beansMap.get(clazz);&nbsp; }}对于课堂来说,BusinessService事情会变得更加棘手。首先,有必要制定compareVitamins一个通用方法来确保两个水果属于同一类型。但即使那样,我也不相信你可以避免 unchecked cast from the result,getFruitCheckService因为,由于FruitServices必须处理不同类型水果的服务,你不能为 的返回值提供精确的类型参数getFruitCheckService。class BusinessService {&nbsp; &nbsp; private FruitServices fruitServices;&nbsp; &nbsp; public <T extends Fruit> void compareVitamins(T one, T two) {&nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; &nbsp; &nbsp; FruitCheckService<T> fruitCheckService =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (FruitCheckService<T>) fruitServices.getFruitCheckService(one.getClass());&nbsp; &nbsp; &nbsp; &nbsp; List<String> result = fruitCheckService.compareVitaminsFromDifferentCountries(one, two);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java