PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/kilim/examples/Reflect.java

http://github.com/kilim/kilim
Java | 51 lines | 41 code | 8 blank | 2 comment | 2 complexity | f94cd4dbbcb1776e5d787d91338679e9 MD5 | raw file
 1package kilim.examples;
 2
 3import java.lang.reflect.Method;
 4
 5import kilim.Pausable;
 6import kilim.Task;
 7
 8public class Reflect extends Task {
 9  @Override
10  public void execute() throws Pausable, Exception {
11    int n = test();
12    System.out.println("test (normal invocation of instance method): " + n);
13    // Invoking test() via reflection
14    Method mthd = Reflect.class.getDeclaredMethod("test", new Class[0]);
15    Object ret = Task.invoke(mthd, this, /* no args */(Object[])null);
16    System.out.println("test (reflect invocation of instance method): " + ret);
17
18    n = static_test();
19    System.out.println("test (normal invocation of static method): " + n);
20    // Invoking test() via reflection
21    mthd = Reflect.class.getDeclaredMethod("static_test", new Class[0]);
22    ret = Task.invoke(mthd, /* target = */ null, /* no args */(Object[])null);
23    System.out.println("test (reflect invocation of static method): " + ret);
24    System.exit(0);
25  }
26  
27  public int test() throws Pausable {
28    int m = 10;
29    for (int i = 0; i < 2; i++) { // Force multiple yields
30      Task.sleep(100); 
31      m *= 2;
32    }
33    return m; // must return 40 if all goes well.
34  }
35
36  public static int static_test() throws Pausable {
37    int m = 10;
38    for (int i = 0; i < 2; i++) { // Force multiple yields
39      Task.sleep(100); 
40      m *= 2;
41    }
42    return m; // must return 40 if all goes well.
43  }
44
45  
46  public static void main(String args[]) {
47    Reflect ref = new Reflect();
48    ref.start();
49    ref.joinb();
50  }
51}