Dynamically return a list of all class variable values ​​in Java

I am creating a helper class when parsing XML elements, so the developer does not need to know the exact name and capitalization of the XML fields.

private static class TagNames{
      public static String RESOURCE_ID = "ResourceId";
      public static String RESOURCE_NAME = "ResourceName";
      public static String RESOURCE_PRICE = "ResourcePrice";
}

This facilitates the implementation of actions such as:

someXMLParser.getValueByTagName(TagNames.RESOURCE_ID);

My question is that. If I want to iterate over all fields declared in the TagNames class, how to do this? Pseudocode:

For tag in TagNames:
   someXMLParser.getValueByTagName(tag)

I know that I will probably have to rebuild all of this. But I cannot figure out how to make names easily accessible, as well as iterative, without any duplication.

Any suggestions?

+3
source share
5 answers

, , , Java Enum . :

public class EnumTest {
    public enum Tags {
        RESOURCE_ID("ResourceId"), 
        REOURCE_NAME("ResourceName"), 
        RESOURCE_PRICE("ResourcePrice");

        private final String tagName;
        Tags(String tagName) {
            this.tagName = tagName;
        }

        public String getTagName() {
            return tagName;
        }
    }

    public static void main(String[] args) {
        for(Tags tag : Tags.values()) {
            System.out.println("const:" + tag.name() 
                    + " tagName:" + tag.getTagName());
        }
        // API user might do e.g.:
        // document.getValueForTag(Tags.REOURCE_NAME);
    }
}
+2

, ResourceBundle . , , , , .

+1

, , , enums ResourceBundles, . , Map name → (, , try/catch throws)

public static Map<String, Object> getConstantValues(Class<?> clazz){

    Map<String, Object> constantValues = new LinkedHashMap<String, Object>();
    for(Field field : clazz.getDeclaredFields()){
        int modifiers = field.getModifiers();
        if(Modifiers.isPublic(mod)
            && Modifiers.isStatic(mod) && Modifiers.isFinal(mod)){
            constantValues.put(field.getName(), field.get(null));
        }
    }
    return constantValues;
}
+1

, :

public class Main {

    public enum TagName { RESOURCE_ID, REOURCE_NAME, RESOURCE_PRICE }

    private static String[] tags = {"ResourceID", "ResourceName", "ResourcePrice"};

    public static String getValueByTagName(TagName tag) {

    return tags[tag.ordinal()];
    }

    public static void main(String[] args) {

    System.out.println("Calling by getValueByTagName:");
    System.out.println(getValueByTagName(TagName.RESOURCE_ID));

    System.out.println("Calling TagName.values() for loop:");

    for (TagName t : TagName.values()) {

        System.out.println(getValueByTagName(t));
    }
    }
}
0

, "values":

public class Main {
  public static enum TagName {
    RESOURCE_ID("ResourceId"),
    RESOURCE_NAME("ResourceName"),
    RESOURCE_PRICE("ResourcePrice"),
    ;
    private String s;
    private TagName(String s) { this.s = s; }
    public String toString() { return this.s; }
    public static String[] strings() {
      List<String> ss = new ArrayList<String>();
      for (TagName tagName : TagName.values()) {
        ss.add(tagName.toString());
      }
      return ss.toArray(new String[ss.size()]);
    }
  }
  public static void main(String[] args) {
    // Use TagName.values() for the enums, or for strings...
    for (String s : TagName.strings()) {
      System.out.println(s);
    }
  }
}

, ""; , , . , , ...

0

Source: https://habr.com/ru/post/1776359/


All Articles