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

41
LINES

< > BotCompany Repo | #1001136 // nuObject - calls constructor with arguments

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

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

1  
static O nuObject(S className, O... args) ctex {
2  
  ret nuObject(classForName(className), args);
3  
}
4  
5  
// too ambiguous - maybe need to fix some callers
6  
/*static O nuObject(O realm, S className, O... args) {
7  
  ret nuObject(_getClass(realm, className), args);
8  
}*/
9  
10  
static <A> A nuObject(Class<A> c, O... args) ctex {
11  
  if (args == null || args.length == 0) ret nuObjectWithoutArguments(c); // cached!
12  
  
13  
  Constructor m = nuObject_findConstructor(c, args);
14  
  makeAccessible(m);
15  
  return (A) m.newInstance(args);
16  
}
17  
18  
static Constructor nuObject_findConstructor(Class c, Object... args) {
19  
  for (Constructor m : getDeclaredConstructors_cached(c)) {
20  
    if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
21  
      continue;
22  
    return m;
23  
  }
24  
  fail("Constructor " + c.getName() + getClasses(args) + " not found"
25  
    + (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : ""));
26  
}
27  
28  
 static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
29  
    if (types.length != args.length) {
30  
      if (debug)
31  
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
32  
      return false;
33  
    }
34  
    for (int i = 0; i < types.length; i++)
35  
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
36  
        if (debug)
37  
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
38  
        return false;
39  
      }
40  
    return true;
41  
  }

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001136
Snippet name: nuObject - calls constructor with arguments
Eternal ID of this version: #1001136/11
Text MD5: 676d75ee9055a0dfdb2a1954cd1aafc1
Transpilation MD5: f685b230f90e48485314deb03abd1dbc
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:08
Source code size: 1509 bytes / 41 lines
Pitched / IR pitched: No / No
Views / Downloads: 904 / 4640
Version history: 10 change(s)
Referenced in: [show references]