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 private static final class Arity1Func extends FunctionImpl1<Object, Object> {
15 private final Method method;
16 private final boolean returnsVoid;
18 private Arity1Func(Method method, boolean returnsVoid) {
20 this.returnsVoid = returnsVoid;
24 public Object apply(Object p0) {
26 Object ret = method.invoke(null, p0);
27 return returnsVoid ? Tuple0.INSTANCE : ret;
28 } catch (ReflectiveOperationException e) {
29 throw new RuntimeException(e);
34 public int hashCode() {
35 return method == null ? 0 : method.hashCode();
39 public boolean equals(Object obj) {
44 if (getClass() != obj.getClass())
46 Arity1Func other = (Arity1Func) obj;
47 return method.equals(other.method);
51 private static final class Arity2Func extends FunctionImpl2<Object, Object, Object> {
52 private final Method method;
53 private final boolean returnsVoid;
55 private Arity2Func(Method method, boolean returnsVoid) {
57 this.returnsVoid = returnsVoid;
61 public Object apply(Object p0, Object p1) {
63 Object ret = method.invoke(null, p0, p1);
64 return returnsVoid ? Tuple0.INSTANCE : ret;
65 } catch (ReflectiveOperationException e) {
66 throw new RuntimeException(e);
71 public int hashCode() {
72 return method == null ? 0 : method.hashCode();
76 public boolean equals(Object obj) {
81 if (getClass() != obj.getClass())
83 Arity2Func other = (Arity2Func) obj;
84 return method.equals(other.method);
88 private static final class Arity3Func extends FunctionImpl3<Object, Object, Object, Object> {
89 private final Method method;
90 private final boolean returnsVoid;
92 private Arity3Func(Method method, boolean returnsVoid) {
94 this.returnsVoid = returnsVoid;
98 public Object apply(Object p0, Object p1, Object p2) {
100 Object ret = method.invoke(null, p0, p1, p2);
101 return returnsVoid ? Tuple0.INSTANCE : ret;
102 } catch (ReflectiveOperationException e) {
103 throw new RuntimeException(e);
108 public int hashCode() {
109 return method == null ? 0 : method.hashCode();
113 public boolean equals(Object obj) {
118 if (getClass() != obj.getClass())
120 Arity3Func other = (Arity3Func) obj;
121 return method.equals(other.method);
125 private static final class Arity4Func extends FunctionImpl4<Object, Object, Object, Object, Object> {
126 private final Method method;
127 private final boolean returnsVoid;
129 private Arity4Func(Method method, boolean returnsVoid) {
130 this.method = method;
131 this.returnsVoid = returnsVoid;
135 public Object apply(Object p0, Object p1, Object p2, Object p3) {
137 Object ret = method.invoke(null, p0, p1, p2, p3);
138 return returnsVoid ? Tuple0.INSTANCE : ret;
139 } catch (ReflectiveOperationException e) {
140 throw new RuntimeException(e);
145 public int hashCode() {
146 return method == null ? 0 : method.hashCode();
150 public boolean equals(Object obj) {
155 if (getClass() != obj.getClass())
157 Arity4Func other = (Arity4Func) obj;
158 return method.equals(other.method);
162 private static final class ArityNFunc extends FunctionImplN {
163 private final Method method;
164 private final boolean returnsVoid;
166 private ArityNFunc(int arity, Method method, boolean returnsVoid) {
168 this.method = method;
169 this.returnsVoid = returnsVoid;
173 public Object doApply(Object... ps) {
175 Object ret = method.invoke(null, ps);
176 return returnsVoid ? Tuple0.INSTANCE : ret;
177 } catch (ReflectiveOperationException e) {
178 throw new RuntimeException(e);
183 public int hashCode() {
184 return method == null ? 0 : method.hashCode();
188 public boolean equals(Object obj) {
193 if (getClass() != obj.getClass())
195 ArityNFunc other = (ArityNFunc) obj;
196 return method.equals(other.method);
200 public static Object getValueFromStaticMethod(final Method method) throws ReflectiveOperationException {
201 int arity = method.getParameterTypes().length;
202 final boolean returnsVoid = method.getReturnType().equals(void.class);
205 Object ret = method.invoke(null);
206 return returnsVoid ? Tuple0.INSTANCE : ret;
209 return new Arity1Func(method, returnsVoid);
211 return new Arity2Func(method, returnsVoid);
213 return new Arity3Func(method, returnsVoid);
215 return new Arity4Func(method, returnsVoid);
217 return new ArityNFunc(arity, method, returnsVoid);