简体   繁体   English

Java-调用get方法时获取类属性名称

[英]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. 任何人都可以澄清一下,如果我在java中调用类的get / set方法时是否可以获得类属性名称。
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. 但是我想知道是否可以作为一种通用方法来实现,其中validate方法采用类对象并检查所有/指定的属性是否为null / empty,并在出现null / empty值的情况下返回属性名称。

Thanks 谢谢

You can not get the name of the attribute calling a getter or setter. 您无法获取调用getter或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(value == null)更加完整的测试,因为我想您可以拥有多种类型的属性,并且每种类型都有特定的验证。

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 要检查您的注释是否存在于属性上,从而仅验证必填字段

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM