]> gerrit.simantics Code Review - simantics/platform.git/blob
fe08480f0980a64e9b4a8a95a1829cc4b2d27dcd
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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.databoard.binding.reflection;\r
13 \r
14 import java.lang.reflect.Constructor;\r
15 import java.lang.reflect.InvocationTargetException;\r
16 import java.util.WeakHashMap;\r
17 \r
18 import org.simantics.databoard.binding.Binding;\r
19 import org.simantics.databoard.binding.RecordBinding;\r
20 import org.simantics.databoard.binding.error.BindingConstructionException;\r
21 import org.simantics.databoard.type.RecordType;\r
22 \r
23 /**\r
24  * AsmBindingFactory creates code-generated bindings for classes.\r
25  * \r
26  * @author toni.kalajainen\r
27  */\r
28 public class AsmBindingProvider implements RecordBindingProvider {\r
29         \r
30         WeakHashMap<ClassLoader, AsmBindingClassLoader> bindingClassLoaders = new WeakHashMap<ClassLoader, AsmBindingClassLoader>();\r
31 \r
32         public AsmBindingProvider() {           \r
33         }\r
34 \r
35         @Override\r
36         public synchronized RecordBinding provideRecordBinding(Class<?> clazz, RecordType type)\r
37                         throws BindingConstructionException {\r
38                 \r
39                 ClassLoader cl = clazz.getClassLoader();\r
40                 if (cl == null) {\r
41                         cl = Thread.currentThread().getContextClassLoader();\r
42                 }\r
43                 AsmBindingClassLoader bcl = bindingClassLoaders.get(cl);\r
44                 if ( bcl == null ) {\r
45                         bcl = new AsmBindingClassLoader(cl);\r
46                         bindingClassLoaders.put(cl, bcl);\r
47                 }\r
48                 \r
49                 ClassInfo ci = ClassInfo.getInfo(clazz);\r
50                 try {                   \r
51                         Class<?> bindingClass = bcl.getBindingClass( clazz );\r
52                         Constructor<?> c = bindingClass.getConstructor( RecordType.class );\r
53                         RecordBinding binding = (RecordBinding) c.newInstance( type );\r
54                 binding.componentBindings = new Binding[ ci.fields.length ];  \r
55                         return binding;\r
56                 } catch (InstantiationException e) {\r
57                         throw new BindingConstructionException(e);\r
58                 } catch (IllegalAccessException e) {\r
59                         throw new BindingConstructionException(e);\r
60                 } catch (SecurityException e) {\r
61                         throw new BindingConstructionException(e);\r
62                 } catch (NoSuchMethodException e) {\r
63                         throw new BindingConstructionException(e);\r
64                 } catch (IllegalArgumentException e) {\r
65                         throw new BindingConstructionException(e);\r
66                 } catch (InvocationTargetException e) {\r
67                         throw new BindingConstructionException(e);\r
68                 } catch (ClassNotFoundException e) {\r
69                         throw new BindingConstructionException(e);\r
70                 }\r
71         }\r
72                         \r
73 }\r