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

77
LINES

< > BotCompany Repo | #1018499 // callOpt_uncached (works)

JavaX fragment (include)

1  
static O callOpt_uncached(O o) {
2  
  if (o == null) return null;
3  
  return callF(o);
4  
}
5  
6  
static Object callOpt_uncached(Object o, String method, Object... args) {
7  
  try {
8  
    if (o == null) return null;
9  
    if (o instanceof Class) {
10  
      Method m = callOpt_uncached_findStaticMethod((Class) o, method, args, false);
11  
      if (m == null) return null;
12  
      makeAccessible(m);
13  
      return invokeMethod(m, null, args);
14  
    /*} else if (o instanceof DynamicMethods) {
15  
      ret ((DynamicMethods) o)._dynCall(method, args);*/
16  
    } else {
17  
      Method m = callOpt_uncached_findMethod(o, method, args, false);
18  
      if (m == null) return null;
19  
      makeAccessible(m, true);
20  
      return invokeMethod(m, o, args);
21  
    }
22  
  } catch (Exception e) {
23  
    //fail(e.getMessage() + " | Method: " + method + ", receiver: " + className(o) + ", args: (" + join(", ", map(f className, args) + ")");
24  
    throw rethrow(e);
25  
  }
26  
}
27  
28  
static Method callOpt_uncached_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
29  
  Class _c = c;
30  
  while (c != null) {
31  
    for (Method m : c.getDeclaredMethods()) {
32  
      if (debug)
33  
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
34  
      if (!m.getName().equals(method)) {
35  
        if (debug) System.out.println("Method name mismatch: " + method);
36  
        continue;
37  
      }
38  
39  
      if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 || !callOpt_uncached_checkArgs(m, args, debug))
40  
        continue;
41  
42  
      return m;
43  
    }
44  
    c = c.getSuperclass();
45  
  }
46  
  return null;
47  
}
48  
49  
static Method callOpt_uncached_findMethod(Object o, String method, Object[] args, boolean debug) {
50  
  Class c = o.getClass();
51  
  while (c != null) {
52  
    for (Method m : c.getDeclaredMethods()) {
53  
      if (debug)
54  
        System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
55  
      if (m.getName().equals(method) && callOpt_uncached_checkArgs(m, args, debug))
56  
        return m;
57  
    }
58  
    c = c.getSuperclass();
59  
  }
60  
  return null;
61  
}
62  
63  
private static boolean callOpt_uncached_checkArgs(Method m, Object[] args, boolean debug) {
64  
  Class<?>[] types = m.getParameterTypes();
65  
  if (types.length != args.length) {
66  
    if (debug)
67  
      System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
68  
    return false;
69  
  }
70  
  for (int i = 0; i < types.length; i++)
71  
    if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
72  
      if (debug)
73  
        System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
74  
      return false;
75  
    }
76  
  return true;
77  
}

Author comment

Began life as a copy of #1001199

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1018499
Snippet name: callOpt_uncached (works)
Eternal ID of this version: #1018499/3
Text MD5: 377173087b7cc19c4afb152c1f865de3
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:02:04
Source code size: 2703 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 385 / 425
Version history: 2 change(s)
Referenced in: [show references]