jeudi 16 avril 2015

Easy way to access POJO properties in groovy?

I'm new to groovy and still learning my way around. Is there an easy way to get POJO property values in groovy using dot notation? For example, I have the following POJO:



public class MyPOJO {
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


In groovy, I would like to get the value of the name field as follows:



def doSomething (MyPOJO mpj) {
def name = mpj.name
// do something
}


The above does not work. I know that i could still use java getters and setters, but I would like to be able to get to a point where I can dynamically pull pojo values like so:



def doSomething (MyPOJO mpj, String propertyName) {
def propertyValue = mpj.'${propertyName}'
// do something
}


I'm trying to avoid using java reflection. Thanks for the help!






Aucun commentaire:

Enregistrer un commentaire