]> gerrit.simantics Code Review - simantics/platform.git/blob
f8ec0f81a7f11dc05d102d81ce7ef2feeccdaf34
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.services.adaption.reflection;\r
13 \r
14 import java.lang.reflect.Constructor;\r
15 import java.lang.reflect.InvocationTargetException;\r
16 import java.util.Arrays;\r
17 \r
18 import org.simantics.db.AsyncReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.adaption.Adapter;\r
22 import org.simantics.db.common.request.ReadRequest;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.procedure.AsyncProcedure;\r
25 \r
26 public class ReflectionAdapter2<T> implements Adapter<T, Resource> {\r
27 \r
28         Constructor<? extends T> constructor;\r
29         IDynamicAdapter2[] parameters;\r
30         \r
31         public ReflectionAdapter2(Class<? extends T> clazz, IDynamicAdapter2 ... parameters) throws SecurityException, NoSuchMethodException, DatabaseException {\r
32                 Class<?>[] parameterTypes = new Class<?>[parameters.length];\r
33                 for(int i=0;i<parameters.length;++i)\r
34                         parameterTypes[i] = parameters[i].getType();\r
35                 this.constructor = clazz.getConstructor(parameterTypes);\r
36                 this.parameters = parameters;\r
37         }\r
38         \r
39     @Override\r
40     public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {\r
41 \r
42         if(parameters.length == 0) {\r
43             \r
44 //            System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);\r
45 \r
46             try {\r
47                 procedure.execute(g, constructor.newInstance());\r
48             } catch (IllegalArgumentException e) {\r
49                 procedure.exception(g, e);\r
50                 e.printStackTrace();\r
51             } catch (InstantiationException e) {\r
52                 procedure.exception(g, e);\r
53                 e.printStackTrace();\r
54             } catch (IllegalAccessException e) {\r
55                 procedure.exception(g, e);\r
56                 e.printStackTrace();\r
57             } catch (InvocationTargetException e) {\r
58                 procedure.exception(g, e.getCause());\r
59                 e.getCause().printStackTrace();\r
60             }\r
61             \r
62         } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {\r
63 \r
64             try {\r
65                 procedure.execute(g, constructor.newInstance(r));\r
66             } catch (IllegalArgumentException e) {\r
67                 procedure.exception(g, e);\r
68                 e.printStackTrace();\r
69             } catch (InstantiationException e) {\r
70                 procedure.exception(g, e);\r
71                 e.printStackTrace();\r
72             } catch (IllegalAccessException e) {\r
73                 procedure.exception(g, e);\r
74                 e.printStackTrace();\r
75             } catch (InvocationTargetException e) {\r
76                 procedure.exception(g, e.getCause());\r
77                 e.getCause().printStackTrace();\r
78             }\r
79                 \r
80         } else {\r
81         \r
82             g.asyncRequest(new ReadRequest() {\r
83     \r
84                 @Override\r
85                 public void run(ReadGraph graph) throws DatabaseException {\r
86                         \r
87                     Object[] args = new Object[parameters.length];\r
88                     try {\r
89                         for(int i=0;i<parameters.length;++i)\r
90                                 args[i] = parameters[i].adapt(graph, r);\r
91                         procedure.execute(graph, constructor.newInstance(args));\r
92                                 } catch (IllegalArgumentException e) {\r
93                                     procedure.exception(graph, e);\r
94                                         e.printStackTrace();\r
95                                 } catch (InstantiationException e) {\r
96                         procedure.exception(graph, e);\r
97                                         e.printStackTrace();\r
98                                 } catch (IllegalAccessException e) {\r
99                         procedure.exception(graph, e);\r
100                                         e.printStackTrace();\r
101                                 } catch (InvocationTargetException e) {\r
102                         procedure.exception(graph, e.getCause());\r
103                                         e.getCause().printStackTrace();\r
104                                 } catch (DatabaseException e) {\r
105                         procedure.exception(graph, e);\r
106                                         e.printStackTrace();\r
107                                 }\r
108                 }\r
109                 \r
110                 @Override\r
111                 public String toString() {\r
112                         return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);\r
113                 }\r
114                 \r
115             });\r
116         \r
117         }\r
118         \r
119     }\r
120     \r
121     @Override\r
122     public String toString() {\r
123         return "ReflectionAdapter for " + constructor.getName();\r
124     }\r
125 \r
126 }\r