blob: 8f1308f5427781b53bda2fa0b0d532380a03ccda (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*******************************************************************************
* Copyright (c) 2013 THALES GLOBAL SERVICES.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.common.tools.internal.interpreter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.sirius.common.tools.Messages;
import org.eclipse.sirius.common.tools.api.interpreter.EvaluationException;
import com.google.common.base.Preconditions;
/**
* A service which correspond exactly to a single Java method.
*
* @author pcdavid
*/
class MonomorphicService implements IMonomorphicService {
/**
* An instance of the Java class which defines the service method, needed to
* actually invoke the service method.
*/
private final Object serviceInstance;
/**
* The Java method which corresponds to this service.
*/
private final Method serviceMethod;
MonomorphicService(Object serviceInstance, Method serviceMethod) {
this.serviceInstance = Preconditions.checkNotNull(serviceInstance);
this.serviceMethod = Preconditions.checkNotNull(serviceMethod);
Preconditions.checkArgument(ServiceInterpreter.isValidServiceMethod(serviceMethod));
}
public String getName() {
return serviceMethod.getName();
}
@Override
public boolean appliesTo(Object[] target) {
boolean apply = true;
Class<?>[] parameters = serviceMethod.getParameterTypes();
if (parameters.length != target.length) {
apply = false;
} else {
for (int i = 0; i < target.length; i++) {
if ((target[i] != null) && !serviceMethod.getParameterTypes()[i].isAssignableFrom(target[i].getClass())) {
apply = false;
break;
}
}
}
return apply;
}
public Object call(Object[] target) throws EvaluationException {
Object result = null;
try {
result = serviceMethod.invoke(serviceInstance, target);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
fail(e);
}
return result;
}
private void fail(Exception e) throws EvaluationException {
throw new EvaluationException(MessageFormat.format(Messages.MonomorphicService_serviceError, this), e);
}
@Override
public String toString() {
return MessageFormat.format("{0}.{1}({2})", serviceInstance.getClass().getCanonicalName(), serviceMethod.getName(), serviceMethod.getParameterTypes()[0].getCanonicalName()); //$NON-NLS-1$
}
@Override
public List<String> getParametersTypes() {
List<String> parametersTypes = new ArrayList<String>();
for (Class<?> type : serviceMethod.getParameterTypes()) {
parametersTypes.add(type.getSimpleName());
}
return parametersTypes;
}
}
|