Example usage for java.lang.reflect Constructor getParameterTypes

List of usage examples for java.lang.reflect Constructor getParameterTypes

Introduction

In this page you can find the example usage for java.lang.reflect Constructor getParameterTypes.

Prototype

@Override
public Class<?>[] getParameterTypes() 

Source Link

Usage

From source file:de.openknowledge.cdi.common.property.PropertiesLoaderExtension.java

private static boolean isStringConstructorPresent(Type type) {
    for (Constructor<?> constructor : primitiveToWrapper(toClass(type)).getConstructors()) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        if (parameterTypes.length == 1 && String.class.equals(parameterTypes[0])) {
            return true;
        }//from www .j av a2 s . c  o m
    }
    return false;
}

From source file:info.novatec.testit.livingdoc.util.ClassUtils.java

private static boolean arityMatches(Constructor<?> constructor, Object... args) {
    return constructor.getParameterTypes().length == args.length;
}

From source file:com.linecorp.armeria.common.thrift.text.StructContext.java

private static boolean hasNoArgConstructor(Class clazz) {
    Constructor[] allConstructors = clazz.getConstructors();
    for (Constructor ctor : allConstructors) {
        Class<?>[] pType = ctor.getParameterTypes();
        if (pType.length == 0) {
            return true;
        }/*from  ww w . j  a v a 2 s . c  o  m*/
    }

    return false;
}

From source file:org.movealong.junitfu.ConstructorMockBinder.java

private static Collection<? extends MockBinder> createMockBinders(Constructor<?> constructor) {
    if (constructor.getAnnotation(Inject.class) != null) {
        HashSet<MockBinder> mockBinders = new HashSet<MockBinder>();
        for (int parameterIndex = 0; parameterIndex < constructor
                .getParameterTypes().length; parameterIndex++) {
            for (Annotation annotation : constructor.getParameterAnnotations()[parameterIndex]) {
                if (annotation.annotationType() == Mock.class) {
                    Class<?> mockClass = constructor.getParameterTypes()[parameterIndex];
                    String mockName = defaultIfEmpty(((Mock) annotation).value(),
                            uncapitalize(mockClass.getSimpleName()));
                    mockBinders.add(new SimpleMockBinder(mockClass, mockName));
                }//from w  w  w.  j  a  va 2  s.  c o  m
            }
        }
        return mockBinders;
    } else {
        return Collections.emptyList();
    }
}

From source file:org.jdto.impl.BeanClassUtils.java

/**
 * Check if the class has a default constructor.
 *
 * @param cls//from  www.j  a  va  2  s  . c o m
 * @return true if the type has default constructor, false if not.
 */
public static boolean hasDefaultConstructor(Class cls) {

    Constructor[] constructors = cls.getConstructors();

    //go through all the constructors trying to find one with no
    //parameters
    for (Constructor constructor : constructors) {
        if (constructor.getParameterTypes().length == 0) {
            return true;
        }
    }
    return false;
}

From source file:org.openvpms.web.component.im.edit.IMObjectCollectionEditorFactory.java

/**
 * Returns a constructor to construct a new editor.
 *
 * @param type       the editor type//from w  ww.j  a  v a  2s. c om
 * @param collection the collection property
 * @param object     the parent of the collection
 * @param context    the layout context. May be {@code null}
 * @return a constructor to construct the editor, or {@code null} if none can be found
 */
@SuppressWarnings("unchecked")
private static Constructor getConstructor(Class type, CollectionProperty collection, IMObject object,
        LayoutContext context) {
    Constructor[] ctors = type.getConstructors();

    for (Constructor ctor : ctors) {
        // check parameters
        Class[] ctorTypes = ctor.getParameterTypes();
        if (ctorTypes.length == 3) {
            Class ctorCollection = ctorTypes[0];
            Class ctorObj = ctorTypes[1];
            Class ctorLayout = ctorTypes[2];

            if (ctorCollection.isAssignableFrom(collection.getClass())
                    && ctorObj.isAssignableFrom(object.getClass())
                    && ((context != null && ctorLayout.isAssignableFrom(context.getClass()))
                            || (context == null && LayoutContext.class.isAssignableFrom(ctorLayout)))) {
                return ctor;
            }
        }
    }
    return null;
}

From source file:ru.runa.notifier.util.ClassLoaderUtil.java

public static Object instantiate(String className, Object... params) {
    try {/*www . j  a v  a2s  .c om*/
        Class<?> clazz = loadClass(className);
        Class<?>[] paramType;
        if (params != null) {
            paramType = new Class[params.length];
            for (int i = 0; i < params.length; ++i) {
                paramType[i] = params[i].getClass();
            }
        } else {
            paramType = new Class[0];
            params = new Object[0];
        }
        Constructor<?> constructor = null;
        Constructor<?>[] constructors = clazz.getConstructors();
        constrLoop: for (Constructor<?> constr : constructors) {
            Class<?>[] types = constr.getParameterTypes();
            if (types.length != paramType.length) {
                continue;
            }
            for (int i = 0; i < types.length; ++i) {
                if (!types[i].isAssignableFrom(params[i].getClass())) {
                    continue constrLoop;
                }
            }
            constructor = constr;
        }
        if (constructor == null) {
            constructor = clazz.getConstructor(paramType);
        }
        return constructor.newInstance(params);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.xulux.utils.ClassLoaderUtils.java

/**
 * Instantiates the parent object of an inner class.
 * @param clazz the class/*from   ww w .  ja  v  a2  s.c  o  m*/
 * @return the instance of the inner class
 */
protected static Object getParentObjectForInnerClass(Class clazz) {
    String name = clazz.getName();
    int index = name.indexOf("$");
    if (index != -1) {
        name = name.substring(0, index);
        // cannot think of a scenario when this is null,
        Class clz = getClass(name.substring(0, index));
        ArrayList parms = new ArrayList();
        boolean hasEmptyConstructor = false;
        Constructor[] css = clz.getConstructors();
        for (int i = 0; i < css.length; i++) {
            Constructor cs = css[i];
            if (cs.getParameterTypes().length == 0) {
                hasEmptyConstructor = true;
                parms = new ArrayList();
                break;
            } else {
                for (int j = 0; j < cs.getParameterTypes().length; j++) {
                    Class c = cs.getParameterTypes()[j];
                    Object object = null;
                    try {
                        object = c.newInstance();
                    } catch (Exception e) {
                        // eat it..
                    } finally {
                        parms.add(object);
                    }
                }
            }
        }
        return getObjectFromClass(getClass(name.substring(0, index)), parms);
    }
    return null;
}

From source file:se.trillian.goodies.spring.DomainObjectFactoryFactoryBean.java

private static Constructor<?> findMatchingConstructor(Class<?> clazz, Method m) {
    LinkedList<Constructor<?>> constructors = new LinkedList<Constructor<?>>();
    Constructor<?> directMatch = null;
    for (Constructor<?> c : clazz.getDeclaredConstructors()) {
        if (isParameterTypesPrefix(m.getParameterTypes(), c.getParameterTypes())) {
            constructors.add(c);/*from  w w  w .  j  a  v a 2s  .  c  om*/
            if (directMatch == null && isParameterTypesPrefix(c.getParameterTypes(), m.getParameterTypes())) {
                directMatch = c;
            }
        }
    }
    if (constructors.isEmpty()) {
        throw new IllegalArgumentException("No matching constructor found in " + "implementation class '"
                + clazz + "' for factory method '" + m + "'");
    }
    if (constructors.size() > 1) {
        if (directMatch != null) {
            return directMatch;
        }
        throw new IllegalArgumentException("More than 1 matching constructor "
                + "found in implementation class '" + clazz + "' for factory method '" + m + "'");
    }
    return constructors.getFirst();
}

From source file:com.pentaho.big.data.bundles.impl.shim.common.ShimBridgingClassloader.java

public static Object create(BundleContext bundleContext, String className, List<Object> arguments)
        throws KettlePluginException, ClassNotFoundException, IllegalAccessException, InstantiationException,
        InvocationTargetException {
    ShimBridgingClassloader shimBridgingClassloader = new ShimBridgingClassloader(pluginClassloaderGetter
            .getPluginClassloader(LifecyclePluginType.class.getCanonicalName(), HADOOP_SPOON_PLUGIN),
            bundleContext);//from   w  w  w . j  ava  2  s.c o m
    Class<?> clazz = Class.forName(className, true, shimBridgingClassloader);
    if (arguments == null || arguments.size() == 0) {
        return clazz.newInstance();
    }
    for (Constructor<?> constructor : clazz.getConstructors()) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        if (parameterTypes.length == arguments.size()) {
            boolean match = true;
            for (int i = 0; i < parameterTypes.length; i++) {
                Object o = arguments.get(i);
                if (o != null && !parameterTypes[i].isInstance(o)) {
                    match = false;
                    break;
                }
            }
            if (match) {
                return constructor.newInstance(arguments.toArray());
            }
        }
    }
    throw new InstantiationException(
            "Unable to find constructor for class " + className + " with arguments " + arguments);
}