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 Throwable cause = e.getCause();
32 if(cause instanceof RuntimeException)
33 throw (RuntimeException)cause;
35 throw new RuntimeException(cause);
37 throw new RuntimeException(e);
42 public int hashCode() {
43 return method == null ? 0 : method.hashCode();
47 public boolean equals(Object obj) {
52 if (getClass() != obj.getClass())
54 Arity1Func other = (Arity1Func) obj;
55 return method.equals(other.method);
59 private static final class Arity2Func extends FunctionImpl2<Object, Object, Object> {
60 private final Method method;
61 private final boolean returnsVoid;
63 private Arity2Func(Method method, boolean returnsVoid) {
65 this.returnsVoid = returnsVoid;
69 public Object apply(Object p0, Object p1) {
71 Object ret = method.invoke(null, p0, p1);
72 return returnsVoid ? Tuple0.INSTANCE : ret;
73 } catch (ReflectiveOperationException e) {
74 if (e instanceof InvocationTargetException) {
75 Throwable cause = e.getCause();
76 if(cause instanceof RuntimeException)
77 throw (RuntimeException)cause;
79 throw new RuntimeException(cause);
81 throw new RuntimeException(e);
86 public int hashCode() {
87 return method == null ? 0 : method.hashCode();
91 public boolean equals(Object obj) {
96 if (getClass() != obj.getClass())
98 Arity2Func other = (Arity2Func) obj;
99 return method.equals(other.method);
103 private static final class Arity3Func extends FunctionImpl3<Object, Object, Object, Object> {
104 private final Method method;
105 private final boolean returnsVoid;
107 private Arity3Func(Method method, boolean returnsVoid) {
108 this.method = method;
109 this.returnsVoid = returnsVoid;
113 public Object apply(Object p0, Object p1, Object p2) {
115 Object ret = method.invoke(null, p0, p1, p2);
116 return returnsVoid ? Tuple0.INSTANCE : ret;
117 } catch (ReflectiveOperationException e) {
118 if (e instanceof InvocationTargetException) {
119 Throwable cause = e.getCause();
120 if(cause instanceof RuntimeException)
121 throw (RuntimeException)cause;
123 throw new RuntimeException(cause);
125 throw new RuntimeException(e);
130 public int hashCode() {
131 return method == null ? 0 : method.hashCode();
135 public boolean equals(Object obj) {
140 if (getClass() != obj.getClass())
142 Arity3Func other = (Arity3Func) obj;
143 return method.equals(other.method);
147 private static final class Arity4Func extends FunctionImpl4<Object, Object, Object, Object, Object> {
148 private final Method method;
149 private final boolean returnsVoid;
151 private Arity4Func(Method method, boolean returnsVoid) {
152 this.method = method;
153 this.returnsVoid = returnsVoid;
157 public Object apply(Object p0, Object p1, Object p2, Object p3) {
159 Object ret = method.invoke(null, p0, p1, p2, p3);
160 return returnsVoid ? Tuple0.INSTANCE : ret;
161 } catch (ReflectiveOperationException e) {
162 if (e instanceof InvocationTargetException) {
163 Throwable cause = e.getCause();
164 if(cause instanceof RuntimeException)
165 throw (RuntimeException)cause;
167 throw new RuntimeException(cause);
169 throw new RuntimeException(e);
174 public int hashCode() {
175 return method == null ? 0 : method.hashCode();
179 public boolean equals(Object obj) {
184 if (getClass() != obj.getClass())
186 Arity4Func other = (Arity4Func) obj;
187 return method.equals(other.method);
191 private static final class ArityNFunc extends FunctionImplN {
192 private final Method method;
193 private final boolean returnsVoid;
195 private ArityNFunc(int arity, Method method, boolean returnsVoid) {
197 this.method = method;
198 this.returnsVoid = returnsVoid;
202 public Object doApply(Object... ps) {
204 Object ret = method.invoke(null, ps);
205 return returnsVoid ? Tuple0.INSTANCE : ret;
206 } catch (ReflectiveOperationException e) {
207 if (e instanceof InvocationTargetException) {
208 Throwable cause = e.getCause();
209 if(cause instanceof RuntimeException)
210 throw (RuntimeException)cause;
212 throw new RuntimeException(cause);
214 throw new RuntimeException(e);
219 public int hashCode() {
220 return method == null ? 0 : method.hashCode();
224 public boolean equals(Object obj) {
229 if (getClass() != obj.getClass())
231 ArityNFunc other = (ArityNFunc) obj;
232 return method.equals(other.method);
236 public static Object getValueFromStaticMethod(final Method method) throws ReflectiveOperationException {
237 int arity = method.getParameterTypes().length;
238 final boolean returnsVoid = method.getReturnType().equals(void.class);
241 Object ret = method.invoke(null);
242 return returnsVoid ? Tuple0.INSTANCE : ret;
245 return new Arity1Func(method, returnsVoid);
247 return new Arity2Func(method, returnsVoid);
249 return new Arity3Func(method, returnsVoid);
251 return new Arity4Func(method, returnsVoid);
253 return new ArityNFunc(arity, method, returnsVoid);