]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.services/src/org/simantics/db/services/adaption/reflection/StaticMethodAdapter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.services / src / org / simantics / db / services / adaption / reflection / StaticMethodAdapter.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.InvocationTargetException;
15 import java.lang.reflect.Method;
16
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.procedure.AsyncProcedure;
20
21 public class StaticMethodAdapter<T> extends AbstractReflectionAdapter<T> {
22
23         Method constructor;
24         
25         public StaticMethodAdapter(Class<? extends T> clazz, String methodName, IDynamicAdapter2 ... parameters) throws SecurityException, NoSuchMethodException, DatabaseException {
26             super(parameters);      
27                 Class<?>[] parameterTypes = new Class<?>[parameters.length];
28                 for(int i=0;i<parameters.length;++i)
29                         parameterTypes[i] = parameters[i].getType();
30                 this.constructor = clazz.getMethod(methodName, parameterTypes);
31         }
32         
33         @SuppressWarnings("unchecked")
34     @Override
35         public void construct(AsyncReadGraph g, AsyncProcedure<T> procedure, Object ... args) {
36             try {
37             procedure.execute(g, (T)constructor.invoke(null, args));
38         } catch (IllegalArgumentException e) {
39             procedure.exception(g, e);
40             e.printStackTrace();
41         } catch (IllegalAccessException e) {
42             procedure.exception(g, e);
43             e.printStackTrace();
44         } catch (InvocationTargetException e) {
45             procedure.exception(g, e.getCause());
46             e.getCause().printStackTrace();
47         }
48         }
49             
50     @Override
51     public String toString() {
52         return "ReflectionAdapter for " + constructor.getName();
53     }
54
55 }