Getting property / field name using getter method for pojo / java bean?

I have a class below and I need to get the field name from the getter method using java reflection. Can I get a field name or property name using the getter method?

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

My questions are: can I get the field / property name with the getter method? If I use getName (), can I get the name property? I need a property of the name, but not its meaning. Is this possible with java reflection?

+8
java reflection
source share
9 answers

Yes, it is 100% possible.

 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; } 
+12
source share

It is not entirely correct to simply remove the "get" or "is" prefix and lowercase first letter. For example, the corresponding bean for getID would be ID, not iD.

The easiest way to get the bean name is to remove the get or pre prefix and then pass the result to Introspector.decapitalize .

Here is the method I wrote to do this:

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

You cannot verify what the code does using reflection.

You can assume that the getName() method reads a field called name and does nothing. However, there are no requirements for this. for example, the field name may be m_name or _name or nameField or may not even be a field.

+5
source share

If your beans follow JavaBean conventions, you use reflection to get all the get and is methods and remove the get or is prefixes from the found method names and you have the field names.

Update

 // 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 } } 
+1
source share

You must access this method. At present, the getter will return the name member, but this may change in the future. This can lazily create an instance from a database or web service by building it from first / last name, etc. The name field may not exist.

So always go through the method (even through reflection)

0
source share

You can

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

If you know the name of the method, you only need to remove "get" and lowercase the next letter, so you do not need to speculate.

If the getter (getName ()) method returns a property with a name other than "name", you cannot get the property name from the method name.

If you do not know the name of the method, you can get all the methods by reflection, and you can also get all the properties of the name.

0
source share

Using the reflection API, POJO fields can be obtained as shown below. The inherited class may find the problem here.

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

Or, using the Reflections API, you can also get all the methods in the class and iterate over it to find the attribute names (standard POJO methods start with get / is / set) ... This approach worked for me for the Inherited class structure.

 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)); } } 

However, a more interesting approach is below:

Using the Jackson library, I was able to find all the properties of a class of type String / integer / double and the corresponding values ​​in the Map class. (all without using api reflections!)

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

Try to execute

 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; } 

}

The get method is used to dynamically retrieve data from a program method or from a database. It will display only non-value values.

-one
source share

All Articles