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