mardi 18 juin 2019

What is the best strategy to obtain the field name from a class using a functional interface getter method

I need to obtain the field name of a POJO class using a @FuncionalInterface that models the [get/is]XXXX methods.

I'm already using a library to do some work (https://github.com/cronn-de/reflection-util), and for now, I can obtain the field name when it follows the POJO specification (where the field name respect the [get/is]XXXX format).

For example, given the following class:

public class MyBean {
    private String testName;
    public String getTestName() { return testName; }
    public void setTestName(String testName) { this.testName = testName; }
}

I can obtain the field name using the following source code:

String fieldName = de.cronn.reflection.util.PropertyUtils(MyBean.java, MyBean::getName).getName();

Besides it is working for me, I would like to know if there is some better strategy or more performatic way?





Aucun commentaire:

Enregistrer un commentaire