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: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;//from  w  ww .j a  va 2s . c  om
        }
    }
    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);
}

From source file:ReflectUtils.java

/**
 * Convert the constructor to a Java Code String
 * (arguments are replaced by the simple types)
 * @param c Constructor/*from w  w  w  .  j a v  a2 s  .c  o  m*/
 * @return
 */
public static String getJavaCallString(Constructor c) {
    StringBuilder call = new StringBuilder();
    call.append(c.getDeclaringClass().getSimpleName());
    addParamsString(call, c.getParameterTypes());
    return call.toString();
}

From source file:Main.java

public static String getDesc(Constructor<?> paramConstructor) {
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("(");
    Class[] arrayOfClass = paramConstructor.getParameterTypes();
    for (int i = 0; i < arrayOfClass.length; i++) {
        localStringBuffer.append(getDesc(arrayOfClass[i]));
    }// ww w . ja v  a  2s .  c om
    localStringBuffer.append(")V");
    return localStringBuffer.toString();
}

From source file:edu.uga.cs.fluxbuster.db.DBInterfaceFactory.java

/**
 * Creates a database interface./* w ww .  ja  v a 2s . c o m*/
 *
 * @return the database interface
 */
@SuppressWarnings("rawtypes")
public static DBInterface loadDBInterface() {
    DBInterface retval = null;
    try {
        DBInterfaceFactory.init();

        Constructor[] constructors = dbifaceClass.getConstructors();
        Constructor con = null;
        for (Constructor constructor : constructors) {
            Class[] paramtypes = constructor.getParameterTypes();
            if (paramtypes.length == 1 && paramtypes[0].isInstance(DBInterfaceFactory.connectionPool)) {
                con = constructor;
            }
        }
        Object obj = con.newInstance(DBInterfaceFactory.connectionPool);
        retval = (DBInterface) obj;
    } catch (Exception e) {
        if (log.isErrorEnabled()) {
            log.error("Error loading db interface.", e);
        }
    }
    return retval;
}

From source file:me.hurel.hqlbuilder.internal.ProxyUtil.java

public static Constructor<?> findBestConstructor(Constructor<?>[] constructors) {
    // First try : find a constructor with only primitive arguments
    for (Constructor<?> c : constructors) {
        Class<?>[] parameterTypes = c.getParameterTypes();
        if (parameterTypes.length == 0) {
            return c;
        } else {//w w  w  . java2 s  .  c o  m
            boolean useConstructor = true;
            for (Class<?> parameterType : parameterTypes) {
                if (!parameterType.isPrimitive()) {
                    useConstructor = false;
                    break;
                }
            }
            if (useConstructor) {
                return c;
            }
        }
    }
    // Second try : Okay, let's give a chance to String parameters too
    for (Constructor<?> c : constructors) {
        Class<?>[] parameterTypes = c.getParameterTypes();
        if (parameterTypes.length == 0) {
            return c;
        } else {
            boolean useConstructor = true;
            for (Class<?> parameterType : parameterTypes) {
                if (!parameterType.isPrimitive() && !parameterType.equals(String.class)) {
                    useConstructor = false;
                    break;
                }
            }
            if (useConstructor) {
                return c;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Takes a class type and constructs it. If the class does not have an empty constructor this
 * will//  w  ww . j  a  v a 2s  . c o  m
 * find the parametrized constructor and use nulls and default values to construct the class.
 *
 * @param clazz The class to construct.
 * @param <T> The type of the class to construct.
 * @return The constructed object.
 */
public static <T> T createInstance(Class<T> clazz) {
    T created = null;
    Constructor<?>[] constructors = clazz.getConstructors();
    for (Constructor<?> constructor : constructors) {
        if (!Modifier.isPrivate(constructor.getModifiers())) {
            Class<?>[] parameterTypes = constructor.getParameterTypes();
            List<Object> params = new ArrayList<Object>();
            for (Class<?> parameterType : parameterTypes)
                if (!parameterType.isPrimitive()) {
                    params.add(null);
                } else {
                    if (parameterType == boolean.class) {
                        params.add(false);
                    } else {
                        params.add(0);
                    }
                }

            try {
                @SuppressWarnings("unchecked")
                T newObject = (T) constructor.newInstance(params.toArray());
                created = newObject;
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            break;
        }
    }
    return created;
}

From source file:org.lanternpowered.server.util.ReflectionHelper.java

@SuppressWarnings("unchecked")
public static <T> Constructor<T> findConstructor(final Class<T> objectClass, @Nullable Object... args) {
    final Constructor<?>[] ctors = objectClass.getConstructors();
    if (args == null) {
        args = new Object[] { null };
    }/*from   w w  w . j a  v  a 2s .c  o  m*/
    // 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:MethodHashing.java

public static long constructorHash(Constructor method) throws Exception {
    Class[] parameterTypes = method.getParameterTypes();
    String methodDesc = method.getName() + "(";
    for (int j = 0; j < parameterTypes.length; j++) {
        methodDesc += getTypeString(parameterTypes[j]);
    }/*from  w  w w.ja v a  2  s . c o  m*/
    methodDesc += ")";

    long hash = 0;
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(512);
    MessageDigest messagedigest = MessageDigest.getInstance("SHA");
    DataOutputStream dataoutputstream = new DataOutputStream(
            new DigestOutputStream(bytearrayoutputstream, messagedigest));
    dataoutputstream.writeUTF(methodDesc);
    dataoutputstream.flush();
    byte abyte0[] = messagedigest.digest();
    for (int j = 0; j < Math.min(8, abyte0.length); j++)
        hash += (long) (abyte0[j] & 0xff) << j * 8;
    return hash;
}

From source file:org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovyCallSiteSelector.java

public static @CheckForNull Constructor<?> constructor(@Nonnull Class<?> receiver, @Nonnull Object[] args) {
    for (Constructor<?> c : receiver.getDeclaredConstructors()) {
        if (matches(c.getParameterTypes(), args)) {
            return c;
        }//  ww  w  .j a  v a2 s .  co m
    }
    return null;
}

From source file:org.apache.hadoop.hbase.util.ReflectionUtils.java

@SuppressWarnings("unchecked")
public static <T> Constructor<T> findConstructor(Class<T> type, Object... paramTypes) {
    Constructor<T>[] constructors = (Constructor<T>[]) type.getConstructors();
    for (Constructor<T> ctor : constructors) {
        Class<?>[] ctorParamTypes = ctor.getParameterTypes();
        if (ctorParamTypes.length != paramTypes.length) {
            continue;
        }//  www.  j  a  va 2  s .c  o m

        boolean match = true;
        for (int i = 0; i < ctorParamTypes.length && match; ++i) {
            Class<?> paramType = paramTypes[i].getClass();
            match = (!ctorParamTypes[i].isPrimitive()) ? ctorParamTypes[i].isAssignableFrom(paramType)
                    : ((int.class.equals(ctorParamTypes[i]) && Integer.class.equals(paramType))
                            || (long.class.equals(ctorParamTypes[i]) && Long.class.equals(paramType))
                            || (char.class.equals(ctorParamTypes[i]) && Character.class.equals(paramType))
                            || (short.class.equals(ctorParamTypes[i]) && Short.class.equals(paramType))
                            || (boolean.class.equals(ctorParamTypes[i]) && Boolean.class.equals(paramType))
                            || (byte.class.equals(ctorParamTypes[i]) && Byte.class.equals(paramType)));
        }

        if (match) {
            return ctor;
        }
    }
    throw new UnsupportedOperationException("Unable to find suitable constructor for class " + type.getName());
}