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

49
LINES

< > BotCompany Repo | #1034783 // preciseNuObject - precise constructor call with conversions [like newPreciseCall, dev.]

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

Libraryless. Click here for Pure Java version (7923L/45K).

sbool preciseNuObject_debug;

static <A> A preciseNuObject(Class<A> c, O... args) ctex {
  Constructor[] methods = getDeclaredConstructors_cached(c);
  if (methods.length == 0)
    fail(c + " doesn't define any constructors");
  ret preciseNuObject(methods, args);
}
  
static <A> A preciseNuObject(Constructor[] methods, O... args) ctex {
  Constructor best = null;
  int bestScore = Int.MAX_VALUE;
  bool widening;
  if (preciseNuObject_debug)
    printVars("preciseNuObject", c := first(methods).getDeclaringClass(), methods := l(methods));
    
  for (m : methods) {
    int score = methodApplicabilityScore_withPrimitiveWidening(m, args);
    if (score == 0) 
      ret (A) m.newInstance(args);
    
    if (preciseNuObject_debug)
      print("Method score: " + m + " " + score);
        
    if (abs(score) < bestScore) {
      best = m;
      bestScore = abs(score);
      widening = score < 0;
    }
  }
  
  if (best != null)
    if (widening)
      ret (A) invokeConstructorWithWidening(best, args);
    else
      ret (A) best.newInstance(args);
    
  // try varargs - TODO: make this precise too
  
  methodSearch: for (m : methods) {
    continue unless m.isVarArgs();
    O[] newArgs = massageArgsForVarArgsCall(m, args);
    if (newArgs != null)
      ret (A) m.newInstance(newArgs);
  }
    
  var c = first(methods).getDeclaringClass();
  fail("No matching constructor found: " + formatFunctionCall(c, map _getClass(args))); 
}

Author comment

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: 92 / 179
Version history: 9 change(s)
Referenced in: [show references]