1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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.databoard.binding.reflection;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.WeakHashMap;
18 import org.simantics.databoard.binding.Binding;
19 import org.simantics.databoard.binding.RecordBinding;
20 import org.simantics.databoard.binding.error.BindingConstructionException;
21 import org.simantics.databoard.type.RecordType;
24 * AsmBindingFactory creates code-generated bindings for classes.
26 * @author toni.kalajainen
28 public class AsmBindingProvider implements RecordBindingProvider {
30 WeakHashMap<ClassLoader, AsmBindingClassLoader> bindingClassLoaders = new WeakHashMap<ClassLoader, AsmBindingClassLoader>();
32 public AsmBindingProvider() {
36 public synchronized RecordBinding provideRecordBinding(Class<?> clazz, RecordType type)
37 throws BindingConstructionException {
39 ClassLoader cl = clazz.getClassLoader();
41 cl = Thread.currentThread().getContextClassLoader();
43 AsmBindingClassLoader bcl = bindingClassLoaders.get(cl);
45 bcl = new AsmBindingClassLoader(cl);
46 bindingClassLoaders.put(cl, bcl);
49 ClassInfo ci = ClassInfo.getInfo(clazz);
51 Class<?> bindingClass = bcl.getBindingClass( clazz );
52 Constructor<?> c = bindingClass.getConstructor( RecordType.class );
53 RecordBinding binding = (RecordBinding) c.newInstance( type );
54 binding.componentBindings = new Binding[ ci.fields.length ];
56 } catch (InstantiationException e) {
57 throw new BindingConstructionException(e);
58 } catch (IllegalAccessException e) {
59 throw new BindingConstructionException(e);
60 } catch (SecurityException e) {
61 throw new BindingConstructionException(e);
62 } catch (NoSuchMethodException e) {
63 throw new BindingConstructionException(e);
64 } catch (IllegalArgumentException e) {
65 throw new BindingConstructionException(e);
66 } catch (InvocationTargetException e) {
67 throw new BindingConstructionException(e.getCause());
68 } catch (ClassNotFoundException e) {
69 throw new BindingConstructionException(e);