Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

16
LINES

< > BotCompany Repo | #1000597 // "get" function (get static field by reflection) - old

JavaX fragment (include)

1  
  static Object get(Class c, String field) {
2  
    try {
3  
      Field f = get_findStaticField(c, field);
4  
      f.setAccessible(true);
5  
      return f.get(null);
6  
    } catch (Exception e) {
7  
      throw new RuntimeException(e);
8  
    }
9  
  }
10  
11  
  static Field get_findStaticField(Class<?> c, String field) {
12  
    for (Field f : c.getDeclaredFields())
13  
      if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
14  
        return f;
15  
    throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
16  
  }

Author comment

Began life as a copy of #1000415

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000597
Snippet name: "get" function (get static field by reflection) - old
Eternal ID of this version: #1000597/1
Text MD5: 262d06ffddf6f235d9f46dd43dedcc30
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-22 20:13:06
Source code size: 548 bytes / 16 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 625 / 541
Referenced in: [show references]