Example usage for java.lang Class getConstructors

List of usage examples for java.lang Class getConstructors

Introduction

In this page you can find the example usage for java.lang Class getConstructors.

Prototype

@CallerSensitive
public Constructor<?>[] getConstructors() throws SecurityException 

Source Link

Document

Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.

Usage

From source file:ch.digitalfondue.npjt.ConstructorAnnotationRowMapper.java

/**
 * Check if the given class has the correct form.
 * //from   w  w w  .j av a 2  s . c o  m
 * <ul>
 * <li>must have exactly one public constructor.</li>
 * <li>must at least have one parameter.</li>
 * <li>all the parameters must be annotated with @Column annotation.</li>
 * </ul>
 * 
 * @param clazz
 * @return
 */
public static boolean hasConstructorInTheCorrectForm(Class<?> clazz) {

    if (clazz.getConstructors().length != 1) {
        return false;
    }

    Constructor<?> con = clazz.getConstructors()[0];

    if (con.getParameterTypes().length == 0) {
        return false;
    }

    Annotation[][] parameterAnnotations = con.getParameterAnnotations();
    for (Annotation[] as : parameterAnnotations) {
        if (!hasColumnAnnotation(as)) {
            return false;
        }
    }

    return true;
}

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

/**
 * Returns a constructor to construct a new dialog.
 *
 * @param type    the editor dialog type
 * @param editor  the editor/*from   ww  w. ja v a2  s .co m*/
 * @param context the context
 * @return a constructor to construct the dialog, or {@code null} if none can be found
 */
private static Constructor getConstructor(Class type, IMObjectEditor editor, Context context) {
    Constructor[] ctors = type.getConstructors();
    for (Constructor ctor : ctors) {
        // check parameters
        Class<?>[] ctorTypes = ctor.getParameterTypes();
        if (ctorTypes.length == 2) {
            Class<?> ctorEditor = ctorTypes[0];
            Class<?> ctorContext = ctorTypes[1];
            if (ctorEditor.isAssignableFrom(editor.getClass())
                    && ctorContext.isAssignableFrom(context.getClass())) {
                return ctor;
            }
        }
    }
    return null;
}

From source file:org.codehaus.groovy.grails.plugins.springsecurity.acl.ProxyUtils.java

/**
 * Finds the constructor in the unproxied superclass if proxied.
 * @param constructor  the constructor/*  ww w . j  a  v a 2 s  .  c o  m*/
 * @return  the constructor in the unproxied class
 */
public static Constructor<?> unproxy(final Constructor<?> constructor) {
    Class<?> clazz = constructor.getDeclaringClass();

    if (!AopUtils.isCglibProxyClass(clazz)) {
        return constructor;
    }

    Class<?> searchType = unproxy(clazz);
    while (searchType != null) {
        for (Constructor<?> c : searchType.getConstructors()) {
            if (constructor.getName().equals(c.getName()) && (constructor.getParameterTypes() == null
                    || Arrays.equals(constructor.getParameterTypes(), c.getParameterTypes()))) {
                return c;
            }
        }
        searchType = searchType.getSuperclass();
    }

    return null;
}

From source file:org.mskcc.cbio.importer.util.ClassLoader.java

/**
 * Creates a new instance of given class with given arguments.
 *
 * @param className String// www .ja v a2  s. c  o m
 * @param args Object[]
 * @return Object
 */
public static Object getInstance(String className, Object[] args) throws Exception {

    // sanity check
    if (className == null || className.length() == 0) {
        throw new IllegalArgumentException("className must not be null");
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("getInstance(), className: " + className);
    }

    try {
        Class<?> clazz = Class.forName(className);
        Constructor[] constructors = clazz.getConstructors();
        // our classes only have the one constructor
        return constructors[0].newInstance(args);
    } catch (Exception e) {
        LOG.error(("Failed to instantiate " + className), e);
        throw e;
    }
}

From source file:org.ovirt.engine.sdk.mapping.Mapper.java

/**
 * Fetches class contractor for mapping context
 * /*from w  w  w.ja v a  2  s  . c  o m*/
 * @param to
 *            class to look at
 * 
 * @return .ctr
 * 
 * @throws NoSuchMethodException
 */
private static <T> Constructor<?> getConstracor(Class<T> to) throws NoSuchMethodException {
    for (Constructor<?> ctr : to.getConstructors()) {
        if (ctr.getParameterTypes().length > 0 && ctr.getParameterTypes()[0].equals(HttpProxyBroker.class)) {
            return ctr;
        }
    }
    throw new NoSuchMethodException("HttpProxyBroker");
}

From source file:grails.plugin.springsecurity.acl.util.ProxyUtils.java

/**
 * Finds the constructor in the unproxied superclass if proxied.
 * @param constructor  the constructor/*from   w w  w .jav  a  2  s .  c o  m*/
 * @return  the constructor in the unproxied class
 */
public static Constructor<?> unproxy(final Constructor<?> constructor) {
    Class<?> clazz = constructor.getDeclaringClass();

    if (!isProxy(clazz)) {
        return constructor;
    }

    Class<?> searchType = unproxy(clazz);
    while (searchType != null) {
        for (Constructor<?> c : searchType.getConstructors()) {
            if (constructor.getName().equals(c.getName()) && (constructor.getParameterTypes() == null
                    || Arrays.equals(constructor.getParameterTypes(), c.getParameterTypes()))) {
                return c;
            }
        }
        searchType = searchType.getSuperclass();
    }

    return null;
}

From source file:org.spongepowered.common.util.ReflectionUtil.java

@SuppressWarnings("unchecked")
public static <T> Constructor<T> findConstructor(final Class<T> objectClass, Object... args) {
    final Constructor<?>[] ctors = objectClass.getConstructors();
    if (args == null) {
        args = new Object[] { null };
    }/*w  w w  . ja v  a  2s  . com*/
    // labeled loops
    dance: for (final Constructor<?> ctor : ctors) {
        final Class<?>[] paramTypes = ctor.getParameterTypes();
        if (paramTypes.length != args.length) {
            for (Object object : args) {
                if (object != null) { // hahahah
                    if (object.getClass().isArray()) {
                        final Object[] objects = deconstructArray(args).toArray();
                        return findConstructor(objectClass, objects);
                    }
                }
            }
            continue; // we haven't found the right constructor
        }
        for (int i = 0; i < paramTypes.length; i++) {
            final Class<?> parameter = paramTypes[i];
            if (!isAssignable(args[i] == null ? null : args[i].getClass(), parameter, true)) {
                continue dance; // continue the outer loop since we didn't find the right one
            }
        }
        // We've found the right constructor, now to actually construct it!
        return (Constructor<T>) ctor;
    }
    throw new IllegalArgumentException("Applicable constructor not found for class: "
            + objectClass.getCanonicalName() + " with args: " + Arrays.toString(args));
}

From source file:gov.nih.nci.firebird.selenium2.pages.util.FirebirdTableUtils.java

@SuppressWarnings("unchecked")
// will be correct constructor or error will be thrown above
private static <T> Constructor<T> getConstructor(final AbstractLoadableComponent<?> parent,
        Class<T> tableListingClass) throws NoSuchMethodException {
    return (Constructor<T>) tableListingClass.getConstructors()[0];
}

From source file:lite.flow.runtime.kiss.ComponentUtil.java

private static Constructor<?> pickConstructor(Class<?> componentClazz, Map<String, Object> resources,
        Map<String, Object> parameters) {

    Constructor<?>[] constructors = componentClazz.getConstructors();
    if (constructors == null || constructors.length < 1)
        throw new IllegalArgumentException(
                format("Class '%s' don't have public constructors", componentClazz.getName()));

    return constructors[0];
}

From source file:io.cloudslang.lang.entities.bindings.values.PyObjectValueProxyFactory.java

private static PyObjectValueProxyClass createProxyClass(Class proxyClass, PyObject pyObject) throws Exception {
    Constructor<?> constructor = proxyClass.getConstructors()[0];
    for (Constructor<?> con : proxyClass.getConstructors()) {
        if (con.getParameterTypes().length < constructor.getParameterTypes().length) {
            constructor = con;// www  .j  av a  2s.c  o  m
        }
    }
    Object[] params = new Object[constructor.getParameterTypes().length];
    for (int index = 0; index < constructor.getParameterTypes().length; index++) {
        Class<?> parameterType = constructor.getParameterTypes()[index];
        params[index] = getParamDefaultValue(pyObject, parameterType);
    }
    return new PyObjectValueProxyClass(proxyClass, constructor, params);
}