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:org.codehaus.groovy.grails.plugins.springsecurity.acl.ProxyUtils.java

/**
 * Finds the constructor in the unproxied superclass if proxied.
 * @param constructor  the constructor// w  ww  .  j  a  v a 2s  .c om
 * @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:grails.plugin.springsecurity.acl.util.ProxyUtils.java

/**
 * Finds the constructor in the unproxied superclass if proxied.
 * @param constructor  the constructor/*from   w w w .ja  va 2s.  com*/
 * @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:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.DataGetterUtils.java

private static boolean isConstructorSuitableForArguments(Constructor<?> ct, Object[] args) {
    Class<?>[] parameterTypes = ct.getParameterTypes();
    if (args.length != parameterTypes.length) {
        return false;
    }// w  w  w.ja  v a  2s  .c om
    for (int i = 0; i < args.length; i++) {
        Class<? extends Object> argClass = args[i].getClass();
        if (!parameterTypes[i].isAssignableFrom(argClass)) {
            return false;
        }
    }
    return true;
}

From source file:io.github.benas.randombeans.util.ReflectionUtils.java

private static boolean hasSameArgumentTypes(final Constructor<?> constructor,
        final RandomizerArgument[] randomizerArguments) {
    Class<?>[] constructorParameterTypes = constructor.getParameterTypes();
    for (int i = 0; i < randomizerArguments.length; i++) {
        if (!constructorParameterTypes[i].isAssignableFrom(randomizerArguments[i].type())) {
            // Argument types does not match
            return false;
        }/*from   w  ww  . j a  v a2  s .  c om*/
    }
    return true;
}

From source file:io.konik.utils.RandomInvoiceGenerator.java

private static Constructor<?> findBiggestConstructor(Class<?> root) {
    Constructor<?>[] constructors = root.getConstructors();
    Constructor<?> biggestConstructor = null;
    for (Constructor<?> constructor : constructors) {
        if (biggestConstructor == null) {
            biggestConstructor = constructor;
            continue;
        }/*from   ww  w. j ava 2 s  .c o  m*/
        if (constructor.getTypeParameters().length > biggestConstructor.getParameterTypes().length) {
            biggestConstructor = constructor;
        }
    }
    return biggestConstructor;
}

From source file:org.opendaylight.controller.cluster.datastore.DatastoreContextIntrospector.java

/**
 * Finds the appropriate constructor for the specified type that we will use to construct
 * instances./*from w  w  w. j a  va2s .  co  m*/
 */
private static void processPropertyType(Class<?> propertyType) throws Exception {
    Class<?> wrappedType = Primitives.wrap(propertyType);
    if (constructors.containsKey(wrappedType)) {
        return;
    }

    // If the type is a primitive (or String type), we look for the constructor that takes a
    // single String argument, which, for primitives, validates and converts from a String
    // representation which is the form we get on ingress.
    if (propertyType.isPrimitive() || Primitives.isWrapperType(propertyType)
            || propertyType.equals(String.class)) {
        constructors.put(wrappedType, propertyType.getConstructor(String.class));
    } else {
        // This must be a yang-defined type. We need to find the constructor that takes a
        // primitive as the only argument. This will be used to construct instances to perform
        // validation (eg range checking). The yang-generated types have a couple single-argument
        // constructors but the one we want has the bean ConstructorProperties annotation.
        for (Constructor<?> ctor : propertyType.getConstructors()) {
            ConstructorProperties ctorPropsAnnotation = ctor.getAnnotation(ConstructorProperties.class);
            if (ctor.getParameterTypes().length == 1 && ctorPropsAnnotation != null) {
                findYangTypeGetter(propertyType, ctorPropsAnnotation.value()[0]);
                constructors.put(propertyType, ctor);
                break;
            }
        }
    }
}

From source file:co.jirm.mapper.definition.SqlParameterDefinition.java

private static Map<String, SqlParameterDefinition> getSqlBeanParametersFromJsonCreatorConstructor(
        Constructor<?> c, SqlObjectConfig config) {
    Map<String, SqlParameterDefinition> parameters = new LinkedHashMap<String, SqlParameterDefinition>();
    Annotation[][] aas = c.getParameterAnnotations();
    Class<?>[] pts = c.getParameterTypes();
    if (aas == null || aas.length == 0) {
        return parameters;
    }//from  w  w w .  j  a  v a 2s. co  m

    for (int i = 0; i < aas.length; i++) {
        Annotation[] as = aas[i];
        Class<?> parameterType = pts[i];
        for (int j = 0; j < as.length; j++) {
            Annotation a = as[j];
            if (JsonProperty.class.equals(a.annotationType())) {
                JsonProperty p = (JsonProperty) a;
                String value = p.value();
                final SqlParameterDefinition definition = parameterDef(config, c.getDeclaringClass(), value,
                        parameterType, i);
                parameters.put(value, definition);
            }
        }

    }

    return parameters;
}

From source file:at.ac.tuwien.infosys.jcloudscale.api.CloudObjects.java

/**
 * Creates a remote CloudObject using the given proxy and parameters for the remote constructor invocation.
 * <p/>/*from  w  w  w . j  a  v  a2s  . com*/
 * <b>Note that this method is intended to be used by JCloudScale internally.</b><br/>
 * It is recommended to use {@link #create(Class, Object...)} instead.
 *
 * @param constructor the constructor to use
 * @param type the type of the object to create
 * @param args the constructor arguments
 * @return the proxy
 * @throws NoSuchMethodException if there are zero or more than one constructor candidates
 * @see #create(Class, Object...)
 */
public static <T> T create(Constructor<?> constructor, Class<T> type, Object... args) {

    // we just have to ensure that JCloudScaleClient is started before doing anything with JCloudScale.
    JCloudScaleClient.getClient();

    // create a proxy of this object with cglib
    T newProxy = null;
    try {
        newProxy = (T) CgLibUtil.replaceCOWithProxy(type, args, constructor.getParameterTypes());
    } catch (Throwable e) {
        throw new JCloudScaleException(e);
    }

    ReflectionUtil.checkLegalCloudIdDef(newProxy);
    ReflectionUtil.checkLegalCloudInvocationInfoDef(newProxy);

    // find which parameters should be passed by ref, and which should be passed by value
    Object[] processedParams = JCloudScaleReferenceManager.getInstance().processArguments(constructor, args);

    UUID id = CloudManager.getInstance().createNewInstance(type, processedParams,
            constructor.getParameterTypes(), newProxy);

    // this doesn't actually work at the moment (and it's also not strictly required)
    //      ReflectionUtil.injectCloudId(newProxy, id);

    idMaps.put(newProxy, id);
    return newProxy;
}

From source file:com.mani.cucumber.ReflectionUtils.java

private static <T> Constructor<T> findConstructor(Class<T> clazz, Object[] params) {

    for (Constructor<?> constructor : clazz.getConstructors()) {
        Class<?>[] paramTypes = constructor.getParameterTypes();
        if (matches(paramTypes, params)) {
            @SuppressWarnings("unchecked")
            Constructor<T> rval = (Constructor<T>) constructor;
            return rval;
        }//from   w  ww  .j a  v  a2s  .c om
    }

    throw new IllegalStateException("No appropriate constructor found for " + clazz.getCanonicalName());
}

From source file:co.jirm.mapper.definition.SqlParameterDefinition.java

private static Map<String, SqlParameterDefinition> getSqlBeanParametersFromConstructorProperties(
        Constructor<?> c, SqlObjectConfig config) {
    Map<String, SqlParameterDefinition> parameters = new LinkedHashMap<String, SqlParameterDefinition>();

    String[] constructorProperties = c.getAnnotation(ConstructorProperties.class).value();
    Class<?>[] pts = c.getParameterTypes();

    for (int i = 0; i < pts.length; i++) {
        parameters.put(constructorProperties[i],
                parameterDef(config, c.getDeclaringClass(), constructorProperties[i], pts[i], i));
    }//from   ww  w. j  a v  a 2s.c  o  m

    return parameters;
}