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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.services.adaption.reflection;
\r
14 import java.lang.reflect.Constructor;
\r
15 import java.lang.reflect.InvocationTargetException;
\r
16 import java.util.Arrays;
\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
26 public class ReflectionAdapter2<T> implements Adapter<T, Resource> {
\r
28 Constructor<? extends T> constructor;
\r
29 IDynamicAdapter2[] parameters;
\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
40 public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
\r
42 if(parameters.length == 0) {
\r
44 // System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);
\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);
\r
59 e.printStackTrace();
\r
62 } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
\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);
\r
77 e.printStackTrace();
\r
82 g.asyncRequest(new ReadRequest() {
\r
85 public void run(ReadGraph graph) throws DatabaseException {
\r
87 Object[] args = new Object[parameters.length];
\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);
\r
103 e.printStackTrace();
\r
104 } catch (DatabaseException e) {
\r
105 procedure.exception(graph, e);
\r
106 e.printStackTrace();
\r
111 public String toString() {
\r
112 return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);
\r
122 public String toString() {
\r
123 return "ReflectionAdapter for " + constructor.getName();
\r