gpt4 book ai didi

java - Set 中的 GenericsAdd (capture ) 无法应用于 (ConstraintViolation)

转载 作者:行者123 更新时间:2023-12-02 04:12:31 24 4
gpt4 key购买 nike

我有以下代码:

UniqueConstraintViolation violation = new UniqueConstraintViolationImpl();
Set<? extends javax.validation.ConstraintViolation> violations = new HashSet<UniqueConstraintViolation>();
violations.add(violation);
throw new javax.validation.ConstraintViolationException(violations);

其中 UniqueConstraintViolation 是接口(interface)扩展 javax.validation.ConstraintViolationUniqueConstraintViolationImpl 是实现 UniqueConstraintViolation 的类

编译时出现以下错误

java:42: error: no suitable method found for add(UniqueConstraintViolation) violations.add(violation); ^ method Collection.add(CAP#1) is not applicable (argument mismatch; UniqueConstraintViolation cannot be converted to CAP#1 ) method Set.add(CAP#1) is not applicable (argument mismatch; UniqueConstraintViolation cannot be converted to CAP#1 ) where CAP#1 is a fresh type-variable: CAP#1 extends ConstraintViolation from capture of ? extends ConstraintViolation C:\workspace\zcts-travel\crud-rest\src\main\java\ru\zcts\crud\AbstractResource.java:43: error: incompatible types: Set cannot be converted to Set> throw new javax.validation.ConstraintViolationException(violations);

                                                                ^   where CAP#1 is a fresh type-variable:
CAP#1 extends ConstraintViolation from capture of ? extends ConstraintViolation

我只想将我自己的 javax.validation.ConstraintViolation 实现传入 javax.validation.ConstraintViolationException 构造函数中。我该怎么做?

最佳答案

所以有你的限制:

interface UniqueConstraintViolation extends ConstraintViolation<YourClass> {

}

public class UniqueConstraintViolationImpl implements UniqueConstraintViolation {
/* your impl here */
}

这将编译:

UniqueConstraintViolation violation = new UniqueConstraintViolationImpl();

Set<UniqueConstraintViolation> violations = new HashSet<UniqueConstraintViolation>();
violations.add(violation);
throw new ConstraintViolationException(violations);

关于java - Set 中的 GenericsAdd (capture <? extends ConstraintViolation>) 无法应用于 (ConstraintViolation),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33668234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com