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:Main.java

@SuppressWarnings("unchecked")
public static Object newInstance(Class _Class, int constructorIndex, Object... args) throws Exception {
    Constructor cons = _Class.getConstructors()[constructorIndex];
    return cons.newInstance(args);
}

From source file:ReflectUtils.java

/**
 * Adds all Constructor (from Class.getConstructorCalls) to the list
 * @param aClass//from w w w .  j a v  a 2 s.  c om
 * @param list
 * @return number of constructors added
 */
public static int addConstrcutors(Class aClass, List<Member> list) {
    Constructor[] constructors = aClass.getConstructors();
    for (Constructor c : constructors) {
        list.add(c);
    }
    return constructors.length;
}

From source file:ReflectUtils.java

/**
 * Gets an array of all Constructor calls for the given class
 * @param aClass//from w  ww . j  av  a2  s.  c  om
 * @return
 */
public static String[] getConstructorCalls(Class aClass) {
    Constructor[] constructors = aClass.getConstructors();
    String[] cons = new String[constructors.length];
    int i = 0;
    for (Constructor c : constructors) {
        cons[i++] = getJavaCallString(c);
    }
    return cons;
}

From source file:Main.java

private static final Constructor findCtor(Class<?> clazz, List<Class<?>> actualArgs) {
    for (final Constructor m : clazz.getConstructors()) {
        if (callableWith(m.getParameterTypes(), actualArgs)) {
            return m;
        }/*from  ww  w.  j  ava 2 s  .  com*/
    }
    return null;
}

From source file:edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessDataGetterN3Utils.java

private static ProcessDataGetterN3 instantiateClass(String processorClass, JSONObject jsonObject) {
    ProcessDataGetterN3 pn = null;// ww w.  j a  va 2 s  . co m
    try {
        Class<?> clz = Class.forName(processorClass);
        Constructor<?>[] ctList = clz.getConstructors();
        for (Constructor<?> ct : ctList) {
            Class<?>[] parameterTypes = ct.getParameterTypes();
            if (parameterTypes.length > 0 && parameterTypes[0].isAssignableFrom(jsonObject.getClass())) {
                pn = (ProcessDataGetterN3) ct.newInstance(jsonObject);
            } else {
                pn = (ProcessDataGetterN3) ct.newInstance();
            }
        }

    } catch (Exception ex) {
        log.error("Error occurred instantiating " + processorClass, ex);
    }
    return pn;

}

From source file:com.jythonui.server.BUtil.java

public static Object construct(Class cl) {
    Constructor[] c = cl.getConstructors();
    // assume there is only default constructor
    try {//from ww w .  j a v a  2 s .  c  om
        return c[0].newInstance();
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException e) {
        errorMess(L(), IErrorCode.ERRORCODE119, ILogMess.CANNOTINITIATEOBJECT, e, cl.getSimpleName());

    }
    return null;
}

From source file:Main.java

/**
 * Creates a new instance of xposed class.
 *
 * @param clzOurClass         the xposed class
 * @param objectOriginalClass   the instance of the original class
 * @return/*from   ww w .  j  a va  2 s. c o m*/
 */
private static Object createInstanceFromOurClass(Class clzOurClass, Object objectOriginalClass) {
    Object objectReturn = null;

    try {
        objectReturn = clzOurClass.getConstructors()[0].newInstance(objectOriginalClass);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return objectReturn;
}

From source file:fi.luontola.cqrshotel.JsonSerializationTest.java

private static Object newDummy(Class<?> type) throws Exception {
    Constructor<?> ctor = type.getConstructors()[0];
    Class<?>[] paramTypes = ctor.getParameterTypes();
    Object[] params = new Object[paramTypes.length];
    for (int i = 0; i < paramTypes.length; i++) {
        params[i] = randomValue(paramTypes[i]);
    }//from w w  w .j  a  v a 2 s  . co  m
    return ctor.newInstance(params);
}

From source file:Main.java

/**
 * Takes a class type and constructs it. If the class does not have an empty constructor this
 * will/*from w w w.  ja  v a  2 s  .  c om*/
 * 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:com.espertech.esper.util.ConstructorHelper.java

private static Constructor findMatchingConstructor(Class clazz, Class parameterTypes[]) {
    Constructor[] ctors = clazz.getConstructors();

    for (int i = 0; i < ctors.length; i++) {
        Class[] ctorParams = ctors[i].getParameterTypes();

        if (isAssignmentCompatible(parameterTypes, ctorParams)) {
            return ctors[i];
        }//w w  w  .  ja  v a2 s  .  c om
    }

    return null;
}