Git Product home page Git Product logo

Comments (2)

matthiasthieroff avatar matthiasthieroff commented on August 30, 2024

+1

from afterburner.fx.

paulhudsonx avatar paulhudsonx commented on August 30, 2024

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);
    }
  }

from afterburner.fx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.