Mark Eastwood
Mark Eastwood

Reputation: 85

Java get bean value

Given the following:

public class Person {
    private Car car;
    // .. bean stuff
}

.

public class Car {
    private CarStuff carStuff;
    // .. bean stuff
}

I can use BeanUtils to set the "car" property of Person:

BeanUtils.setProperty(person, "car", theirCar);

Ok that was super easy. Now how do I get "car" out by its name?

BeanUtils.getProperty(person, "car")

Will return a String, but Car is not a String

I have tried:

Map<String, ? extends Object> props = new HashMap<>();
BeanUtils.populate(person, props);

but there is no "car" entry, and investigating the documentation, the output (while promisingly of type Object) would still be either String or String[]

My classes all have proper bean getter and setter methods. How can I obtain the value?

Upvotes: 2

Views: 4629

Answers (1)

KKishore
KKishore

Reputation: 452

You can try using PropertyUtils.getProperty(Object, String) instead

Upvotes: 5

Related Questions