Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds support for manually kicking off an instance's destruction with …

…Injector#destroyInstance
  • Loading branch information...
commit 8191b1a88077782140e7dbc6be43983bccc08b46 1 parent aa79289
@tschneidereit authored
View
23 src/org/swiftsuspenders/injection/Injector.as
@@ -23,6 +23,7 @@ package org.swiftsuspenders.injection
import org.swiftsuspenders.reflection.Reflector;
import org.swiftsuspenders.typedescriptions.ConstructorInjectionPoint;
import org.swiftsuspenders.typedescriptions.InjectionPoint;
+ import org.swiftsuspenders.typedescriptions.PreDestroyInjectionPoint;
import org.swiftsuspenders.typedescriptions.TypeDescription;
import org.swiftsuspenders.utils.TypeDescriptor;
import org.swiftsuspenders.utils.SsInternal;
@@ -254,6 +255,28 @@ package org.swiftsuspenders.injection
}
/**
+ * Uses the <code>TypeDescription</code> the injector associates with the given instance's
+ * type to iterate over all <code>[PreDestroy]</code> methods in the instance, supporting
+ * automated destruction.
+ *
+ * @param instance The instance to destroy
+ */
+ public function destroyInstance(instance : Object) : void
+ {
+ if (!instance)
+ {
+ return;
+ }
+ const type : Class = _reflector.getClass(instance);
+ const typeDescription : TypeDescription = getTypeDescription(type);
+ for (var preDestroyHook : PreDestroyInjectionPoint = typeDescription.preDestroyMethods;
+ preDestroyHook; preDestroyHook = PreDestroyInjectionPoint(preDestroyHook.next))
+ {
+ preDestroyHook.applyInjection(instance, type, this);
+ }
+ }
+
+ /**
* Indicates whether the injector can supply a response for the specified dependency either
* by using a mapping of its own or by querying one of its ancestor injectors.
*
View
9 test/org/swiftsuspenders/InjectorTests.as
@@ -913,5 +913,14 @@ package org.swiftsuspenders
injector.unmap(Clazz);
assertThat(singleton, hasPropertyWithValue("preDestroyCalled", true));
}
+
+ [Test]
+ public function injectorDestroyInstanceInvokesPreDestroyMethodsOnInstance() : void
+ {
+ const target : Clazz = new Clazz();
+ assertThat(target, hasPropertyWithValue("preDestroyCalled", false));
+ injector.destroyInstance(target);
+ assertThat(target, hasPropertyWithValue("preDestroyCalled", true));
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.