diff --git a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/ContextInjectionFactory.java b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/ContextInjectionFactory.java index 3b06e0c..9fa0ddc 100644 --- a/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/ContextInjectionFactory.java +++ b/bundles/org.eclipse.e4.core.contexts/src/org/eclipse/e4/core/contexts/ContextInjectionFactory.java @@ -132,6 +132,18 @@ } /** + * Call all annotated methods, injecting the parameters from the context. + * @param object The object to perform injection on + * @param qualifier the annotation tagging method to be called + * @param context The context to obtain injected values from + * @throws InjectionException if an exception occurred while performing this operation + */ + static public void invokeAll(Object object, Class qualifier, IEclipseContext context) throws InjectionException { + PrimaryObjectSupplier supplier = ContextObjectSupplier.getObjectSupplier(context, injector); + injector.invokeAll(object, qualifier, supplier); + } + + /** * Un-injects the context from the object. The un-injection requires that all injected * values were marked as optional, or the un-injection will fail. * diff --git a/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/di/IInjector.java b/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/di/IInjector.java index b4d08d1..afc22b7 100644 --- a/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/di/IInjector.java +++ b/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/di/IInjector.java @@ -117,6 +117,15 @@ public Object invoke(Object object, Class qualifier, Object defaultValue, PrimaryObjectSupplier objectSupplier, PrimaryObjectSupplier localSupplier) throws InjectionException; /** + * Call all annotated methods, injecting the parameters from the context. + * @param object The object to perform injection on + * @param qualifier the annotation tagging method to be called + * @param objectSupplier primary object supplier + * @throws InjectionException if an exception occurred while performing this operation + */ + public void invokeAll(Object object, Class qualifier, PrimaryObjectSupplier objectSupplier) throws InjectionException; + + /** * Obtain an instance of the specified class and inject it with the data from the supplier. * @param the type of the object to be created * @param clazz the class to be instantiated diff --git a/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/InjectorImpl.java b/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/InjectorImpl.java index 7eac2f4..bdab3b4 100644 --- a/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/InjectorImpl.java +++ b/bundles/org.eclipse.e4.core.di/src/org/eclipse/e4/core/internal/di/InjectorImpl.java @@ -235,6 +235,35 @@ return invokeUsingClass(userObject, superClass, qualifier, defaultValue, objectSupplier, tempSupplier, throwUnresolved); } + public void invokeAll(Object object, Class qualifier, PrimaryObjectSupplier objectSupplier) { + invokeAllUsingClass(object, object.getClass(), qualifier, objectSupplier, null, false); + } + + private void invokeAllUsingClass(Object userObject, Class currentClass, Class qualifier, PrimaryObjectSupplier objectSupplier, PrimaryObjectSupplier tempSupplier, boolean throwUnresolved) { + Method[] methods = getDeclaredMethods(currentClass); + for (int j = 0; j < methods.length; j++) { + Method method = methods[j]; + if (method.getAnnotation(qualifier) == null) + continue; + MethodRequestor requestor = new MethodRequestor(method, this, objectSupplier, tempSupplier, userObject, false); + + Object[] actualArgs = resolveArgs(requestor, objectSupplier, tempSupplier, false, true, false); + int unresolved = unresolved(actualArgs); + if (unresolved != -1) { + if (throwUnresolved) + reportUnresolvedArgument(requestor, unresolved); + continue; + } + requestor.setResolvedArgs(actualArgs); + requestor.execute(); + } + Class superClass = currentClass.getSuperclass(); + if (superClass == null) + return; + + invokeAllUsingClass(userObject, superClass, qualifier, objectSupplier, tempSupplier, throwUnresolved); + } + public T make(Class clazz, PrimaryObjectSupplier objectSupplier) { Class implementationClass = getImplementationClass(clazz); return clazz.cast(internalMake(implementationClass, objectSupplier, null));