X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fcodegen%2Futils%2FValueFromMethod.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Finternal%2Fcodegen%2Futils%2FValueFromMethod.java;h=bccfa892832a05c2a024f5a7b0f3258c3e35c6c3;hp=5a736a38075b4e635b3b6914e5c8a8e5180a0947;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/ValueFromMethod.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/ValueFromMethod.java index 5a736a380..bccfa8928 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/ValueFromMethod.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/ValueFromMethod.java @@ -1,85 +1,85 @@ -package org.simantics.scl.compiler.internal.codegen.utils; - -import java.lang.reflect.Method; - -import org.simantics.scl.runtime.function.FunctionImpl1; -import org.simantics.scl.runtime.function.FunctionImpl2; -import org.simantics.scl.runtime.function.FunctionImpl3; -import org.simantics.scl.runtime.function.FunctionImpl4; -import org.simantics.scl.runtime.function.FunctionImplN; -import org.simantics.scl.runtime.tuple.Tuple0; - -public class ValueFromMethod { - - public static Object getValueFromStaticMethod(final Method method) throws ReflectiveOperationException { - int arity = method.getParameterTypes().length; - final boolean returnsVoid = method.getReturnType().equals(void.class); - switch(arity) { - case 0: { - Object ret = method.invoke(null); - return returnsVoid ? Tuple0.INSTANCE : ret; - } - case 1: - return new FunctionImpl1() { - @Override - public Object apply(Object p0) { - try { - Object ret = method.invoke(null, p0); - return returnsVoid ? Tuple0.INSTANCE : ret; - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } - }; - case 2: - return new FunctionImpl2() { - @Override - public Object apply(Object p0, Object p1) { - try { - Object ret = method.invoke(null, p0, p1); - return returnsVoid ? Tuple0.INSTANCE : ret; - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } - }; - case 3: - return new FunctionImpl3() { - @Override - public Object apply(Object p0, Object p1, Object p2) { - try { - Object ret = method.invoke(null, p0, p1, p2); - return returnsVoid ? Tuple0.INSTANCE : ret; - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } - }; - case 4: - return new FunctionImpl4() { - @Override - public Object apply(Object p0, Object p1, Object p2, Object p3) { - try { - Object ret = method.invoke(null, p0, p1, p2, p3); - return returnsVoid ? Tuple0.INSTANCE : ret; - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } - }; - default: - return new FunctionImplN(arity) { - @Override - public Object doApply(Object... ps) { - try { - Object ret = method.invoke(null, ps); - return returnsVoid ? Tuple0.INSTANCE : ret; - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } - } - }; - } - } - -} +package org.simantics.scl.compiler.internal.codegen.utils; + +import java.lang.reflect.Method; + +import org.simantics.scl.runtime.function.FunctionImpl1; +import org.simantics.scl.runtime.function.FunctionImpl2; +import org.simantics.scl.runtime.function.FunctionImpl3; +import org.simantics.scl.runtime.function.FunctionImpl4; +import org.simantics.scl.runtime.function.FunctionImplN; +import org.simantics.scl.runtime.tuple.Tuple0; + +public class ValueFromMethod { + + public static Object getValueFromStaticMethod(final Method method) throws ReflectiveOperationException { + int arity = method.getParameterTypes().length; + final boolean returnsVoid = method.getReturnType().equals(void.class); + switch(arity) { + case 0: { + Object ret = method.invoke(null); + return returnsVoid ? Tuple0.INSTANCE : ret; + } + case 1: + return new FunctionImpl1() { + @Override + public Object apply(Object p0) { + try { + Object ret = method.invoke(null, p0); + return returnsVoid ? Tuple0.INSTANCE : ret; + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + }; + case 2: + return new FunctionImpl2() { + @Override + public Object apply(Object p0, Object p1) { + try { + Object ret = method.invoke(null, p0, p1); + return returnsVoid ? Tuple0.INSTANCE : ret; + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + }; + case 3: + return new FunctionImpl3() { + @Override + public Object apply(Object p0, Object p1, Object p2) { + try { + Object ret = method.invoke(null, p0, p1, p2); + return returnsVoid ? Tuple0.INSTANCE : ret; + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + }; + case 4: + return new FunctionImpl4() { + @Override + public Object apply(Object p0, Object p1, Object p2, Object p3) { + try { + Object ret = method.invoke(null, p0, p1, p2, p3); + return returnsVoid ? Tuple0.INSTANCE : ret; + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + }; + default: + return new FunctionImplN(arity) { + @Override + public Object doApply(Object... ps) { + try { + Object ret = method.invoke(null, ps); + return returnsVoid ? Tuple0.INSTANCE : ret; + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + }; + } + } + +}