Libraryless. Click here for Pure Java version (7923L/45K).
1 | sbool preciseNuObject_debug; |
2 | |
3 | static <A> A preciseNuObject(Class<A> c, O... args) ctex { |
4 | Constructor[] methods = getDeclaredConstructors_cached(c); |
5 | if (methods.length == 0) |
6 | fail(c + " doesn't define any constructors"); |
7 | ret preciseNuObject(methods, args); |
8 | } |
9 | |
10 | static <A> A preciseNuObject(Constructor[] methods, O... args) ctex { |
11 | Constructor best = null; |
12 | int bestScore = Int.MAX_VALUE; |
13 | bool widening; |
14 | if (preciseNuObject_debug) |
15 | printVars("preciseNuObject", c := first(methods).getDeclaringClass(), methods := l(methods)); |
16 | |
17 | for (m : methods) { |
18 | int score = methodApplicabilityScore_withPrimitiveWidening(m, args); |
19 | if (score == 0) |
20 | ret (A) m.newInstance(args); |
21 | |
22 | if (preciseNuObject_debug) |
23 | print("Method score: " + m + " " + score); |
24 | |
25 | if (abs(score) < bestScore) { |
26 | best = m; |
27 | bestScore = abs(score); |
28 | widening = score < 0; |
29 | } |
30 | } |
31 | |
32 | if (best != null) |
33 | if (widening) |
34 | ret (A) invokeConstructorWithWidening(best, args); |
35 | else |
36 | ret (A) best.newInstance(args); |
37 | |
38 | // try varargs - TODO: make this precise too |
39 | |
40 | methodSearch: for (m : methods) { |
41 | continue unless m.isVarArgs(); |
42 | O[] newArgs = massageArgsForVarArgsCall(m, args); |
43 | if (newArgs != null) |
44 | ret (A) m.newInstance(newArgs); |
45 | } |
46 | |
47 | var c = first(methods).getDeclaringClass(); |
48 | fail("No matching constructor found: " + formatFunctionCall(c, map _getClass(args))); |
49 | } |
Began life as a copy of #1001136
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034783 |
Snippet name: | preciseNuObject - precise constructor call with conversions [like newPreciseCall, dev.] |
Eternal ID of this version: | #1034783/10 |
Text MD5: | 99bc9b548d51d05b8fc6c782eb5d43a8 |
Transpilation MD5: | f805d56ddefcbe2bc7450f8d64b8d1a2 |
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:41:10 |
Source code size: | 1493 bytes / 49 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 214 / 330 |
Version history: | 9 change(s) |
Referenced in: | [show references] |