-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethodBasedModule.java
146 lines (127 loc) · 4.34 KB
/
MethodBasedModule.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
* Copyright (c) 2009, 2013 itemis AG (http://www.itemis.eu) and others.
* 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:
* itemis AG - Initial contribution and API
*******************************************************************************/
package org.eclipse.emf.parsley.runtime.service;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import org.apache.log4j.Logger;
import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.binder.LinkedBindingBuilder;
/**
* @author Sebastian Zarnekow - Initial contribution and API
* @author Sven Efftinge
*/
public abstract class MethodBasedModule implements Module {
private static final Logger LOGGER = Logger.getLogger(MethodBasedModule.class);
private final Method method;
private final Object owner;
protected MethodBasedModule(Method method, Object owner) {
this.method = method;
this.owner = owner;
}
public Method getMethod() {
return method;
}
public Object getOwner() {
return owner;
}
@Override
@SuppressWarnings("unchecked")
public void configure(Binder binder) {
Type key = getKeyType();
if (isClassBinding()) {
Class<?> value = (Class<?>) invokeMethod();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Adding binding from " + key + " to " + value.getName()
+ ". Declaring Method was '" + getMethod().toGenericString() + "' in Module "
+ this.getClass().getName());
}
if (value != null && !Void.class.equals(value)) {
LinkedBindingBuilder<Object> bind = binder.bind((Key<Object>)Key.get(key));
if (!key.equals(value)) {
bindToClass(bind, value);
}
if (isEager()) {
bind.asEagerSingleton();
} else if (isSingleton()) {
bind.in(Scopes.SINGLETON);
}
}
} else {
Object instance = invokeMethod();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(
"Adding binding from " + getMethod().getReturnType().getName() +
" to instance " + instance.toString()
+ ". Declaring Method was '" + getMethod().toGenericString() + "' in Module "
+ this.getClass().getName());
}
LinkedBindingBuilder<Object> bind = binder.bind((Key<Object>)Key.get(key));
bindToInstance(bind, instance);
}
}
protected void bindToClass(LinkedBindingBuilder<Object> bind, Class<?> value) {
bind.to(value);
}
protected void bindToInstance(LinkedBindingBuilder<Object> bind, Object instance) {
bind.toInstance(instance);
}
protected boolean isSame(Type typeA, Type typeB) {
return typeA.equals(typeB);
}
public Type getKeyType() {
Type genericReturnType = getMethod().getGenericReturnType();
if (isClassBinding()) {
Type type = genericReturnType;
if (!(type instanceof ParameterizedType)) {
throw throwIllegalReturnTypeDeclaration(getMethod());
}
return getFirstTypeParameter((ParameterizedType) type);
} else {
return genericReturnType;
}
}
protected Type getFirstTypeParameter(ParameterizedType type) {
Type firstParam = type.getActualTypeArguments()[0];
if (firstParam instanceof WildcardType) {
return ((WildcardType)firstParam).getUpperBounds()[0];
}
return firstParam;
}
protected IllegalStateException throwIllegalReturnTypeDeclaration(Method method) {
return new IllegalStateException("return type of "+method.getName()+" should be declared with wildcard and upperbound (i.e. Class<? extends IScopeProvider>)");
}
public boolean isClassBinding() {
return Class.class.equals(getMethod().getReturnType());
}
public boolean isEager() {
SingletonBinding binding = getMethod().getAnnotation(SingletonBinding.class);
if (binding != null) {
return binding.eager();
}
return false;
}
public boolean isSingleton() {
return getMethod().getAnnotation(SingletonBinding.class) != null;
}
public Object invokeMethod(Object... parameters) {
try {
getMethod().setAccessible(true);
return getMethod().invoke(owner, parameters);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}