can beanutil get the property of one field with its native type

classic Classic list List threaded Threaded
2 messages Options
kkk
Reply | Threaded
Open this post in threaded view
|

can beanutil get the property of one field with its native type

kkk
Hi: I am using BeanUtil to get the properties of some bean,then put them to
map. However I found that the getProperty() can only return the string
value,I wonder if it can return the native(original) type?

For example:
Bean:

public class Entity {
 private String  name;
 private int   age;
 private List<Date> childs = new ArrayList<Date>();

 public Entity(String name, int age, List<Date> childs) {
  this.name = name;
  this.age = age;
  this.childs = childs;
 }

 public static Entity newInstance() {
  List<Date> list = new ArrayList<Date>();
  list.add(new Date());
  list.add(new Date());
  return new Entity("Beanutil", 23, list);
 }
 //the getter and setter for all the fields }

------------------------------

Then I want to get some field property of it,and I do not exactly know
which fields are requried,so I use it this way:
 public class BeanUtilMain {
 public static void main(String[] args){
  String[] requestPro={"name","childs"}; //this field is specified by clien
  Map<String, Object> map=new HashMap<String, Object>();
  Entity en=Entity.newInstance();
  for(String p:requestPro){
   map.put(p, BeanUtils.getProperty(en, p));
  }
  System.out.println(map);
 } }

Then the map is:

{name=beanutil, childs=Wed Dec 01 12:24:37 CST 2010}

The type of "childs" field is java.util.List,but in the example, it is
converted to java.lang.String.

I have to hold the orignal type of the fields in the map.

Any ideas?
Reply | Threaded
Open this post in threaded view
|

Re: can beanutil get the property of one field with its native type

Brent Worden-2
I think you are just observing ArrayList's toString behavior.
BeanUtils.getProperty() returns the same object that the corresponding
getter method returns.

Try replacing System.out.println(map) with something like
System.out.println(map.get("childs").getClass().getName()) to see what
the actual object type is.

HTH,

Brent.

On Tue, Nov 30, 2010 at 10:53 PM, maven apache <[hidden email]> wrote:

> Hi: I am using BeanUtil to get the properties of some bean,then put them to
> map. However I found that the getProperty() can only return the string
> value,I wonder if it can return the native(original) type?
>
> For example:
> Bean:
>
> public class Entity {
>  private String  name;
>  private int   age;
>  private List<Date> childs = new ArrayList<Date>();
>
>  public Entity(String name, int age, List<Date> childs) {
>  this.name = name;
>  this.age = age;
>  this.childs = childs;
>  }
>
>  public static Entity newInstance() {
>  List<Date> list = new ArrayList<Date>();
>  list.add(new Date());
>  list.add(new Date());
>  return new Entity("Beanutil", 23, list);
>  }
>  //the getter and setter for all the fields }
>
> ------------------------------
>
> Then I want to get some field property of it,and I do not exactly know
> which fields are requried,so I use it this way:
>  public class BeanUtilMain {
>  public static void main(String[] args){
>  String[] requestPro={"name","childs"}; //this field is specified by clien
>  Map<String, Object> map=new HashMap<String, Object>();
>  Entity en=Entity.newInstance();
>  for(String p:requestPro){
>   map.put(p, BeanUtils.getProperty(en, p));
>  }
>  System.out.println(map);
>  } }
>
> Then the map is:
>
> {name=beanutil, childs=Wed Dec 01 12:24:37 CST 2010}
>
> The type of "childs" field is java.util.List,but in the example, it is
> converted to java.lang.String.
>
> I have to hold the orignal type of the fields in the map.
>
> Any ideas?
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]