Added by Alex Ruiz, last edited by Alex Ruiz on Nov 25, 2009  (view change)

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.

Accessing JavaBean Properties

Let's assume we have a simple class Person that defines the following property:

class Person {

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String newName) {
    name = newName;
  }
}

The following sections illustrate property access using FEST-Reflect. We will assume that we have the variable person of type Person. We are also going to assume the following static import:

import static org.fest.reflect.core.Reflection.property;

Reading the value of a property

The following code listing is equivalent to calling getName():

String name = property("name").ofType(String.class)
                              .in(person)
                              .get();

Setting the value of a property

The following code listing is equivalent to calling setName("Leia"):

property("name").ofType(String.class)
                .in(person)
                .set("Leia");

See Also