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=4efd2a8d404e7037925fd7d005d782e1fa5c8d3f;hp=bccfa892832a05c2a024f5a7b0f3258c3e35c6c3;hb=6c0602e24f372963350b594fed9c7828a5540641;hpb=1a1bd2744cf9615f1cc7364094c5d9bdbdd4c97f 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 bccfa8928..4efd2a8d4 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 @@ -11,6 +11,192 @@ import org.simantics.scl.runtime.tuple.Tuple0; public class ValueFromMethod { + private static final class Arity1Func extends FunctionImpl1 { + private final Method method; + private final boolean returnsVoid; + + private Arity1Func(Method method, boolean returnsVoid) { + this.method = method; + this.returnsVoid = returnsVoid; + } + + @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); + } + } + + @Override + public int hashCode() { + return method == null ? 0 : method.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Arity1Func other = (Arity1Func) obj; + return method.equals(other.method); + } + } + + private static final class Arity2Func extends FunctionImpl2 { + private final Method method; + private final boolean returnsVoid; + + private Arity2Func(Method method, boolean returnsVoid) { + this.method = method; + this.returnsVoid = returnsVoid; + } + + @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); + } + } + + @Override + public int hashCode() { + return method == null ? 0 : method.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Arity2Func other = (Arity2Func) obj; + return method.equals(other.method); + } + } + + private static final class Arity3Func extends FunctionImpl3 { + private final Method method; + private final boolean returnsVoid; + + private Arity3Func(Method method, boolean returnsVoid) { + this.method = method; + this.returnsVoid = returnsVoid; + } + + @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); + } + } + + @Override + public int hashCode() { + return method == null ? 0 : method.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Arity3Func other = (Arity3Func) obj; + return method.equals(other.method); + } + } + + private static final class Arity4Func extends FunctionImpl4 { + private final Method method; + private final boolean returnsVoid; + + private Arity4Func(Method method, boolean returnsVoid) { + this.method = method; + this.returnsVoid = returnsVoid; + } + + @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); + } + } + + @Override + public int hashCode() { + return method == null ? 0 : method.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Arity4Func other = (Arity4Func) obj; + return method.equals(other.method); + } + } + + private static final class ArityNFunc extends FunctionImplN { + private final Method method; + private final boolean returnsVoid; + + private ArityNFunc(int arity, Method method, boolean returnsVoid) { + super(arity); + this.method = method; + this.returnsVoid = returnsVoid; + } + + @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); + } + } + + @Override + public int hashCode() { + return method == null ? 0 : method.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ArityNFunc other = (ArityNFunc) obj; + return method.equals(other.method); + } + } + public static Object getValueFromStaticMethod(final Method method) throws ReflectiveOperationException { int arity = method.getParameterTypes().length; final boolean returnsVoid = method.getReturnType().equals(void.class); @@ -20,65 +206,15 @@ public class ValueFromMethod { 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); - } - } - }; + return new Arity1Func(method, returnsVoid); 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); - } - } - }; + return new Arity2Func(method, returnsVoid); 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); - } - } - }; + return new Arity3Func(method, returnsVoid); 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); - } - } - }; + return new Arity4Func(method, returnsVoid); 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); - } - } - }; + return new ArityNFunc(arity, method, returnsVoid); } }