Raj K
Raj K

Reputation: 470

Java - get a class attribute name when get Method is called

Can anyone please clarify me, if it is possible to get a class attribute name when I call get/set method of the class in java.
I saw something on online that it is possible to get class attribute name using Reflection concept.

My situation:
Trying to write a method that checks the value of the attribute for null/empty and return the attribute name in case the attribute value is null/empty.

example:

Class:

public class MyClass {
  private appName;

  public void setAppName(String appName) {
  this.appName = appName;
  }

  public String getAppName() {
  return this.appName;
  }
}

Validation Method:

public String validateForNull(MyClass myclass) {
   String name = myclass.getAppName();
   if(name == null || name.isEmpty()) {
   return //here I want to return attributeName which will be "appName"
   }
}

I realized returning the constant string that represent attribute name will be lot easier and neat way for the approach. But I was wondering if I can do it as a generic way where validate method takes the class object and check all/specified attributes for null/empty and return attribute name in case null/empty value.

Thanks

Upvotes: 2

Views: 30141

Answers (2)

VGR
VGR

Reputation: 44328

It’s best to avoid reflection. Instead of trying to find the name automatically, pass it as an argument:

public String validateForNull(String attributeValue,
                              String attributeName) {

    if (attributeValue == null || attributeValue.isEmpty()) {
        return attributeName;
    }
    return null;
}

// ...

String emptyAttribute =
    validateForNull(myclass.getAppName(), "appName");

Upvotes: 2

Guillaume Barré
Guillaume Barré

Reputation: 4218

You can not get the name of the attribute calling a getter or setter.

By the way you have no guarantee that the method you invoke just set or return a simple attribute.

But you are right, you can, by reflection, get the values of the attributes for a given object.

   public String validateForNull(MyClass myclass) throws IllegalArgumentException, IllegalAccessException {
        // Get the attributes of the class
        Field[] fs = myclass.getClass().getFields();
        for(Field f : fs) {
            // make the attribute accessible if it's a private one
            f.setAccessible(true);

            // Get the value of the attibute of the instance received as parameter  
            Object value = f.get(myclass);
            if(value == null) {
                return f.getName();
            }
        }
        return null;
     }

Doing something like this will require a testing more complete than just if(value == null) because I imagine that you can have attributes of several types and each type will have a specific validation.

If you decide to go this way you can use an annotation to identify the attributes to validate and the use :

Annotation[] ans =  f.getAnnotations();

To check if your annotation is present on the attribute and thus validate only the required fields

Upvotes: 3

Related Questions