private static boolean invokeLifecycleMethod()

in core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java [178:271]


    private static boolean invokeLifecycleMethod(Object target, Class<?> targetClass,
            String methodName, boolean allowIntegerArgument,
            MockComponentContext componentContext, Map<String,Object> properties) {

        return findAndInvokeNearestMethod(targetClass, candidateClass -> {
            // 1. componentContext
            Method method = getMethod(candidateClass, methodName, new Class<?>[] { ComponentContext.class });
            if (method != null) {
                invokeMethod(target, method, new Object[] { componentContext });
                return true;
            }

            // 2. bundleContext
            method = getMethod(candidateClass, methodName, new Class<?>[] { BundleContext.class });
            if (method != null) {
                invokeMethod(target, method, new Object[] { componentContext.getBundleContext() });
                return true;
            }

            // 3. map
            method = getMethod(candidateClass, methodName, new Class<?>[] { Map.class });
            if (method != null) {
                invokeMethod(target, method, new Object[] { componentContext.getPropertiesAsMap() });
                return true;
            }

            // 4. Component property type (annotation lass)
            method = getMethod(candidateClass, methodName, new Class<?>[] { Annotation.class });
            if (method != null) {
                invokeMethod(target, method, new Object[] { Annotations.toObject(method.getParameterTypes()[0],
                        componentContext.getPropertiesAsMap(),
                        componentContext.getBundleContext().getBundle(), false) });
                return true;
            }

            // 5. int (deactivation only)
            if (allowIntegerArgument) {
                method = getMethod(candidateClass, methodName, new Class<?>[] { int.class });
                if (method != null) {
                    invokeMethod(target, method, new Object[] { 0 });
                    return true;
                }
            }

            // 6. Integer (deactivation only)
            if (allowIntegerArgument) {
                method = getMethod(candidateClass, methodName, new Class<?>[] { Integer.class });
                if (method != null) {
                    invokeMethod(target, method, new Object[] { 0 });
                    return true;
                }
            }

            // 7. mixed arguments
            Class<?>[] mixedArgsAllowed = allowIntegerArgument ?
                    new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, Annotation.class, int.class, Integer.class }
                    : new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, Annotation.class };
            method = getMethodWithAnyCombinationArgs(candidateClass, methodName, mixedArgsAllowed);
            if (method != null) {
                Object[] args = new Object[method.getParameterTypes().length];
                for (int i=0; i<args.length; i++) {
                    if (method.getParameterTypes()[i] == ComponentContext.class) {
                        args[i] = componentContext;
                    }
                    else if (method.getParameterTypes()[i] == BundleContext.class) {
                        args[i] = componentContext.getBundleContext();
                    }
                    else if (method.getParameterTypes()[i] == Map.class) {
                        args[i] = componentContext.getPropertiesAsMap();
                    }
                    else if (method.getParameterTypes()[i].isAnnotation()) {
                        args[i] = Annotations.toObject(method.getParameterTypes()[i],
                                componentContext.getPropertiesAsMap(),
                                componentContext.getBundleContext().getBundle(), false);
                    }
                    else if (method.getParameterTypes()[i] == int.class || method.getParameterTypes()[i] == Integer.class) {
                        args[i] = 0;
                    }
                }
                invokeMethod(target, method, args);
                return true;
            }

            // 8. noargs
            method = getMethod(candidateClass, methodName, new Class<?>[0]);
            if (method != null) {
                invokeMethod(target, method, new Object[0]);
                return true;
            }

            // no match found
            return false;
        });
    }