]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.reflection/src/org/simantics/scl/reflection/functions/ConstructorFunction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.reflection / src / org / simantics / scl / reflection / functions / ConstructorFunction.java
diff --git a/bundles/org.simantics.scl.reflection/src/org/simantics/scl/reflection/functions/ConstructorFunction.java b/bundles/org.simantics.scl.reflection/src/org/simantics/scl/reflection/functions/ConstructorFunction.java
new file mode 100755 (executable)
index 0000000..f766089
--- /dev/null
@@ -0,0 +1,39 @@
+package org.simantics.scl.reflection.functions;\r
+\r
+import java.lang.reflect.Constructor;\r
+import java.lang.reflect.InvocationTargetException;\r
+\r
+import org.simantics.scl.runtime.function.FunctionImplN;\r
+\r
+public class ConstructorFunction extends FunctionImplN {\r
+    Constructor<?> constructor;\r
+\r
+    public ConstructorFunction(Constructor<?> constructor) {\r
+        super(constructor.getParameterTypes().length);\r
+        this.constructor = constructor;\r
+    }\r
+    \r
+    public Constructor<?> getConstructor() {\r
+        return constructor;\r
+    }\r
+\r
+    @Override\r
+    public Object doApply(Object... ps) {\r
+        try {\r
+            return constructor.newInstance(ps);\r
+        } catch (InstantiationException e) {\r
+            throw new RuntimeException(e);\r
+        } catch (IllegalArgumentException e) {\r
+            throw new RuntimeException(e);\r
+        } catch (IllegalAccessException e) {\r
+            throw new RuntimeException(e);\r
+        } catch (InvocationTargetException e) {\r
+            throw new RuntimeException(e);\r
+        }\r
+    }\r
+    \r
+    @Override\r
+    public String toString() {\r
+        return constructor.getName();\r
+    }\r
+}\r