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