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

25
LINES

< > BotCompany Repo | #1008660 // nuObjectWithoutArguments

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

Libraryless. Click here for Pure Java version (7541L/43K).

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 : getDeclaredConstructors_cached(c))
    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  old transpilations   

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

No comments. add comment

Snippet ID: #1008660
Snippet name: nuObjectWithoutArguments
Eternal ID of this version: #1008660/11
Text MD5: 16af5afd895104e026f270024a5132f9
Transpilation MD5: a1d702df2b4e3c0d797836917ef9e947
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-29 19:42:35
Source code size: 903 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 635 / 732
Version history: 10 change(s)
Referenced in: [show references]