Finding Getters and Setters with Java Reflection

Java Reflection provides a set of useful APIs that help you find methods in Java classes. You can find all the methods in a class or a method with a specific name and signature. Surprisingly, there are no APIs to determine if a Java method is a getter or a setter.

In this post, I’ll develop a useful method that returns a list of getters and setters for any Java class. I’ll also create separate methods that inspect Java Reflection Method objects and determine if the method they represent is a getter or a setter.

A Class with Getters and Setters

We’ll need a class to work with, so here’s a Person class with getters and setters.

public class Person {
   private String name;
   private int age;
   private boolean deceased;
   public Person() { }
   public Person(String name, int age) {
      this.name = name;
      this.age = age;
      deceased = false;
   }
   public String getName() { return name; }
   public void setName(String name) {
      this.name = name;
   }
   public int getAge() { return age; }
   public void setAge(int age) {
      this.age = age;
   }
   public boolean isDeceased() {
      return deceased;
   }
   public void setDeceased(boolean deceased) {
      this.deceased = deceased;
   }
   public boolean isTeenager() {
      return (age > 12 && age < 20) ? true : false;
   }
   @Override
   public String toString() {
      return name + " " + age; }

The Person class includes getters and setters that follow typical naming protocols. This includes the “set” and “get” methods for field data, as well as “is” methods with booleans. The toString() method in line 29 (which is not a getter or setter) is overridden to return a string with a Person’s name and age.

Getter Methods

Before we move on, let’s review what makes a Java method a getter.

First of all, getter methods must be public. A getter method has no arguments and its return type must be something other than void.

Getters also have naming conventions. The name of a getter method begins with “get” followed by an uppercase letter. In the Person class, examples are getName() in line 11 and getAge() in line 15.

The name of a getter method can also begin with “is” followed by an uppercase letter. These getter methods do not have arguments and must return booleans. Examples are isDeceased() in line 19 and isTeenAger() in line 25 from the Person class.

Setter Methods

What makes a method a setter?

Setter methods must be public and have only one argument. Their return type must be void. Setter methods also have names that begin with “set“, followed by an uppercase letter.

Examples in the Person class are setName() in line 12, setAge() in line 16, and setDeceased() in line 22.

The findGettersSetters() Method

Now we’re ready to develop several useful Java methods that find getter and setter methods in Java classes.

The first one is called findGettersSetters().

static ArrayList<Method> findGettersSetters(Class<?> c) {
   ArrayList<Method> list = new ArrayList<Method>();
   Method[] methods = c.getDeclaredMethods();
   for (Method method : methods)
      if (isGetter(method) || isSetter(method))
         list.add(method);
   return list;
}

This method has a Class<?> object argument and returns an ArrayList of Method objects representing the getters and setters in that class. The call to c.getDeclaredMethods() generates an array of class Method objects. We loop through this array and add a method to the ArrayList if it’s a getter or a setter.

The isGetter() and isSetter() methods inside the for loop determine if their Method object argument is a getter or setter, respectively. We’ll show you the code for these methods shortly.

Let’s try out findGettersSetters() with the Person class.

public static void main(String[] args) {
    for (Method method :  findGettersSetters(Person.class))
       System.out.println(method);
}

Here’s the output.

public java.lang.String Person.getName()
public void Person.setName(java.lang.String)
public int Person.getAge()
public void Person.setAge(int)
public boolean Person.isDeceased()
public void Person.setDeceased(boolean)
public boolean Person.isTeenager()

The isGetter() Method

Our findGettersSetters() method uses isGetter() to find getters. This method has a Method object argument and returns a boolean (true if the method is a getter).

public static boolean isGetter(Method method) {
   if (Modifier.isPublic(method.getModifiers()) &&
      method.getParameterTypes().length == 0) {
         if (method.getName().matches("^get[A-Z].*") &&
            !method.getReturnType().equals(void.class))
               return true;
         if (method.getName().matches("^is[A-Z].*") &&
            method.getReturnType().equals(boolean.class))
               return true;
   }
   return false;
}

The first if statement makes sure the method is public and the method has no arguments.

The second if statement uses a regular expression to verify the method’s name begins with “get” followed by an uppercase letter. It also makes sure that the method’s return type is something other than void.

The third if statement verifies the method’s name begins with “is” followed by an uppercase letter and that the method’s return type is boolean.

The isSetter() Method

Our findGettersSetters() method also uses isSetter() to find setters. The implementation of this method is done with four steps in a single return statement.

public static boolean isSetter(Method method) {
   return Modifier.isPublic(method.getModifiers()) &&
      method.getReturnType().equals(void.class) &&
         method.getParameterTypes().length == 1 &&
            method.getName().matches("^set[A-Z].*");
}

Step one makes sure the method is public. Step two verifies the method’s return type is void. Step three insures the method has one argument. The last step verifies that the method’s name begins with “set” followed by an uppercase letter.

Wrap Up

In this post, I developed a findGettersSetters() method that returns a list of getter and setter Method objects for any Java class. I also created isGetter() and isSetter() boolean methods that determine if a specific class method is a getter or a setter.

These three methods are useful when you need to find getters and setters in a class or need to know if a specific method is a getter or a setter.

If you’d like to learn more about Java Reflection and other techniques like these, check out my JavaReflection LiveLesson.

Java Reflection Live Lesson

Java Reflection Live Lesson