1 /*******************************************************************************
\r
2 * Copyright (c) 2010 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.method;
14 import org.simantics.databoard.Methods;
\r
15 import org.simantics.databoard.binding.Binding;
\r
16 import org.simantics.databoard.binding.RecordBinding;
\r
17 import org.simantics.databoard.binding.UnionBinding;
\r
20 * MethodTypeBinding is a binding of MethodType (Request, Response and Error data types)
23 * @see Methods#getMethodTypeBinding(java.lang.reflect.Method)
24 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
26 public class MethodTypeBinding {
28 MethodTypeDefinition methodDefinition;
30 RecordBinding requestBinding;
31 Binding responseBinding;
32 UnionBinding errorBinding;
34 public MethodTypeBinding(String methodName, RecordBinding requestBinding, Binding responseBinding, UnionBinding errorBinding)
36 if (methodName==null||requestBinding==null||responseBinding==null||errorBinding==null)
37 throw new IllegalArgumentException("null arg");
38 this.methodDefinition = new MethodTypeDefinition(methodName, new MethodType(requestBinding.type(), responseBinding.type(), errorBinding.type()));
39 this.requestBinding = requestBinding;
40 this.responseBinding = responseBinding;
41 this.errorBinding = errorBinding;
44 public MethodTypeBinding(MethodTypeDefinition methodDescription, RecordBinding requestBinding, Binding responseBinding, UnionBinding errorBinding)
46 if (methodDescription==null||requestBinding==null||responseBinding==null||errorBinding==null)
47 throw new IllegalArgumentException("null arg");
48 this.methodDefinition = methodDescription;
49 this.requestBinding = requestBinding;
50 this.responseBinding = responseBinding;
51 this.errorBinding = errorBinding;
54 public MethodTypeDefinition getMethodDefinition() {
55 return methodDefinition;
58 public MethodType getMethodType() {
59 return methodDefinition.getType();
62 public String getName() {
63 return methodDefinition.getName();
66 public RecordBinding getRequestBinding() {
67 return requestBinding;
70 public Binding getResponseBinding() {
71 return responseBinding;
74 public UnionBinding getErrorBinding() {
78 // Puts error classes into an union type
79 public static Binding error(final Class<?>...errorClasses)
81 UnionType ut = new UnionType();
82 UnionBinding ub = new UnionBinding() {
84 public Object create(int tag, Object value)
85 throws BindingException {
89 public int getTag(Object obj) throws BindingException {
90 for (int i=0; i<errorClasses.length; i++)
91 if (errorClasses[i].isInstance(obj))
93 throw new BindingException("pl");
96 public Object getValue(Object obj) throws BindingException {
100 public boolean isInstance(Object obj) {
101 for (Class<?> clazz : errorClasses)
102 if (clazz.isInstance(obj))
107 ub.componentBindings = new Binding[errorClasses.length];
108 ut.components = new Component[errorClasses.length];
109 for (int i=0; i<errorClasses.length; i++)
111 Component c = new Component();
112 c.name = errorClasses[i].getName();
113 c.type = DataTypes.getDataType(errorClasses[i]);
114 ut.components[i] = c;
115 ub.componentBindings[i] = DataTypes.getBinding(errorClasses[i]);
121 // Puts request arguments to record type
122 public static Binding request(Class<?>...argumentClasses)
127 public static Binding response(Class<?> response)
129 return DataTypes.getBinding(response);