繁体   English   中英

无法反映。 异常java.lang.IllegalArgumentException:object不是声明类的实例

[英]Unable to reflect. Exception java.lang.IllegalArgumentException: object is not an instance of declaring class

我有两个不同的类,一个是验证方法。

@Validator
public boolean validIdentifier() throws IllegalArgumentException, IllegalAccessException{
        boolean flag=false;
        Field[] fields=this.data.getClass().getDeclaredFields();
        for(Field field : fields) {
            if (!field.isAccessible()){
                field.setAccessible(true);
            }       
            if (field.getName().endsWith("Identifier") && field.get(data)!=null){
                flag=true;
                field.setAccessible(false);
                break;
            }
            field.setAccessible(false);
        }
        return flag;
}

另一种方法有两种方法可以反映上述验证方法

public void validate(){
  AppValidator validator=new AppValidator(somebean);
  doCommonValidation(validator.getClass());
}

public void doCommonValidation(Class clazz){
        Method[] methods = clazz.getMethods();
        for(Method method:methods) {
            for(Annotation annotation:method.getAnnotations()){
                try {
                    if (annotation instanceof Validator && !(boolean)method.invoke(clazz)){ //exception is here
                        String errorMessage = ((Validator)annotation).message();
                        String display=((Validator)annotation).displayField();
                        if(errorMessage != null) {
                            System.out.println(display+":"+errorMessage);
                        }
                    }
                } catch (IllegalAccessException | IllegalArgumentException
                        | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }

}

上面的代码抛出java.lang.IllegalArgumentException:object不是声明类的实例。

如果我更改doCommonValidation并绕过这样的验证方法,一切正常。

public void validate() {
        try {
            AppValidator validator=new AppValidator(someBean);
            Method[] methods = validator.getClass().getMethods();           
            for(Method method:methods) {
                for(Annotation annotation:method.getAnnotations()){
                    if (annotation instanceof ValidatingMethod && !(boolean)method.invoke(validator)){
                        String errorMessage = ((ValidatingMethod)annotation).message();
                        String display=((ValidatingMethod)annotation).displayField();
                        if(errorMessage != null) {
                            System.out.println(display+":"+errorMessage);
                        }
                    }
                }
            }

        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
            e.printStackTrace();
            return new HashMap<String, String>();
        }
}

问题是我确实需要能够插入几个不同的验证类。 如果我明确表示他们然后我必须写重复代码,其中唯一的差异是我反思的类。

我不明白为什么在第一种情况下我得到了例外而在第二种情况下我没有。 有任何想法吗?

谢谢

在错误的情况下执行此行时:

if (annotation instanceof Validator && !(boolean)method.invoke(clazz))

您需要向invoke方法传递一个表示调用该方法的对象的参数,因为它是一个实例方法。 此参数应为由作为doCommonValidation(Class)方法的参数传递的Class实例表示的类型。 你传递的是Class<T>一个实例,而不是一个实例。

我想到了。 最糟糕的是答案总是在我面前!

我需要在对象而不是类上调用方法。 因此,验证方法应如下所示。

public void validate(){
  AppValidator validator=new AppValidator(somebean);
  doCommonValidation(validator);
}

和doCommonValidation方法应如下所示

public void doCommonValidation(Object obj){
    Method[] methods = obj.getClass().getMethods();
    for(Method method:methods) {
        for(Annotation annotation:method.getAnnotations()){
        try {
            if (annotation instanceof Validator && !(boolean)method.invoke(obj)){ 
            String errorMessage = ((Validator)annotation).message();
            String display=((Validator)annotation).displayField();
            if(errorMessage != null) {
                System.out.println(display+":"+errorMessage);
            }
            }
        } catch (IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
            e.printStackTrace();
        }
        }
    }
}

暂无
暂无

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

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