Class InjectionEngine

java.lang.Object
de.aristaflow.adept2.util.injection.InjectionEngine

public final class InjectionEngine extends Object
The driver-agnostic engine of the injection mechanism: instance plus bindings become populated Injected fields. It applies no eligibility test of its own – which instances reach it is the rule of the driver, and only the deserialisation driver's rule is being InjectionAware. Calling InjectionAware.afterInjection() is likewise the move of the driver, at the moment its protocol prescribes.

Field discovery covers the complete class tree: the engine walks the superclass chain of the runtime class of the instance and collects the annotated fields every class in it declares, mirroring how serialisation itself handles fields per class. The interfaces the walk offers are skipped, a field of an interface being a constant and never state of an instance; an Injected on such a constant is therefore inert everywhere and deliberately not reported as invalid, by a claim as little as by the declaration scan. A supertype need not be InjectionAware itself for its declared fields to be served on a claimed instance. Annotations are never inherited, so a field declared in a subclass is a slot of its own; where a subclass shadows a supertype field, both are distinct slots. Visibility does not matter, the engine populates through Field.setAccessible(boolean).

The per-class metadata is cached in a ClassValue: buddy-policy classloading funnels classes from many bundles through the engine, and a plain map keyed by Class would pin the keys across bundle refreshes, which a ClassValue does not. The cached value is reachable from its key all the same, so a declaration naming a type of another bundle – Injected.type() or Injected.typeArgs() – keeps that bundle alive as long as the declaring one is loaded. For plain-data classes the cache answers "nothing to do" and the per-object cost is a single lookup. Resolved values are never cached, a logger or an executor being specific to the instance providing it.

  • Method Details

    • inject

      public static void inject(Object instance, BindingContext bindings, Object target, Logger logger) throws InjectionException
      Populates the Injected fields of the designated instance from the designated bindings. A field the bindings do not serve stays untouched, which keeps a driver-free fallback a readObject has seeded; a required one fails instead.
      Parameters:
      instance - The instance which to populate the annotated fields of. This must not be null.
      bindings - The resolution surface of the driver claiming the designated instance. This must not be null.
      target - The target of the invocation carrying the designated instance or null if the driver resolves none. This is passed on to the bindings.
      logger - The logger to report a class carrying invalid declarations with, once per class. This must not be null.
      Throws:
      InjectionException - If a declaration of the designated instance is invalid, a required binding is unavailable, a binding does not match the field it was resolved for or a field cannot be written, an InjectionException will be thrown.
    • getInjectedFields

      protected static Collection<InjectionEngine.InjectedField> getInjectedFields(Class<?> cls) throws InjectionException
      Gets the Injected fields the designated class validly declares in its class tree, in the order the engine populates them. An invalid declaration fails with the very exception a claim would fail with, so a build-time test can validate the declarations where they are written instead of where they are claimed. Interface fields are outside the walk, so an annotation on an interface constant is not reported here either. The tests of this package are its only callers, hence the visibility: no driver needs the slots, and handing out accessible fields is nothing to offer a bundle.

      The slots carry what the declarations state rather than only the fields, so a test asserting something about a declaration reads it here instead of reflecting over the annotation itself – the effective type and the type arguments above all, which the engine derives from the annotation and the generic signature together.

      Parameters:
      cls - The class of which to get the annotated fields of its class tree.
      Returns:
      The valid slots of the class tree of the designated class in population order. This will never be null but it may be empty.
      Throws:
      InjectionException - If a declaration of the class tree of the designated class is invalid, an InjectionException stating every invalid declaration will be thrown.