PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/test/com/googlecode/guice/bundle/OSGiTestActivator.java

https://gitlab.com/metamorphiccode/guice
Java | 511 lines | 281 code | 192 blank | 38 comment | 8 complexity | 34714b9af2d4079d03698575a54f8dd8 MD5 | raw file
  1. /**
  2. * Copyright (C) 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.googlecode.guice.bundle;
  17. import static com.google.inject.name.Names.named;
  18. import com.google.inject.AbstractModule;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Inject;
  21. import com.google.inject.Injector;
  22. import com.google.inject.Key;
  23. import com.google.inject.matcher.AbstractMatcher;
  24. import org.osgi.framework.Bundle;
  25. import org.osgi.framework.BundleActivator;
  26. import org.osgi.framework.BundleContext;
  27. import org.osgi.framework.BundleException;
  28. import java.lang.reflect.Method;
  29. import java.lang.reflect.Modifier;
  30. import java.util.Random;
  31. /**
  32. * Test Guice from inside an OSGi bundle activator.
  33. *
  34. * @author mcculls@gmail.com (Stuart McCulloch)
  35. */
  36. @SuppressWarnings("unused") public class OSGiTestActivator
  37. implements BundleActivator {
  38. // varying visibilities to test our code-generation support
  39. public static class Undefined {}
  40. public interface A {}
  41. protected interface B {}
  42. interface C {}
  43. private interface D {}
  44. public static class AA
  45. implements A {
  46. public AA() {}
  47. @Inject public void setA(Undefined undefined) {}
  48. @Inject protected void setB(Undefined undefined) {}
  49. @Inject void setC(Undefined undefined) {}
  50. @Inject private void setD(Undefined undefined) {}
  51. @Inject public Undefined a;
  52. @Inject protected Undefined b;
  53. @Inject Undefined c;
  54. @Inject private Undefined d;
  55. }
  56. protected static class AB
  57. implements A {
  58. public AB() {}
  59. @Inject public void setA(Undefined undefined) {}
  60. @Inject protected void setB(Undefined undefined) {}
  61. @Inject void setC(Undefined undefined) {}
  62. @Inject private void setD(Undefined undefined) {}
  63. @Inject public Undefined a;
  64. @Inject protected Undefined b;
  65. @Inject Undefined c;
  66. @Inject private Undefined d;
  67. }
  68. static class AC
  69. implements A {
  70. public AC() {}
  71. @Inject public void setA(Undefined undefined) {}
  72. @Inject protected void setB(Undefined undefined) {}
  73. @Inject void setC(Undefined undefined) {}
  74. @Inject private void setD(Undefined undefined) {}
  75. @Inject public Undefined a;
  76. @Inject protected Undefined b;
  77. @Inject Undefined c;
  78. @Inject private Undefined d;
  79. }
  80. private static class AD
  81. implements A {
  82. public AD() {}
  83. @Inject public void setA(Undefined undefined) {}
  84. @Inject protected void setB(Undefined undefined) {}
  85. @Inject void setC(Undefined undefined) {}
  86. @Inject private void setD(Undefined undefined) {}
  87. @Inject public Undefined a;
  88. @Inject protected Undefined b;
  89. @Inject Undefined c;
  90. @Inject private Undefined d;
  91. }
  92. public static class BA
  93. implements B {
  94. protected BA() {}
  95. @Inject public void setA(Undefined undefined) {}
  96. @Inject protected void setB(Undefined undefined) {}
  97. @Inject void setC(Undefined undefined) {}
  98. @Inject private void setD(Undefined undefined) {}
  99. @Inject public Undefined a;
  100. @Inject protected Undefined b;
  101. @Inject Undefined c;
  102. @Inject private Undefined d;
  103. }
  104. protected static class BB
  105. implements B {
  106. protected BB() {}
  107. @Inject public void setA(Undefined undefined) {}
  108. @Inject protected void setB(Undefined undefined) {}
  109. @Inject void setC(Undefined undefined) {}
  110. @Inject private void setD(Undefined undefined) {}
  111. @Inject public Undefined a;
  112. @Inject protected Undefined b;
  113. @Inject Undefined c;
  114. @Inject private Undefined d;
  115. }
  116. static class BC
  117. implements B {
  118. protected BC() {}
  119. @Inject public void setA(Undefined undefined) {}
  120. @Inject protected void setB(Undefined undefined) {}
  121. @Inject void setC(Undefined undefined) {}
  122. @Inject private void setD(Undefined undefined) {}
  123. @Inject public Undefined a;
  124. @Inject protected Undefined b;
  125. @Inject Undefined c;
  126. @Inject private Undefined d;
  127. }
  128. private static class BD
  129. implements B {
  130. protected BD() {}
  131. @Inject public void setA(Undefined undefined) {}
  132. @Inject protected void setB(Undefined undefined) {}
  133. @Inject void setC(Undefined undefined) {}
  134. @Inject private void setD(Undefined undefined) {}
  135. @Inject public Undefined a;
  136. @Inject protected Undefined b;
  137. @Inject Undefined c;
  138. @Inject private Undefined d;
  139. }
  140. public static class CA
  141. implements C {
  142. CA() {}
  143. @Inject public void setA(Undefined undefined) {}
  144. @Inject protected void setB(Undefined undefined) {}
  145. @Inject void setC(Undefined undefined) {}
  146. @Inject private void setD(Undefined undefined) {}
  147. @Inject public Undefined a;
  148. @Inject protected Undefined b;
  149. @Inject Undefined c;
  150. @Inject private Undefined d;
  151. }
  152. protected static class CB
  153. implements C {
  154. CB() {}
  155. @Inject public void setA(Undefined undefined) {}
  156. @Inject protected void setB(Undefined undefined) {}
  157. @Inject void setC(Undefined undefined) {}
  158. @Inject private void setD(Undefined undefined) {}
  159. @Inject public Undefined a;
  160. @Inject protected Undefined b;
  161. @Inject Undefined c;
  162. @Inject private Undefined d;
  163. }
  164. static class CC
  165. implements C {
  166. CC() {}
  167. @Inject public void setA(Undefined undefined) {}
  168. @Inject protected void setB(Undefined undefined) {}
  169. @Inject void setC(Undefined undefined) {}
  170. @Inject private void setD(Undefined undefined) {}
  171. @Inject public Undefined a;
  172. @Inject protected Undefined b;
  173. @Inject Undefined c;
  174. @Inject private Undefined d;
  175. }
  176. private static class CD
  177. implements C {
  178. CD() {}
  179. @Inject public void setA(Undefined undefined) {}
  180. @Inject protected void setB(Undefined undefined) {}
  181. @Inject void setC(Undefined undefined) {}
  182. @Inject private void setD(Undefined undefined) {}
  183. @Inject public Undefined a;
  184. @Inject protected Undefined b;
  185. @Inject Undefined c;
  186. @Inject private Undefined d;
  187. }
  188. public static class DA
  189. implements D {
  190. @Inject private DA() {}
  191. @Inject public void setA(Undefined undefined) {}
  192. @Inject protected void setB(Undefined undefined) {}
  193. @Inject void setC(Undefined undefined) {}
  194. @Inject private void setD(Undefined undefined) {}
  195. @Inject public Undefined a;
  196. @Inject protected Undefined b;
  197. @Inject Undefined c;
  198. @Inject private Undefined d;
  199. }
  200. protected static class DB
  201. implements D {
  202. @Inject private DB() {}
  203. @Inject public void setA(Undefined undefined) {}
  204. @Inject protected void setB(Undefined undefined) {}
  205. @Inject void setC(Undefined undefined) {}
  206. @Inject private void setD(Undefined undefined) {}
  207. @Inject public Undefined a;
  208. @Inject protected Undefined b;
  209. @Inject Undefined c;
  210. @Inject private Undefined d;
  211. }
  212. static class DC
  213. implements D {
  214. @Inject private DC() {}
  215. @Inject public void setA(Undefined undefined) {}
  216. @Inject protected void setB(Undefined undefined) {}
  217. @Inject void setC(Undefined undefined) {}
  218. @Inject private void setD(Undefined undefined) {}
  219. @Inject public Undefined a;
  220. @Inject protected Undefined b;
  221. @Inject Undefined c;
  222. @Inject private Undefined d;
  223. }
  224. private static class DD
  225. implements D {
  226. private DD() {}
  227. @Inject public void setA(Undefined undefined) {}
  228. @Inject protected void setB(Undefined undefined) {}
  229. @Inject void setC(Undefined undefined) {}
  230. @Inject private void setD(Undefined undefined) {}
  231. @Inject public Undefined a;
  232. @Inject protected Undefined b;
  233. @Inject Undefined c;
  234. @Inject private Undefined d;
  235. }
  236. enum Visibility {
  237. PUBLIC, PROTECTED, PACKAGE_PRIVATE, PRIVATE
  238. }
  239. static final Class<?>[] TEST_CLAZZES = {A.class, B.class, C.class, D.class};
  240. // registers all the class combinations
  241. static class TestModule
  242. extends AbstractModule {
  243. final Bundle bundle;
  244. TestModule(Bundle bundle) {
  245. this.bundle = bundle;
  246. }
  247. @Override @SuppressWarnings("unchecked") protected void configure() {
  248. for (Class<?> api : TEST_CLAZZES) {
  249. for (Visibility visibility : Visibility.values()) {
  250. try {
  251. // this registers: A + PUBLIC -> AA, A + PROTECTED -> AB, etc...
  252. String suffix = TEST_CLAZZES[visibility.ordinal()].getSimpleName();
  253. Class imp = bundle.loadClass(api.getName() + suffix);
  254. bind(api).annotatedWith(named(visibility.name())).to(imp);
  255. } catch (ClassNotFoundException e) {
  256. throw new RuntimeException("Unable to load test class", e);
  257. }
  258. }
  259. }
  260. }
  261. }
  262. /*if[AOP]*/
  263. // applies method-interception to classes with enough visibility
  264. static class InterceptorModule
  265. extends AbstractModule {
  266. @Override protected void configure() {
  267. bindInterceptor(new AbstractMatcher<Class<?>>() {
  268. public boolean matches(Class<?> clazz) {
  269. try {
  270. // the class and constructor must be visible
  271. int clazzModifiers = clazz.getModifiers();
  272. int ctorModifiers = clazz.getConstructor().getModifiers();
  273. return (clazzModifiers & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0
  274. && (ctorModifiers & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0;
  275. } catch (NoSuchMethodException e) {
  276. return false;
  277. }
  278. }
  279. }, new AbstractMatcher<Method>() {
  280. public boolean matches(Method method) {
  281. // the intercepted method must also be visible
  282. int methodModifiers = method.getModifiers();
  283. return (methodModifiers & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0;
  284. }
  285. }, new org.aopalliance.intercept.MethodInterceptor() {
  286. public Object invoke(org.aopalliance.intercept.MethodInvocation mi)
  287. throws Throwable {
  288. return mi.proceed();
  289. }
  290. });
  291. }
  292. }
  293. /*end[AOP]*/
  294. // called from OSGi when bundle starts
  295. public void start(BundleContext context)
  296. throws BundleException {
  297. final Bundle bundle = context.getBundle();
  298. Injector injector = Guice.createInjector(new TestModule(bundle));
  299. /*if[AOP]*/
  300. Injector aopInjector = Guice.createInjector(new TestModule(bundle), new InterceptorModule());
  301. /*end[AOP]*/
  302. // test code-generation support
  303. for (Class<?> api : TEST_CLAZZES) {
  304. for (Visibility vis : Visibility.values()) {
  305. injector.getInstance(Key.get(api, named(vis.name())));
  306. /*if[AOP]*/
  307. aopInjector.getInstance(Key.get(api, named(vis.name())));
  308. /*end[AOP]*/
  309. }
  310. }
  311. // test injection of system class (issue 343)
  312. injector.getInstance(Random.class);
  313. /*if[AOP]*/
  314. aopInjector.getInstance(Random.class);
  315. /*end[AOP]*/
  316. }
  317. // called from OSGi when bundle stops
  318. public void stop(BundleContext context) {}
  319. }