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

69
LINES

< > BotCompany Repo | #2000497 // call function(s) for reflective calls (e.g. to hotwired objects), fixed, not requiring translator

New Tinybrain snippet

1  
  static Object call(Object o, String method, Object... args) {
2  
    try {
3  
      Method m = call_findMethod(o, method, args, false);
4  
      m.setAccessible(true);
5  
      return m.invoke(o, args);
6  
    } catch (Exception e) {
7  
      throw new RuntimeException(e);
8  
    }
9  
  }
10  
11  
  static Object call(Class c, String method, Object... args) {
12  
    try {
13  
      Method m = call_findStaticMethod(c, method, args, false);
14  
      m.setAccessible(true);
15  
      return m.invoke(null, args);
16  
    } catch (Exception e) {
17  
      throw new RuntimeException(e);
18  
    }
19  
  }
20  
21  
  static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
22  
    while (c != null) {
23  
      for (Method m : c.getDeclaredMethods()) {
24  
        if (debug)
25  
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
26  
        if (!m.getName().equals(method)) {
27  
          if (debug) System.out.println("Method name mismatch: " + method);
28  
          continue;
29  
        }
30  
31  
        if ((m.getModifiers() & Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug))
32  
          continue;
33  
34  
        return m;
35  
      }
36  
      c = c.getSuperclass();
37  
    }
38  
    throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + c.getName());
39  
  }
40  
41  
  static Method call_findMethod(Object o, String method, Object[] args, boolean debug) {
42  
    Class c = o.getClass();
43  
    while (c != null) {
44  
      for (Method m : c.getDeclaredMethods()) {
45  
        if (debug)
46  
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
47  
        if (m.getName().equals(method) && call_checkArgs(m, args, debug))
48  
          return m;
49  
      }
50  
      c = c.getSuperclass();
51  
    }
52  
    throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName());
53  
  }
54  
55  
  private static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
56  
    Class<?>[] types = m.getParameterTypes();
57  
    if (types.length != args.length) {
58  
      if (debug)
59  
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
60  
      return false;
61  
    }
62  
    for (int i = 0; i < types.length; i++)
63  
      if (!(args[i] == null || types[i].isInstance(args[i]))) {
64  
        if (debug)
65  
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
66  
        return false;
67  
      }
68  
    return true;
69  
  }

Author comment

Began life as a copy of #2000492

download  show line numbers   

Snippet is not live.

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

Comments [hide]

ID Author/Program Comment Date
862 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
static Object call(Object o, String method, Object... args) {
try {
Method m = call_findMethod(o, method, args, false);
m.setAccessible(true);
return m.invoke(o, args);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

static Object call(Class c, String method, Object... args) {
try {
Method m = call_findStaticMethod(c, method, args, false);
m.setAccessible(true);
return m.invoke(null, args);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (!m.getName().equals(method)) {
if (debug) System.out.println("Method name mismatch: " + method);
continue;
}

if ((m.getModifiers() & Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug))
continue;

return m;
}
c = c.getSuperclass();
}
throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + c.getName());
}

static Method call_findMethod(Object o, String method, Object[] args, boolean debug) {
Class c = o.getClass();
while (c != null) {
for (Method m : c.getDeclaredMethods()) {
if (debug)
System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
if (m.getName().equals(method) && call_checkArgs(m, args, debug))
return m;
}
c = c.getSuperclass();
}
throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName());
}

private static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
Class<?>[] types = m.getParameterTypes();
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || types[i].isInstance(args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}


}}
2015-08-19 22:48:19  delete 
796 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #2000497
Snippet name: call function(s) for reflective calls (e.g. to hotwired objects), fixed, not requiring translator
Eternal ID of this version: #2000497/1
Text MD5: 827ea77428a09af0e1e6d0faaf4308a5
Author: stefan
Category:
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-05 17:35:01
Source code size: 2576 bytes / 69 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 846 / 1023
Referenced in: [show references]