1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.services.adaption.reflection;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.Arrays;
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;
25 public class ReflectionAdapter2<T> implements Adapter<T, Resource> {
27 Constructor<? extends T> constructor;
28 IDynamicAdapter2[] parameters;
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;
39 public void adapt(AsyncReadGraph g, final Resource source, final Resource r, final AsyncProcedure<T> procedure) {
41 if(parameters.length == 0) {
43 // System.out.println("ReflectionAdapter2 " + ReflectionAdapter2.this);
46 procedure.execute(g, constructor.newInstance());
47 } catch (IllegalArgumentException e) {
48 procedure.exception(g, e);
50 } catch (InstantiationException e) {
51 procedure.exception(g, e);
53 } catch (IllegalAccessException e) {
54 procedure.exception(g, e);
56 } catch (InvocationTargetException e) {
57 procedure.exception(g, e.getCause());
58 e.getCause().printStackTrace();
61 } else if( parameters.length == 1 && parameters[0] instanceof ThisResource2) {
64 procedure.execute(g, constructor.newInstance(r));
65 } catch (IllegalArgumentException e) {
66 procedure.exception(g, e);
68 } catch (InstantiationException e) {
69 procedure.exception(g, e);
71 } catch (IllegalAccessException e) {
72 procedure.exception(g, e);
74 } catch (InvocationTargetException e) {
75 procedure.exception(g, e.getCause());
76 e.getCause().printStackTrace();
81 g.asyncRequest(new AsyncReadRequest() {
84 public void run(AsyncReadGraph graph) {
86 Object[] args = new Object[parameters.length];
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);
94 } catch (InstantiationException e) {
95 procedure.exception(g, e);
97 } catch (IllegalAccessException e) {
98 procedure.exception(g, e);
100 } catch (InvocationTargetException e) {
101 procedure.exception(g, e.getCause());
102 e.getCause().printStackTrace();
103 } catch (DatabaseException e) {
104 procedure.exception(g, e);
106 } catch (Throwable t) {
107 procedure.exception(g, t);
113 public String toString() {
114 return "ReflectionAdapter$1" + constructor + "$" + Arrays.toString(parameters);
124 public String toString() {
125 return "ReflectionAdapter for " + constructor.getName();