1 package org.simantics.scl.compiler.internal.codegen.utils;
3 import java.lang.reflect.Method;
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;
12 public class ValueFromMethod {
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);
19 Object ret = method.invoke(null);
20 return returnsVoid ? Tuple0.INSTANCE : ret;
23 return new FunctionImpl1<Object,Object>() {
25 public Object apply(Object p0) {
27 Object ret = method.invoke(null, p0);
28 return returnsVoid ? Tuple0.INSTANCE : ret;
29 } catch (ReflectiveOperationException e) {
30 throw new RuntimeException(e);
35 return new FunctionImpl2<Object,Object,Object>() {
37 public Object apply(Object p0, Object p1) {
39 Object ret = method.invoke(null, p0, p1);
40 return returnsVoid ? Tuple0.INSTANCE : ret;
41 } catch (ReflectiveOperationException e) {
42 throw new RuntimeException(e);
47 return new FunctionImpl3<Object,Object,Object,Object>() {
49 public Object apply(Object p0, Object p1, Object p2) {
51 Object ret = method.invoke(null, p0, p1, p2);
52 return returnsVoid ? Tuple0.INSTANCE : ret;
53 } catch (ReflectiveOperationException e) {
54 throw new RuntimeException(e);
59 return new FunctionImpl4<Object,Object,Object,Object,Object>() {
61 public Object apply(Object p0, Object p1, Object p2, Object p3) {
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);
71 return new FunctionImplN(arity) {
73 public Object doApply(Object... ps) {
75 Object ret = method.invoke(null, ps);
76 return returnsVoid ? Tuple0.INSTANCE : ret;
77 } catch (ReflectiveOperationException e) {
78 throw new RuntimeException(e);