/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2020 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.qa.glnm.sampling.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.stream.Collectors;

import io.wcm.qa.glnm.exceptions.GaleniumException;
import io.wcm.qa.glnm.sampling.Sampler;

/**
 * Generic sampler construction.
 *
 * @since 5.0.0
 */
public final class SamplerFactory {

  private SamplerFactory() {
    // do not instantiate
  }

  /**
   * Factory method for constructing samplers from class and
   * constructor parameters.
   *
   * @param samplerClass class of sampler
   * @param constructorParams parameters for constructor
   * @return an instance of a sampler
   */
  public static Sampler instance(
      Class<? extends Sampler> samplerClass,
      Object... constructorParams) {
    try {
      Class[] parameterTypes = Arrays.stream(constructorParams)
          .map(param -> param.getClass()).collect(Collectors.toList()).toArray(new Class[0]);
      Constructor constructor = samplerClass.getConstructor(parameterTypes);
      return (Sampler)constructor.newInstance(constructorParams);
    }
    catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
        | SecurityException ex) {
      throw new GaleniumException("When constructing sampler instance.", ex);
    }

  }
}

@@ -660,16 +660,115 @@
 .util;%0A%0A
+import static org.apache.commons.lang3.reflect.ConstructorUtils.getMatchingAccessibleConstructor;%0A%0A
 import j
@@ -1542,166 +1542,20 @@
    C
-lass%5B%5D parameterTypes = Arrays.stream(constructorParams)%0A          .map(param -%3E param.getClass()).collect(Collectors.toList()).toArray(new Class%5B0%5D);%0A      C
+onstructor c
 onst
@@ -1553,33 +1553,38 @@
 tor constructor 
-c
+= getC
 onstructor = sam
@@ -1577,19 +1577,17 @@
 structor
- = 
+(
 samplerC
@@ -1594,13 +1594,11 @@
 lass
-.getC
+, c
 onst
@@ -1607,22 +1607,13 @@
 ctor
-(p
+P
 aram
-eterType
 s);%0A
@@ -1801,32 +1801,8 @@
 tion
- %7C NoSuchMethodException
 %0A   
@@ -1920,11 +1920,695 @@
   %7D%0A%0A  %7D
+%0A%0A  private static Constructor getConstructor(Class%3C? extends Sampler%3E samplerClass, Object... constructorParams) %7B%0A    Class%5B%5D parameterTypes = Arrays.stream(constructorParams)%0A        .map(param -%3E param.getClass()).collect(Collectors.toList()).toArray(new Class%5B0%5D);%0A    Constructor constructor = getMatchingAccessibleConstructor(samplerClass, parameterTypes);%0A    if (constructor == null) %7B%0A      StringBuilder msg = new StringBuilder()%0A          .append(%22No constructor found for %22)%0A          .append(samplerClass)%0A          .append(%22 with %22)%0A          .append(Arrays.toString(parameterTypes));%0A      throw new GaleniumException(msg.toString());%0A    %7D%0A    return constructor;%0A  %7D
 %0A%7D%0A

use Guava to find matching constructor