private List<Object> getMergedImplementations(final Method method,
                                                final Object[] args,
                                                final Class returnType,
                                                final List<?> implementations,
                                                final boolean intersect) throws IllegalAccessException, InvocationTargetException {

    final List<Object> results = new ArrayList<>();

    if (returnType.isInterface()) {
      final List<Object> orderedPrimaryKeys = new SmartList<>();
      final Map<Object, List<Set<Object>>> map = FactoryMap.create(key -> {
        orderedPrimaryKeys.add(key);
        return new SmartList<>();
      });
      final Map<Object, int[]> counts = FactoryMap.create(key -> new int[implementations.size()]);
      for (int i = 0; i < implementations.size(); i++) {
        Object t = implementations.get(i);
        final Object o = method.invoke(t, args);
        if (o instanceof Collection) {
          for (final Object o1 : (Collection)o) {
            addToMaps(o1, counts, map, i, results, false, intersect);
          }
        }
        else if (o != null) {
          addToMaps(o, counts, map, i, results, true, intersect);
        }

      }

      for (final Object primaryKey : orderedPrimaryKeys) {
        for (final Set<Object> objects : map.get(primaryKey)) {
          results.add(mergeImplementations(returnType, new ArrayList<>(objects)));
        }
      }
    }
    else {
      HashSet<Object> map = new HashSet<>();
      for (final Object t : implementations) {
        final Object o = method.invoke(t, args);
        if (o instanceof Collection) {
          map.addAll((Collection<Object>)o);
        }
        else if (o != null) {
          map.add(o);
          break;
        }
      }
      results.addAll(map);
    }
    return results;
  }
private List<Object> getMergedImplementations(final Method method,
                                                final Object[] args,
                                                final Class returnType,
                                                final List<?> implementations,
                                                final boolean intersect)