Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package dk.jlo.util;
  2.  
  3.  
  4. import java.lang.reflect.Field;
  5.  
  6. /**
  7. * Injects an object into a (private) field in a bean; primarily for poor man's injection for unit testing when not running in a Dependency Injection environment.<p>
  8. * Travels through super classes if necessary.<p>
  9. * Usage:
  10. * <pre>
  11. * import static dk.jlo.util.PoorMansInjector.inject;
  12. * [...]
  13. * inject(object).intoField("fieldName").inBean(bean);
  14. * </pre>
  15. */
  16. @SuppressWarnings("unused")
  17. public class PoorMansInjector {
  18. /**
  19. * Instantiates the injector.
  20. *
  21. * @param object the object to be injected.
  22. * @return The injector.
  23. */
  24. public static FieldInjector inject(Object object) {
  25. return new FieldInjector(object);
  26. }
  27.  
  28. public static class FieldInjector {
  29. private Object object;
  30.  
  31. FieldInjector(Object object) {
  32. this.object = object;
  33. }
  34.  
  35. /**
  36. * Tells the injector where to inject the previously given object.
  37. *
  38. * @param privateFieldName the (usually private) field in question.
  39. * @return The injector.
  40. */
  41. public FinalInjector intoField(String privateFieldName) {
  42. FinalInjector finalInjector = new FinalInjector(object, privateFieldName);
  43. object = null;
  44. return finalInjector;
  45. }
  46. }
  47.  
  48. public static class FinalInjector {
  49. private Object object;
  50. private String privateFieldName;
  51.  
  52. FinalInjector(Object object, String privateFieldName) {
  53. this.object = object;
  54. this.privateFieldName = privateFieldName;
  55. }
  56.  
  57. /**
  58. * Performs the injection.
  59. *
  60. * @param bean the bean/object containing the field.
  61. * @throws IllegalAccessException in case the injection is somehow not possible even after setting the field accessible.
  62. * @throws NoSuchFieldException in case the field is not found in the class or superclass(es).
  63. */
  64. public void inBean(Object bean) throws IllegalAccessException, NoSuchFieldException {
  65. Field beanField = getFieldInClassHierarchy(bean, privateFieldName);
  66. beanField.setAccessible(true);
  67. beanField.set(bean, object);
  68. object = null;
  69. privateFieldName = null;
  70. }
  71.  
  72. private Field getFieldInClassHierarchy(Object bean, String fieldName) throws NoSuchFieldException {
  73. Field beanField = null;
  74. Class<?> aClass = bean.getClass();
  75. do {
  76. try {
  77. beanField = aClass.getDeclaredField(fieldName);
  78. } catch (NoSuchFieldException e) {
  79. aClass = aClass.getSuperclass();
  80. if (aClass == null) {
  81. throw e;
  82. }
  83. }
  84. } while (beanField == null);
  85. return beanField;
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement