我怎么能叫的getter / setter标注的相关定制标注的财产? [英] How can I call getter/setter for property marked with custom annotation?

查看:230
本文介绍了我怎么能叫的getter / setter标注的相关定制标注的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建用于解析一个excel文件的自定义编组工具。我想知道我怎么能先找到一个自定义注解所有属性(这需要考虑继承考虑,所以比getDeclaredFields更多),然后根据我使用调用相应的getter或setter什么方法。现在,我只是专注于制定者。

当前code:

 私人< T> ŧfindAnnotations(类< T> clazz所)
 {
    ŧ的obj = NULL;    注释[] =注释clazz.getAnnotations();    对于(译注注解:注解)
    {
        如果(annotation.annotationType()== ExcelColumn.class)
        {
            如果(OBJ == NULL)
            {
                尝试{
                    物镜= clazz.newInstance();
                }赶上(IllegalAccessException | InstantiationException E){
                }
            }
            //注释发现
            //的属性调用setter方法
            //使用ChildTest样品级呼叫的setName并设置父名
            //用绳子previously解析。
            //即obj.setName()和obj.setParentName()
        }
    }
    返回OBJ;
}

样品类别:

 公共类ChildTest扩展父{
     @ExcelColumn
     私人字符串名称;
     公共无效setname可以(字符串名称){
          this.name =名称;
     }
}公共类父{
     @ExcelColumn
     私人字符串parentName;
     公共无效setParentName(字符串parentName){
          this.parentName = parentName;
     }
 }


解决方案

您可以使用反射来调用这些方法。

  obj.getClass()实现getMethod(的setName,为String.class).invoke(OBJ,名)。
。obj.getClass()实现getMethod(setParentName,为String.class).invoke(OBJ,父名);

I am currently creating a custom marshalling tool for parsing an excel file. I would like to know how I can first find all properties with a custom annotation (this needs to take inheritance into account, so more than getDeclaredFields), then based on what method I am using call the corresponding getter or setter. Right now, I am just focusing on the setter.

Current Code:

private <T> T findAnnotations(Class<T> clazz)
 {
    T obj  = null;

    Annotation[] annotations = clazz.getAnnotations();

    for(Annotation annotation : annotations)
    {
        if(annotation.annotationType() == ExcelColumn.class)
        {
            if(obj == null)
            {
                try {
                    obj = clazz.newInstance();
                } catch (IllegalAccessException | InstantiationException e) {
                }
            }
            //annotation found
            //call setter of property
            //using ChildTest sample class call setname and set parent name 
            //with string previously parsed.
            // i.e obj.setName("") and obj.setParentName("") 
        }
    }
    return obj;
}

Sample Class:

public class ChildTest extends Parent{
     @ExcelColumn
     private String name;
     public void setName(String name) {
          this.name = name;
     }
}

public class Parent{
     @ExcelColumn
     private String parentName;
     public void setParentName(String parentName) {
          this.parentName= parentName;
     }
 }

解决方案

You can use reflection to invoke those methods.

obj.getClass().getMethod("setName", String.class).invoke(obj, "Name");
obj.getClass().getMethod("setParentName", String.class).invoke(obj, "Parent name");

这篇关于我怎么能叫的getter / setter标注的相关定制标注的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆