-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.services.adaption.reflection;\r
-\r
-import java.lang.reflect.Constructor;\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.util.Arrays;\r
-\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.adaption.Adapter;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.procedure.AsyncProcedure;\r
-\r
-public class ReflectionAdapter2<T> implements Adapter<T, Resource> {\r
-\r
- Constructor<? extends T> constructor;\r
- IDynamicAdapter2[] parameters;\r
- \r
- public ReflectionAdapter2(Class<? extends T> clazz, IDynamicAdapter2 ... parameters) throws SecurityException, NoSuchMethodException, DatabaseException {\r
- Class<?>[] parameterTypes = new Class<?>[parameters.length];\r
- for(int i=0;i<parameters.length;++i)\r
- parameterTypes[i] = parameters[i].getType();\r
- this.constructor = clazz.getConstructor(parameterTypes);\r
- this.parameters = parameters;\r
- }\r
- \r
- @Override\r
- public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {\r
-\r
- if(parameters.length == 0) {\r
- \r
-// System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);\r
-\r
- try {\r
- procedure.execute(g, constructor.newInstance());\r
- } catch (IllegalArgumentException e) {\r
- procedure.exception(g, e);\r
- e.printStackTrace();\r
- } catch (InstantiationException e) {\r
- procedure.exception(g, e);\r
- e.printStackTrace();\r
- } catch (IllegalAccessException e) {\r
- procedure.exception(g, e);\r
- e.printStackTrace();\r
- } catch (InvocationTargetException e) {\r
- procedure.exception(g, e.getCause());\r
- e.getCause().printStackTrace();\r
- }\r
- \r
- } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {\r
-\r
- try {\r
- procedure.execute(g, constructor.newInstance(r));\r
- } catch (IllegalArgumentException e) {\r
- procedure.exception(g, e);\r
- e.printStackTrace();\r
- } catch (InstantiationException e) {\r
- procedure.exception(g, e);\r
- e.printStackTrace();\r
- } catch (IllegalAccessException e) {\r
- procedure.exception(g, e);\r
- e.printStackTrace();\r
- } catch (InvocationTargetException e) {\r
- procedure.exception(g, e.getCause());\r
- e.getCause().printStackTrace();\r
- }\r
- \r
- } else {\r
- \r
- g.asyncRequest(new ReadRequest() {\r
- \r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- \r
- Object[] args = new Object[parameters.length];\r
- try {\r
- for(int i=0;i<parameters.length;++i)\r
- args[i] = parameters[i].adapt(graph, r);\r
- procedure.execute(graph, constructor.newInstance(args));\r
- } catch (IllegalArgumentException e) {\r
- procedure.exception(graph, e);\r
- e.printStackTrace();\r
- } catch (InstantiationException e) {\r
- procedure.exception(graph, e);\r
- e.printStackTrace();\r
- } catch (IllegalAccessException e) {\r
- procedure.exception(graph, e);\r
- e.printStackTrace();\r
- } catch (InvocationTargetException e) {\r
- procedure.exception(graph, e.getCause());\r
- e.getCause().printStackTrace();\r
- } catch (DatabaseException e) {\r
- procedure.exception(graph, e);\r
- e.printStackTrace();\r
- }\r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);\r
- }\r
- \r
- });\r
- \r
- }\r
- \r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return "ReflectionAdapter for " + constructor.getName();\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.services.adaption.reflection;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Arrays;
+
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.adaption.Adapter;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.AsyncProcedure;
+import org.simantics.db.request.AsyncRead;
+
+public class ReflectionAdapter2<T> implements Adapter<T, Resource> {
+
+ Constructor<? extends T> constructor;
+ IDynamicAdapter2[] parameters;
+
+ public ReflectionAdapter2(Class<? extends T> clazz, IDynamicAdapter2 ... parameters) throws SecurityException, NoSuchMethodException, DatabaseException {
+ Class<?>[] parameterTypes = new Class<?>[parameters.length];
+ for(int i=0;i<parameters.length;++i)
+ parameterTypes[i] = parameters[i].getType();
+ this.constructor = clazz.getConstructor(parameterTypes);
+ this.parameters = parameters;
+ }
+
+ @Override
+ public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
+
+ if(parameters.length == 0) {
+
+// System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);
+
+ try {
+ procedure.execute(g, constructor.newInstance());
+ } catch (IllegalArgumentException e) {
+ procedure.exception(g, e);
+ e.printStackTrace();
+ } catch (InstantiationException e) {
+ procedure.exception(g, e);
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ procedure.exception(g, e);
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ procedure.exception(g, e.getCause());
+ e.getCause().printStackTrace();
+ }
+
+ } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
+
+ try {
+ procedure.execute(g, constructor.newInstance(r));
+ } catch (IllegalArgumentException e) {
+ procedure.exception(g, e);
+ e.printStackTrace();
+ } catch (InstantiationException e) {
+ procedure.exception(g, e);
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ procedure.exception(g, e);
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ procedure.exception(g, e.getCause());
+ e.getCause().printStackTrace();
+ }
+
+ } else {
+
+ g.asyncRequest(new AsyncRead() {
+
+ @Override
+ public void perform(AsyncReadGraph graph, AsyncProcedure p) {
+
+ Object[] args = new Object[parameters.length];
+ try {
+ for(int i=0;i<parameters.length;++i)
+ args[i] = parameters[i].adapt(graph, r);
+ p.execute(graph, constructor.newInstance(args));
+ } catch (IllegalArgumentException e) {
+ p.exception(g, e);
+ e.printStackTrace();
+ } catch (InstantiationException e) {
+ p.exception(g, e);
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ p.exception(g, e);
+ e.printStackTrace();
+ } catch (InvocationTargetException e) {
+ p.exception(g, e.getCause());
+ e.getCause().printStackTrace();
+ } catch (DatabaseException e) {
+ p.exception(g, e);
+ e.printStackTrace();
+ } catch (Throwable t) {
+ p.exception(g, t);
+ t.printStackTrace();
+ }
+
+ }
+
+ @Override
+ public String toString() {
+ return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);
+ }
+
+ }, procedure);
+
+ }
+
+ }
+
+ @Override
+ public String toString() {
+ return "ReflectionAdapter for " + constructor.getName();
+ }
+
+}