使用反射打印列表 [英] Printing a list using reflection

查看:67
本文介绍了使用反射打印列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public class Service{ 
       String serviceName;
      //setter and getter
  }

  public class Version{ 
       int VersionID;
      //setter and getter
  }

  public void test(Object list){

        //it shd print the obtained list

   }

列表<服务> list1; //服务是Bean
列表<版本> list2; //版本是Bean
test(list1);
测试(列表2);

现在测试方法将打印获得的列表-(即)如果列表的类型为 Service ,则应该使用其getter打印serviceName.如果列表类型为版本,则应打印版本ID.
不使用接口或抽象类就可以实现这一目标吗?

解决方案

@danLeon到目前为止是最简单的想法(在类中添加toString),前提是您可以访问ServiceVersion.

我不确定您为什么要考虑反射,但是我唯一能想到的就是您想要一种可以与具有单个属性String getter的对象一起使用的东西,然后您将要做一些事情像这样(疯狂的IMO,但它使用反射):

Class clazz = list.get(0).getClass();
Method[] methods = clazz.getDeclaredMethods();
Method onlyStringGetter = null;
for (Method method: methods) {
    String mName = method.getName();
    if (mName.matches("get\w+") {
        if (method.getReturnType().equals(String.class) {
            if (onlyStringGetter != null) thrown new RuntimeException("More than one String getter available");
            onlyStringGetter = method;
        }
    }
}
if (onlyStringGetter == null) throw new RuntimeException("No String getter found for class: " + clazz.getName());
List<String> strings = new ArrayList<String>();
for (Object singleStringAttribObj: list) {
    // some exception handling needed for below
    String result = (String)onlyStringGetter.invoke(singleStringAttribObj);
    strings.add(result);
}
System.out.println(strings);

我还没有编译或尝试过,但这是正确的.肯定需要一些额外的异常处理

 public class Service{ 
       String serviceName;
      //setter and getter
  }

  public class Version{ 
       int VersionID;
      //setter and getter
  }

  public void test(Object list){

        //it shd print the obtained list

   }

List< Service> list1; //Service is a Bean
List< Version> list2; //Version is a Bean
test(list1);
test(list2);

Now the test method shd print the obtained list - (i.e) If the list is of type Service ,then serviceName should be printed using its getter. If the list type is Version versionID should be printed.
Is it possible to achieve this without using Interface or abstract class?

解决方案

@danLeon has the simplest idea so far (adding toString to the classes), assuming you have access to Service and Version.

I'm not sure why you were thinking of reflection, but the only thing I can think of is that you want something that will work with any object that has a single attribute String getter, and then you would do something like this (crazy IMO, but it uses reflection):

Class clazz = list.get(0).getClass();
Method[] methods = clazz.getDeclaredMethods();
Method onlyStringGetter = null;
for (Method method: methods) {
    String mName = method.getName();
    if (mName.matches("get\w+") {
        if (method.getReturnType().equals(String.class) {
            if (onlyStringGetter != null) thrown new RuntimeException("More than one String getter available");
            onlyStringGetter = method;
        }
    }
}
if (onlyStringGetter == null) throw new RuntimeException("No String getter found for class: " + clazz.getName());
List<String> strings = new ArrayList<String>();
for (Object singleStringAttribObj: list) {
    // some exception handling needed for below
    String result = (String)onlyStringGetter.invoke(singleStringAttribObj);
    strings.add(result);
}
System.out.println(strings);

I haven't compiled or tried, but that is approximately right. Definitely some additional exception handling is required

这篇关于使用反射打印列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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