1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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.databoard.binding.reflection;
\r
14 import java.lang.reflect.Constructor;
\r
15 import java.lang.reflect.InvocationTargetException;
\r
16 import java.util.WeakHashMap;
\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
24 * AsmBindingFactory creates code-generated bindings for classes.
\r
26 * @author toni.kalajainen
\r
28 public class AsmBindingProvider implements RecordBindingProvider {
\r
30 WeakHashMap<ClassLoader, AsmBindingClassLoader> bindingClassLoaders = new WeakHashMap<ClassLoader, AsmBindingClassLoader>();
\r
32 public AsmBindingProvider() {
\r
36 public synchronized RecordBinding provideRecordBinding(Class<?> clazz, RecordType type)
\r
37 throws BindingConstructionException {
\r
39 ClassLoader cl = clazz.getClassLoader();
\r
41 cl = Thread.currentThread().getContextClassLoader();
\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
49 ClassInfo ci = ClassInfo.getInfo(clazz);
\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
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