Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency-Injection for superclasses..... #65

Open
agallus opened this issue Jun 21, 2016 · 2 comments
Open

Dependency-Injection for superclasses..... #65

agallus opened this issue Jun 21, 2016 · 2 comments

Comments

@agallus
Copy link

agallus commented Jun 21, 2016

The current implementation of method
public static T instantiatePresenter(Class clazz, Function<String, Object> injectionContext)
in class "Injector" does not apply the injectionContext to superclasses of the passed controller-class "clazz".

Maybe it is possible to also inject superclass-fields like has been implemented in method
public static void injectMembers(Class<? extends Object> clazz, final Object instance) ????

Regards
Axel

@matthiasthieroff
Copy link

+1

@paulhudsonx
Copy link

Additionally the injection context is applied after the injector has already instantiated default objects for injectable fields, which has the additional problem that if field is an interface or an abstract class the injectors default instance supplier falls over when it tries to instantiate the field throwing an IllegalStateException exception. Obviously you can set the instance supplier on the injector, but I think that only really solves it for fields that represent services.

So we have:

  1. Injection context is not propagated to super classes
  2. Unnecessary instantiation of default objects is performed prior to applying the injection context
  3. The unnecessary instantiation of default objects fails in the default instance supplier if the type cannot be constructed, say because it the type is an interface, abstract class etc.

I have a fix for 1) & 2) that also allows you to work around 3) by supplying an appropriate candidate in the injection context.

The fix #85 works by passing the injection context to the injectMembers method which then prioritizes the injectionContext over the configurator and default instantiation. Note that the original injectMembers method has been preserved for backwards compatability.

Modified instantiatePresenter, now performs field injection in one pass

public static <T> T instantiatePresenter(Class<T> clazz, Function<String, Object> injectionContext) {
    @SuppressWarnings("unchecked")
    T presenter = (T) instanceSupplier.apply(clazz);
    injectMembers(clazz, presenter, injectionContext);
    initialize(presenter);
    presenters.add(presenter);

    return presenter;
  }

Original method that now calls new injectMembers with an null injectionContext

  public static void injectMembers(Class<? extends Object> clazz, final Object instance) throws SecurityException {
    injectMembers(clazz, instance, null);
  }

New (reworked injectMembers) now takes an injectionContext, which is also propagated to super classes.

  public static void injectMembers(Class<? extends Object> clazz, final Object instance, Function<String, Object> injectionContext) throws SecurityException {
    LOG.accept("Injecting members for class " + clazz + " and instance " + instance);

    Field[] fields = clazz.getDeclaredFields();

    for (final Field field : fields) {

      if (field.isAnnotationPresent(Inject.class)) {
        LOG.accept("Field annotated with @Inject found: " + field);

        Class<?> type = field.getType();
        String key = field.getName();
        Object value;

        if ((injectionContext != null) && ((value = injectionContext.apply(key)) != null)) {

          // Found injectable content in injectionContext
          LOG.accept("Injection context supplied [" + value + "]");
          injectIntoField(field, instance, value);
        } else if ((value = configurator.getProperty(clazz, key)) != null) {

          // Found injectable content in configurator
          LOG.accept("Configurator context supplied [" + value + "]");
          injectIntoField(field, instance, value);
        } else if (isNotPrimitiveOrString(type)) {

          // Attempt to instantiate the type
          LOG.accept("Instantiating [" + type.getName() + "]");
          value = instantiateModelOrService(type);
          injectIntoField(field, instance, value);
        } else {

          // No injectable content found
          LOG.accept("Warning no injectable content for class [" + clazz.getName() + "] field [" + field.getName() + "]");
        }
      }
    }

    Class<? extends Object> superclass = clazz.getSuperclass();

    if (superclass != null) {
      LOG.accept("Injecting members of: " + superclass);
      injectMembers(superclass, instance, injectionContext);
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants