Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-在调用get方法时获取类属性名称_Java_Reflection_Attributes - Fatal编程技术网

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

Java-在调用get方法时获取类属性名称,java,reflection,attributes,Java,Reflection,Attributes,在java中调用类的get/set方法时,是否可以获取类属性名称,请任何人澄清一下。 我在网上看到,可以使用反射概念获取类属性名 我的处境: 尝试编写一个方法,检查属性值是否为null/empty,并在属性值为null/empty时返回属性名称 示例: public class MyClass { private appName; public void setAppName(String appName) { this.appName = appName; } publ

在java中调用类的get/set方法时,是否可以获取类属性名称,请任何人澄清一下。
我在网上看到,可以使用反射概念获取类属性名

我的处境:
尝试编写一个方法,检查属性值是否为null/empty,并在属性值为null/empty时返回属性名称

示例:

public class MyClass {
  private appName;

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

  public String getAppName() {
  return this.appName;
  }
}
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"
   }
}
类:

public class MyClass {
  private appName;

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

  public String getAppName() {
  return this.appName;
  }
}
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"
   }
}
验证方法:

public class MyClass {
  private appName;

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

  public String getAppName() {
  return this.appName;
  }
}
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"
   }
}
我意识到返回表示属性名的常量字符串对于这种方法来说将更容易、更简洁。但我想知道我是否可以作为一种通用的方法来做,其中validate方法接受类对象,检查所有/指定的属性是否为null/empty,并在null/empty值的情况下返回属性名


谢谢

您无法获取调用getter或setter的属性的名称

顺便说一下,您不能保证您调用的方法只是设置或返回一个简单属性

但是你是对的,你可以通过反射,得到给定对象的属性值

   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;
     }
这样做将需要一个比if(value==null)更完整的测试,因为我认为您可以有几种类型的属性,并且每种类型都有一个特定的验证

如果您决定这样做,可以使用注释来标识要验证的属性和使用:

Annotation[] ans =  f.getAnnotations();

要检查属性上是否存在注释,从而仅验证所需字段,最好避免反射。与其尝试自动查找名称,不如将其作为参数传递:

public String validateForNull(String attributeValue,
                              String attributeName) {

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

// ...

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

我不太清楚你到底想要什么,但如果我明白了什么,那么这听起来像是某种情况。如果我是对的,您可以直接使用bean验证API,也可以在它的一个实现中寻找想法。