0
public class Emp {

    private String employeeId;
    private String employeeName;
    private double employeeSalary;

    public String getEmployeeId() {
        return employeeId;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public double getEmployeeSalary() {
        return employeeSalary;
    }

    public void setEmployeeId(String employeeId) {
        this.employeeId = employeeId;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
    public void setEmployeeSalary(double employeeSalary) {
        this.employeeSalary = employeeSalary;
    }
    @Override
    public String toString() {
        return "Emp [employeeId=" + employeeId + ", employeeName="
                + employeeName + ", employeeSalary=" + employeeSalary + "]";
    }


}

这是一个示例类。用户将输入班级的名称。根据用户输入,我想找到 getter 和 setter 方法及其类的详细信息。为此,我使用下面的代码。但它没有显示employeeId、employeeName、employeeSalary 的gettes 和setter。

BeanInfo info; 
info = Introspector.getBeanInfo(c.getClass()); 
MethodDescriptor[] md = info.getMethodDescriptors();            
for (MethodDescriptor methodDescriptor : md) {
    LOGGER.info("Method-"+methodDescriptor.getMethod().getName());          
} 

我正在尝试使用上面的代码读取类的方法,但它没有显示声明字段的 getter 和 setter。

4

1 回答 1

0

我觉得你可能需要 Field 类。Field 类提供有关类或接口的单个​​字段的信息和对它们的动态访问。反射字段可以是类(静态)字段或实例字段。使用Field[]. 下面是它的包装

import java.lang.reflect.Field;
于 2013-01-03T04:04:07.180 回答