简体   繁体   English

如何动态定义可用作方法中参数的类?

[英]How to dynamically define the Class that can be used as a parameter in a method?

I have a method that I use to validate a POJO in Spring MVC (this allows me to rely on the @Annotation in the POJO and at the same time validate the POJO programmatically whenever and wherever I want without using the annotation @Valid ). 我有一个方法用于验证Spring MVC中的POJO(这允许我依赖POJO中的@Annotation ,同时在不使用注释@Valid情况下随时随地以编程方式验证POJO)。

In need to pass the Class of the object to the method in order to validate: 需要将对象的Class传递给方法才能验证:

public void validate(Class clazz, Object model, BindingResult result){      
    ...         
    Set<ConstraintViolation<clazz>> constraintViolations=validator.validate((clazz)model);
        for (ConstraintViolation<clazz> constraintViolation : constraintViolations) {
...
}

My code is not correct, since a get the error: 我的代码不正确,因为得到错误:

clazz cannot be resolved to a type clazz无法解析为某种类型

How should I edit my code in order to be able to pass to this method any Class I want? 我应该如何编辑我的代码,以便能够将任何我想要的类传递给此方法?

Thank you 谢谢

You can generify the method 您可以generify方法

public <T> void setList(Class<T> clazz){
  List<T> list = new ArrayList<>();
}

and call it as follows 并将其称为如下

setList(Person.class)

You should use generic: 你应该使用泛型:

public <T> void setList(){
   List<T> list = new ArrayList<>();
}
...
obj.<Man>setList();
obj.<Woman>setList();
obj.<Person>setList();

or 要么

    public <T> void setList(Class<T> clazz){
      List<T> list = new ArrayList<>();
    }
...
    obj.setList(Woman.class);
    obj.setList(Man.class);
    obj.setList(Person.class);

or 要么

public static class MyClass <T> {
    public <T> void setList() {
        List<T> list = new ArrayList<>();
    }
}  
...
new MyClass<Woman>().setList();
new MyClass<Man>().setList();
new MyClass<Person>().setList();

Update: Instead of code 更新:而不是代码

public void validate(Class clazz, Object model, BindingResult result){      
    ...         
    Set<ConstraintViolation<clazz>> constraintViolations=validator.validate((clazz)model);
        for (ConstraintViolation<clazz> constraintViolation : constraintViolations) {
...
}

use code 使用代码

public void <T> validate(Class<T> clazz, Object<T> model, BindingResult result){      
    ...         
    Set<ConstraintViolation<T>> constraintViolations=validator.validate(model);
        for (ConstraintViolation<T> constraintViolation : constraintViolations) {
...
}

I guess you don't need a type and can use an unbound wildcard: 我猜你不需要类型,可以使用未绑定的通配符:

public void validate(Class<?> clazz, Object model, BindingResult result){      
    ...         
    Set<ConstraintViolation<?>> constraintViolations=validator.validate(model);
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
...
}

https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM