2

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

1
  • It's not particularly clear to me what exactly you want, but if I understand anything, then this sounds like some sort of Bean Validation case. If I'm right, you can either directly use the bean validation API or look for ideas in one of its implementations. Commented Dec 21, 2017 at 15:51

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.