]> gerrit.simantics Code Review - simantics/platform.git/blob
bccfa892832a05c2a024f5a7b0f3258c3e35c6c3
[simantics/platform.git] /
1 package org.simantics.scl.compiler.internal.codegen.utils;
2
3 import java.lang.reflect.Method;
4
5 import org.simantics.scl.runtime.function.FunctionImpl1;
6 import org.simantics.scl.runtime.function.FunctionImpl2;
7 import org.simantics.scl.runtime.function.FunctionImpl3;
8 import org.simantics.scl.runtime.function.FunctionImpl4;
9 import org.simantics.scl.runtime.function.FunctionImplN;
10 import org.simantics.scl.runtime.tuple.Tuple0;
11
12 public class ValueFromMethod {
13
14     public static Object getValueFromStaticMethod(final Method method) throws ReflectiveOperationException {
15         int arity = method.getParameterTypes().length;
16         final boolean returnsVoid = method.getReturnType().equals(void.class);
17         switch(arity) {
18         case 0: {
19             Object ret = method.invoke(null);
20             return returnsVoid ? Tuple0.INSTANCE : ret;
21         }
22         case 1:
23             return new FunctionImpl1<Object,Object>() {
24                 @Override
25                 public Object apply(Object p0) {
26                     try {
27                         Object ret = method.invoke(null, p0);
28                         return returnsVoid ? Tuple0.INSTANCE : ret;
29                     } catch (ReflectiveOperationException e) {
30                         throw new RuntimeException(e);
31                     }
32                 }
33             };
34         case 2:
35             return new FunctionImpl2<Object,Object,Object>() {
36                 @Override
37                 public Object apply(Object p0, Object p1) {
38                     try {
39                         Object ret = method.invoke(null, p0, p1);
40                         return returnsVoid ? Tuple0.INSTANCE : ret;
41                     } catch (ReflectiveOperationException e) {
42                         throw new RuntimeException(e);
43                     }
44                 }
45             };
46         case 3:
47             return new FunctionImpl3<Object,Object,Object,Object>() {
48                 @Override
49                 public Object apply(Object p0, Object p1, Object p2) {
50                     try {
51                         Object ret = method.invoke(null, p0, p1, p2);
52                         return returnsVoid ? Tuple0.INSTANCE : ret;
53                     } catch (ReflectiveOperationException e) {
54                         throw new RuntimeException(e);
55                     }
56                 }
57             };
58         case 4:
59             return new FunctionImpl4<Object,Object,Object,Object,Object>() {
60                 @Override
61                 public Object apply(Object p0, Object p1, Object p2, Object p3) {
62                     try {
63                         Object ret = method.invoke(null, p0, p1, p2, p3);
64                         return returnsVoid ? Tuple0.INSTANCE : ret;
65                     } catch (ReflectiveOperationException e) {
66                         throw new RuntimeException(e);
67                     }
68                 }
69             };
70         default:
71             return new FunctionImplN(arity) {
72                 @Override
73                 public Object doApply(Object... ps) {
74                     try {
75                         Object ret =  method.invoke(null, ps);
76                         return returnsVoid ? Tuple0.INSTANCE : ret;
77                     } catch (ReflectiveOperationException e) {
78                         throw new RuntimeException(e);
79                     }
80                 }
81             };
82         }
83     }
84     
85 }