Get name of a field

后端 未结 7 970
日久生厌
日久生厌 2020-12-03 02:43

Is it possible in Java to get a name of field in string from the actual field? like:

public class mod {
    @ItemID
    public static ItemLinkTool linkTool;
         


        
7条回答
  •  悲&欢浪女
    2020-12-03 03:10

    My motivation at getting the field name is using it to search for things in a key-value database (like mongodb). I have a struct with members (fields) that represents the saved data. I use the member names to search the db.

    My suggestion is to put a static getter for each important member. Example:

    public class Data {
      public static String getMember1Key() {
        return "member1";
      }
      public String member1;
    }
    

    Hopefully, Java will have some mechanism for this in the future, because nowadays, the metadata may be part of the data. Especially when doing stuff with JSON.

提交回复
热议问题