21

Is it possible in Java to get class property value by its name? for example, i have class like

public class Test {
    private String field;
    public String getField() {...}
    public void setField() {...}
}

and another class with Map

public class Main {
    private static final Map<String, Long> map = new HashMap<String, Long>();
    static {
        map.put("field", new Long(1));
    }
    public void doSth() {
    Set<String> keys = map.keySet();
    Test t = new Test();
    for (String key : keys) {
    //t.getPropertyValueByName(key); ?
    }
    }
kassie
  • 727
  • 4
  • 11
  • 24

7 Answers7

22

You can use some of the Libraries that offer property-based access. I think the most known and used is beanutils. You can find one good example of the beanutils "in action" here. Some sample code:

A someBean = new A();

// access properties as Map
Map<String, Object> properties = BeanUtils.describe(someBean);
properties.set("name","Fred");
BeanUtils.populate(someBean, properties);

// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny"); 
Dan94
  • 346
  • 1
  • 3
  • 3
    There is a problem with `BeanUtils.getProperty` : it returns a string. It's not clear what to do if you have a property of another type. – Alexey Nov 28 '17 at 12:33
  • Try to get it in a Object reference, eg `Object oldname = BeanUtils.getProperty(someBean,"name");` in case of int, bool it will take as a wrapper class object. and then try to check the type for further operation you need – Koushik Chatterjee Jul 02 '18 at 15:03
7

Yes. You can replace the commented out line with t.getClass().getField(map.get(key)).get(t). which will retrieve the value of the field on t.

bjc2406
  • 85
  • 2
2

bjc2406's answer works fine as long as the field(s) in question are accessible:

t.getClass().getField(map.get(key)).get(t)

If you can't reasonably make it public, reflection and other field access APIs should get the job done: How do I read a private field in Java?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Danny Salvadori
  • 51
  • 1
  • 1
  • 4
2

Apart from
String org.apache.commons.beanutils.BeanUtils.getProperty(object, propertyName)
there is
Object org.apache.commons.beanutils.PropertyUtils#getProperty(object, propertyName)
which does not turn value into String.

This might be helpful to preserve integer, decimal and boolean types.

fyrkov
  • 2,245
  • 16
  • 41
  • Many Thanks! Small correction though : Object org.apache.commons.beanutils.PropertyUtils.getProperty(object, propertyName) – Vishnoo Rath Oct 21 '20 at 10:56
0

The question is how often do the properties change? Are the constants, or will it depend on the situation?

If it is the latter case, which it often is, you want the properties in an external file. The standard java properties api is great for that: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html.

If you use a framework like spring, it will also come with standard ways to deal with properties. Look in their documentation.

Jesse van Bekkum
  • 1,466
  • 3
  • 15
  • 24
0

This solution work :

// access individual properties
String oldname = BeanUtils.getProperty(someBean,"name");
BeanUtils.setProperty(someBean,"name","Barny"); 

This solution variante preserve object's type

Object a = BeanUtilsBean.getInstance().getPropertyUtils().getNestedProperty(someBean, "name");
if(a instanceof Boolean) {
...
}
Gawel
  • 21
  • 1
  • 4
-3

You can also go for Properties.java class : http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

It does same job.

Lokesh
  • 7,810
  • 6
  • 48
  • 78
  • The OP is not asking for a property from a properties file, but for the value of a JavaBean property. – Stefan Haberl Jun 11 '15 at 11:12
  • @StefanHaberl: I am suggesting an alternate solution. What OP is trying to achieve, can also be achieved via Properties.java – Lokesh Jun 11 '15 at 13:36