为什么我的编译器不能转换它?

发布于 2024-11-07 19:11:58 字数 1220 浏览 4 评论 0原文

错误:

Type mismatch: cannot convert from Set<ConstraintViolation<capture#1-of ?>> to Set<ConstraintViolation<Class<?>>>

我的代码:

public class validateValue(Class<?> beanType, String propertyName, Object value, Class<?>... groups){
    TraversableResolver tr = new MyTraversableResolver(); 
    Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
    final Set<ConstraintViolation<Class<?>>> constraintViolations = validator.validateValue(beanType, propertyName, value, groups);
}

private class MyTraversableResolver  implements TraversableResolver {

    public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
      return traversableObject == null || Hibernate.isInitialized(traversableObject);
      }
      public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
          return true;
      }
  }

Error:

Type mismatch: cannot convert from Set<ConstraintViolation<capture#1-of ?>> to Set<ConstraintViolation<Class<?>>>

My code:

public class validateValue(Class<?> beanType, String propertyName, Object value, Class<?>... groups){
    TraversableResolver tr = new MyTraversableResolver(); 
    Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
    final Set<ConstraintViolation<Class<?>>> constraintViolations = validator.validateValue(beanType, propertyName, value, groups);
}

private class MyTraversableResolver  implements TraversableResolver {

    public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
      return traversableObject == null || Hibernate.isInitialized(traversableObject);
      }
      public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
          return true;
      }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-11-14 19:11:58

我认为问题是你的编译器不知道你的 beanType 变量的类型与集合匹配。尝试这样的事情(代码可能需要稍微调整一下)

public <T> class validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups)

 final Set<ConstraintViolation<Class<?>>>

然后更改

 final Set<ConstraintViolation<Class<T>>>

I think the problem is your compiler doesn't know that the type of your beanType variable matches the set. Try something like this (code may need to be tweaked a bit)

public <T> class validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups)

Then changing:

 final Set<ConstraintViolation<Class<?>>>

to

 final Set<ConstraintViolation<Class<T>>>
雨的味道风的声音 2024-11-14 19:11:58

@JustinKSU 将通配符 (?) 更改为类型 arg (T) 是正确的。另一个问题是 validator.validateValue(ClassbeanType, String propertyName, Object value, Class... groups) 不会返回一组对该类型的约束违规类,这将是:

Set<ConstraintViolation<Class<T>>>

...而是返回类型上的一组约束违规:

Set<ConstraintViolation<T>>

这是一个细微的差别,但如果您仔细检查错误消息并计算尖括号,就会很明显。

所以,你的代码应该如下所示。为了示例的目的,我简化了验证器的创建。我还添加了一个返回值,因为我假设您想对结果做一些事情。

public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) {
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    final Set<ConstraintViolation<T>> constraintViolations = validator.validateValue(beanType, propertyName, value, groups);
    return constraintViolations;
}

@JustinKSU is correct about changing the wildcard (?) to a type arg (T). The other issue is that validator.validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) doesn't return a set of constraint violations on the type's class, which would be:

Set<ConstraintViolation<Class<T>>>

...but rather it returns a set of constraint violations on the type:

Set<ConstraintViolation<T>>

It's a subtle difference to see, but it's noticeable if you check the error message closely and count the angle brackets.

So, your code should be as below. I simplified the validator creation for purposes of the example. I also added a return value since I assume you want to do something with the results.

public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) {
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    final Set<ConstraintViolation<T>> constraintViolations = validator.validateValue(beanType, propertyName, value, groups);
    return constraintViolations;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文