1 package org.simantics.scl.compiler.internal.codegen.utils;
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
6 import org.simantics.scl.runtime.function.FunctionImpl1;
7 import org.simantics.scl.runtime.function.FunctionImpl2;
8 import org.simantics.scl.runtime.function.FunctionImpl3;
9 import org.simantics.scl.runtime.function.FunctionImpl4;
10 import org.simantics.scl.runtime.function.FunctionImplN;
11 import org.simantics.scl.runtime.tuple.Tuple0;
13 public class ValueFromMethod {
15 private static final class Arity1Func extends FunctionImpl1<Object, Object> {
16 private final Method method;
17 private final boolean returnsVoid;
19 private Arity1Func(Method method, boolean returnsVoid) {
21 this.returnsVoid = returnsVoid;
25 public Object apply(Object p0) {
27 Object ret = method.invoke(null, p0);
28 return returnsVoid ? Tuple0.INSTANCE : ret;
29 } catch (ReflectiveOperationException e) {
30 if (e instanceof InvocationTargetException)
31 throw new RuntimeException(e.getCause());
32 throw new RuntimeException(e);
37 public int hashCode() {
38 return method == null ? 0 : method.hashCode();
42 public boolean equals(Object obj) {
47 if (getClass() != obj.getClass())
49 Arity1Func other = (Arity1Func) obj;
50 return method.equals(other.method);
54 private static final class Arity2Func extends FunctionImpl2<Object, Object, Object> {
55 private final Method method;
56 private final boolean returnsVoid;
58 private Arity2Func(Method method, boolean returnsVoid) {
60 this.returnsVoid = returnsVoid;
64 public Object apply(Object p0, Object p1) {
66 Object ret = method.invoke(null, p0, p1);
67 return returnsVoid ? Tuple0.INSTANCE : ret;
68 } catch (ReflectiveOperationException e) {
69 throw new RuntimeException(e);
74 public int hashCode() {
75 return method == null ? 0 : method.hashCode();
79 public boolean equals(Object obj) {
84 if (getClass() != obj.getClass())
86 Arity2Func other = (Arity2Func) obj;
87 return method.equals(other.method);
91 private static final class Arity3Func extends FunctionImpl3<Object, Object, Object, Object> {
92 private final Method method;
93 private final boolean returnsVoid;
95 private Arity3Func(Method method, boolean returnsVoid) {
97 this.returnsVoid = returnsVoid;
101 public Object apply(Object p0, Object p1, Object p2) {
103 Object ret = method.invoke(null, p0, p1, p2);
104 return returnsVoid ? Tuple0.INSTANCE : ret;
105 } catch (ReflectiveOperationException e) {
106 throw new RuntimeException(e);
111 public int hashCode() {
112 return method == null ? 0 : method.hashCode();
116 public boolean equals(Object obj) {
121 if (getClass() != obj.getClass())
123 Arity3Func other = (Arity3Func) obj;
124 return method.equals(other.method);
128 private static final class Arity4Func extends FunctionImpl4<Object, Object, Object, Object, Object> {
129 private final Method method;
130 private final boolean returnsVoid;
132 private Arity4Func(Method method, boolean returnsVoid) {
133 this.method = method;
134 this.returnsVoid = returnsVoid;
138 public Object apply(Object p0, Object p1, Object p2, Object p3) {
140 Object ret = method.invoke(null, p0, p1, p2, p3);
141 return returnsVoid ? Tuple0.INSTANCE : ret;
142 } catch (ReflectiveOperationException e) {
143 throw new RuntimeException(e);
148 public int hashCode() {
149 return method == null ? 0 : method.hashCode();
153 public boolean equals(Object obj) {
158 if (getClass() != obj.getClass())
160 Arity4Func other = (Arity4Func) obj;
161 return method.equals(other.method);
165 private static final class ArityNFunc extends FunctionImplN {
166 private final Method method;
167 private final boolean returnsVoid;
169 private ArityNFunc(int arity, Method method, boolean returnsVoid) {
171 this.method = method;
172 this.returnsVoid = returnsVoid;
176 public Object doApply(Object... ps) {
178 Object ret = method.invoke(null, ps);
179 return returnsVoid ? Tuple0.INSTANCE : ret;
180 } catch (ReflectiveOperationException e) {
181 throw new RuntimeException(e);
186 public int hashCode() {
187 return method == null ? 0 : method.hashCode();
191 public boolean equals(Object obj) {
196 if (getClass() != obj.getClass())
198 ArityNFunc other = (ArityNFunc) obj;
199 return method.equals(other.method);
203 public static Object getValueFromStaticMethod(final Method method) throws ReflectiveOperationException {
204 int arity = method.getParameterTypes().length;
205 final boolean returnsVoid = method.getReturnType().equals(void.class);
208 Object ret = method.invoke(null);
209 return returnsVoid ? Tuple0.INSTANCE : ret;
212 return new Arity1Func(method, returnsVoid);
214 return new Arity2Func(method, returnsVoid);
216 return new Arity3Func(method, returnsVoid);
218 return new Arity4Func(method, returnsVoid);
220 return new ArityNFunc(arity, method, returnsVoid);