使用pojo / java bean的getter方法获取属性/字段名称?

我有一个下面的类,我需要使用javareflection从getter方法获取字段名称。 是否可以使用getter方法获取字段名称或属性名称?

class A { private String name; private String salary; // getter and setter methods } 

我的问题是:我可以通过getter方法获得字段/属性名称吗? 如果我使用getName(),我可以得到name属性吗? 我需要name属性,但不是它的值。 是否可以通过javareflection?

是的,这是100%的可能..

 public static String getFieldName(Method method) { try { Class clazz=method.getDeclaringClass(); BeanInfo info = Introspector.getBeanInfo(clazz); PropertyDescriptor[] props = info.getPropertyDescriptors(); for (PropertyDescriptor pd : props) { if(method.equals(pd.getWriteMethod()) || method.equals(pd.getReadMethod())) { System.out.println(pd.getDisplayName()); return pd.getName(); } } } catch (IntrospectionException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } 

仅删除“get”或“is”前缀和小写第一个字母并不完全合适。 例如,getID的相应bean名称将是ID而不是iD。

获取bean名称的最简单方法是删除get或is前缀,然后将结果传递给Introspector.decapitalize 。

这是我写的一个方法来做这件事:

 private String getBeanName(String methodName) { // Assume the method starts with either get or is. return Introspector.decapitalize(methodName.substring(methodName.startsWith("is") ? 2 : 3)); } 

您无法使用reflection检查代码的作用。

您可以假设getName()方法读取名为name的字段, getName()执行任何其他操作。 但是没有必要这样做。 例如,字段名称可能是m_name_namenameField甚至不是字段。

如果您的bean遵循JavaBean约定,那么您使用reflection来获取所有“get”和“is”方法,并从检索到的方法名称中删除“get”或“is”前缀,并且您具有字段名称。

更新

 // Get the Class object associated with this class. MyClass myClass= new MyClass (); Class objClass= myClass.getClass(); // Get the public methods associated with this class. Method[] methods = objClass.getMethods(); for (Method method:methods) { String name=method.getName(); if(name.startsWith("get") || name.startsWith("is")) { //...code to remove the prefixes } } 

您应该通过该方法访问。 目前,getter会返回成员name但将来可能会发生变化 。 它可以从数据库或Web服务中懒惰地实例化它,从firstname / surname等构建它。 name字段很可能不存在。

所以总是通过这个方法(即使通过reflection)

您可以

 Field[] declaredFields = A.class.getDeclaredFields(); for(Field f:declaredFields){ System.out.println(f.getName()); } 

如果你知道方法的名称,你只需要删除“get”并将下面的字母转换为低位字母,这样就不需要reflection了。

如果getter方法(getName())返回名称不同于“name”的属性,则无法从方法名称获取属性的名称。

如果你不知道方法的名称,通过reflection你可以获得所有方法,你也可以获得所有名称的属性。

使用reflectionAPI,可以如下检索POJO字段。 inheritance的类可能会在这里找到问题。

 TestClass testObject= new TestClass().getClass(); Fields[] fields = testObject.getFields(); for (Field field:fields) { String name=field.getName(); System.out.println(name); } 

或者通过使用Reflections API,还可以检索类中的所有方法并迭代它以查找属性名称(标准POJO方法以get / is / set开头)…这种方法适用于我的Inherited类结构。

 TestClass testObject= new TestClass().getClass(); Method[] methods = testObject.getMethods(); for (Method method:methods) { String name=method.getName(); if(name.startsWith("get")) { System.out.println(name.substring(3)); }else if(name.startsWith("is")) { System.out.println(name.substring(2)); } } 

然而,更有趣的方法如下:

在Jackson库的帮助下,我能够找到String / integer / double类型的所有类属性,以及Map类中的相应值。 ( 所有没有使用reflectionapi!

 TestClass testObject = new TestClass(); com.fasterxml.jackson.databind.ObjectMapper m = new com.fasterxml.jackson.databind.ObjectMapper(); Map props = m.convertValue(testObject, Map.class); for(Map.Entry entry : props.entrySet()){ if(entry.getValue() instanceof String || entry.getValue() instanceof Integer || entry.getValue() instanceof Double){ System.out.println(entry.getKey() + "-->" + entry.getValue()); } } 

请尝试以下方法

 class A{ private String name; private String salary; //getter and setter methods public void setName(String name){ this.name = name; } public void setSalary(String salary){ this.salary = salary; } public String getName(){ return name; } public String getSalary(){ return salary; } 

}

get方法用于从程序方法或数据库中动态检索数据。 它只会反映值而不是值的属性。