提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
字段到Getter方法
//package com.nowjava; import java.beans.IntrospectionException;/*from 时 代 J a v a 公 众 号 - nowjava.com*/ import java.beans.PropertyDescriptor; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; String fieldName = "nowjava.com"; System.out.println(fieldToGetter(clazz, fieldName)); } public static <T> String fieldToGetter(Class<T> clazz, String fieldName) {//来 自 时 代 J a v a 公 众 号 try { PropertyDescriptor propertyDescriptor = new PropertyDescriptor( fieldName, clazz); return propertyDescriptor.getReadMethod().getName(); } catch (IntrospectionException e) { e.printStackT