All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cucumber.runtime.Utils Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package cucumber.runtime;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class Utils {
    public static  List listOf(int size, T obj) {
        List list = new ArrayList();
        for (int i = 0; i < size; i++) {
            list.add(obj);
        }
        return list;
    }

    public static boolean isInstantiable(Class clazz) {
        boolean isNonStaticInnerClass = !Modifier.isStatic(clazz.getModifiers()) && clazz.getEnclosingClass() != null;
        return Modifier.isPublic(clazz.getModifiers()) && !Modifier.isAbstract(clazz.getModifiers()) && !isNonStaticInnerClass;
    }

    public static boolean hasConstructor(Class clazz, Class[] paramTypes) {
        try {
            clazz.getConstructor(paramTypes);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }

    public static Object invoke(Object target, Method method, Object... args) throws Throwable {
        try {
            return method.invoke(target, args);
        } catch (IllegalArgumentException e) {
            throw new CucumberException("Failed to invoke " + MethodFormat.FULL.format(method), e);
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        } catch (IllegalAccessException e) {
            throw new CucumberException("Failed to invoke " + MethodFormat.FULL.format(method), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy