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

25
LINES

< > BotCompany Repo | #1008660 - nuObjectWithoutArguments

JavaX fragment (include)

static Map<Class, Constructor> nuObjectWithoutArguments_cache = newDangerousWeakHashMap();

static O nuObjectWithoutArguments(S className) ctex {
  ret nuObjectWithoutArguments(classForName(className));
}

static <A> A nuObjectWithoutArguments(Class<A> c) ctex {
  if (nuObjectWithoutArguments_cache == null)
    // in class init
    ret (A) nuObjectWithoutArguments_findConstructor(c).newInstance();
    
  Constructor m = nuObjectWithoutArguments_cache.get(c);
  if (m == null)
    nuObjectWithoutArguments_cache.put(c, m = nuObjectWithoutArguments_findConstructor(c));
  return (A) m.newInstance();
}

static Constructor nuObjectWithoutArguments_findConstructor(Class c) {
  for (Constructor m : c.getDeclaredConstructors())
    if (empty(m.getParameterTypes())) {
      makeAccessible(m);
      ret m;
    }
  fail("No default constructor found in " + c.getName());
}

Author comment

Began life as a copy of #1001136

download  show line numbers  debug dex   

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

No comments. add comment

Snippet ID: #1008660
Snippet name: nuObjectWithoutArguments
Eternal ID of this version: #1008660/10
Text MD5: 83b7b8f8c70f9164180840f976c96394
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-31 14:57:33
Source code size: 897 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 370 / 394
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1008660 & http://1008660.tinybrain.de