简体   繁体   English

从 Java 类中的 @XMLElement 获取注释值

[英]Getting annotation values from @XMLElement in a java class

I am trying to get the @XMLElement annotations from a java class that I have, basically trying to make a map of variables where the annotation is required: true.我正在尝试从我拥有的 java 类中获取 @XMLElement 注释,基本上是尝试制作需要注释的变量映射:true。 However it prints out nothing.但是它什么也没打印出来。

I have a java class that has the following snippet:我有一个包含以下代码段的 java 类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
  "subjectCode",
   "version",
   "messageTitle",
})
@XmlRootElement(name = "CreateMessageRequest", namespace = "mynamespaceblahblah")
public class CreateMessageRequest
  extends AbstractRequest
  implements Serializable
{

private final static long serialVersionUID = 10007L;
@XmlElement(namespace = "mynamespaceblahblah", required = true)
protected String subjectCode;
@XmlElement(namespace = "mynamespaceblahblah")
protected String version;
@XmlElement(namespace = "mynamespaceblahblah", required = true)
protected String messageTitle;


//Getters and setters
}

I tried this:我试过这个:

 public HashMap<String, String> getRequired(Class<?> c) {

HashMap<String, String> fieldMap = new HashMap<>();

Annotation[] annotations = c.getAnnotations();
for (int i = 0; i < annotations.length; i++) {

  Annotation annotation = annotations[i];
  if (annotation instanceof XmlElement) {
    XmlElement theElement = (XmlElement) annotation;
    String name = ((XmlElement) annotation).name();

    if (theElement.required()) {
      fieldMap.put(name, "true");
    } else {
      fieldMap.put(name, "false");
    }
  }
}
return fieldMap;
}

But when I use my method with:但是当我使用我的方法时:

SchemaBuilder s = new SchemaBuilder();
System.out.println("Required Methods of class:");

HashMap<String, String> fieldMap = s.getRequired(CreateMessageRequest.class);

for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
  System.out.println(entry.getKey() + " = " + entry.getValue());
}

It prints out它打印出来

Required Methods of class:

Any advice to what I'm doing wrong?对我做错了什么有什么建议吗? I've considered that because its protected I cant access it (I cannot change the annotated class unfortunately) but I am not sure that is the problem.我已经考虑过,因为它是受保护的,所以我无法访问它(不幸的是,我无法更改带注释的类),但我不确定这是问题所在。

The solution was to look at the individual fields as per the suggestions :)解决方案是根据建议查看各个字段:)

In order to help future googlers:为了帮助未来的谷歌员工:

public HashMap<String, String> getRequired(Class<?> c) {

HashMap<String, String> fieldMap = new HashMap<>();

Field[] fields = c.getDeclaredFields();
for (Field f : fields) {

  Annotation[] annotations = f.getAnnotationsByType(XmlElement.class);
  for (int i = 0; i < annotations.length; i++) {
    Annotation annotation = annotations[i];

    if (annotation instanceof XmlElement) {
      XmlElement theElement = (XmlElement) annotation;
      String name = f.getName();
      if (theElement.required()) {
        fieldMap.put(name, "true");
      } else {
        fieldMap.put(name, "false");
      }
    }
  }
}
return fieldMap;
}

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

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