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

27
LINES

< > BotCompany Repo | #1001974 // smartSet

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3025L/18K).

static void smartSet(Field f, O o, O value) throws Exception {
  try {
    f.set(o, value);
  } catch (Exception e) {
    Class type = f.getType();
    
    // take care of common case (long to int)
    if (type == int.class && value instanceof Long)
      ret with f.set(o, ((Long) value).intValue());
      
    if (type == bool.class && value instanceof S)
      ret with f.set(o, isTrueOrYes(value/S));
    
    if (type == LinkedHashMap.class && value instanceof Map)
      ret with f.set(o, asLinkedHashMap((Map) value));
    
    ifclass Concept
      try {
        if (f.getType() == Concept.Ref.class)
          ret with f.set(o, ((Concept) o).new Ref((Concept) value));
        if (o instanceof Concept.Ref)
          ret with f.set(o, ((Concept.Ref) o).get());
      } catch {}
    endif
    throw e;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 23 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, dpqxeycirhfy, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, ofpaelxlmzfo, onxytkatvevr, podlckwnjdmb, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001974
Snippet name: smartSet
Eternal ID of this version: #1001974/14
Text MD5: a04caf4190df4d06ece4f1f5601050c2
Transpilation MD5: 9d8716e4dc064e9657dc169838545f76
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-22 10:23:06
Source code size: 843 bytes / 27 lines
Pitched / IR pitched: No / No
Views / Downloads: 790 / 8952
Version history: 13 change(s)
Referenced in: [show references]