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

public static void main(String[] argv) throws Exception {
    Constructor[] constructors = String.class.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
        Constructor c = constructors[i];
        Class[] paramTypes = c.getParameterTypes();
        String name = c.getName();
        System.out.print(Modifier.toString(c.getModifiers()));
        System.out.print(" " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getCanonicalName());
        }//  ww w. j  a  va  2s.  com
        System.out.println(");");
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class c = Class.forName("MyClass");

    Constructor constructors[] = c.getDeclaredConstructors();
    Object obj = null;/* w  ww  . ja  v a  2  s.  c om*/
    for (Constructor cons : constructors) {
        Class[] params = cons.getParameterTypes();
        if (params.length == 1 && params[0] == int.class) {
            obj = cons.newInstance(10);
            break;
        }
    }

    if (obj == null) {
        System.out.println("Can't Create MyClass object.");
        return;
    }
}

From source file:ConstructorSift.java

public static void main(String... args) {
    try {/*from  w  w w  .ja v a  2  s .co m*/
        Class<?> cArg = Class.forName(args[1]);

        Class<?> c = Class.forName(args[0]);
        Constructor[] allConstructors = c.getDeclaredConstructors();
        for (Constructor ctor : allConstructors) {
            Class<?>[] pType = ctor.getParameterTypes();
            for (int i = 0; i < pType.length; i++) {
                if (pType[i].equals(cArg)) {
                    out.format("%s%n", ctor.toGenericString());

                    Type[] gpType = ctor.getGenericParameterTypes();
                    for (int j = 0; j < gpType.length; j++) {
                        char ch = (pType[j].equals(cArg) ? '*' : ' ');
                        out.format("%7c%s[%d]: %s%n", ch, "GenericParameterType", j, gpType[j]);
                    }
                    break;
                }
            }
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class clazz = String.class;

    Constructor[] constructors = clazz.getDeclaredConstructors();
    for (Constructor constructor : constructors) {
        String name = constructor.getName();
        System.out.println("Constructor name= " + name);

        Class[] paramterTypes = constructor.getParameterTypes();
        for (Class c : paramterTypes) {
            System.out.println("Param type name = " + c.getName());
        }/* www.j ava 2s. com*/
    }

    Constructor constructor = String.class.getConstructor(new Class[] { String.class });
    System.out.println("Constructor     = " + constructor.getName());
}

From source file:Main.java

public static void print_method_or_constructor(Member member) {
    Constructor c = (Constructor) member;
    Class[] parameters = c.getParameterTypes();
    Class[] exceptions = c.getExceptionTypes();
    System.out.println(c.getDeclaringClass());

    for (int i = 0; i < parameters.length; i++) {
        System.out.println(parameters[i]);
    }//from   w ww  .j av  a  2s  .co m
    for (int i = 0; i < exceptions.length; i++) {
        System.out.println(exceptions[i]);
    }
}

From source file:ReflectionTest.java

public static void printConstructors(Class cl) {
    Constructor[] constructors = cl.getDeclaredConstructors();

    for (int i = 0; i < constructors.length; i++) {
        Constructor c = constructors[i];
        Class[] paramTypes = c.getParameterTypes();
        String name = c.getName();
        System.out.print(Modifier.toString(c.getModifiers()));
        System.out.print(" " + name + "(");
        for (int j = 0; j < paramTypes.length; j++) {
            if (j > 0)
                System.out.print(", ");
            System.out.print(paramTypes[j].getName());
        }/*from   ww w  .  j  a v  a 2s.co m*/
        System.out.println(");");
    }
}

From source file:Main.java

public static String ctorToString(Constructor constructor) {
    Class[] params = constructor.getParameterTypes();
    StringBuffer sb = new StringBuffer(constructor.getName());
    sb.append("(");
    for (int i = 0; i < params.length; i++) {
        String s = params[i].getName();
        sb.append(s);//from www .  jav a  2 s  . c  o  m
        if (i < params.length - 1) {
            sb.append(", ");
        }
    }
    sb.append(")");
    return sb.toString();
}

From source file:Main.java

public static <T> T newInstance(Class<T> claxx)
        throws IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<?>[] cons = claxx.getDeclaredConstructors();
    for (Constructor<?> c : cons) {
        Class[] cls = c.getParameterTypes();
        if (cls.length == 0) {
            c.setAccessible(true);/*from  ww w . j  a  va2s.  co  m*/
            return (T) c.newInstance();
        } else {
            Object[] objs = new Object[cls.length];
            for (int i = 0; i < cls.length; i++) {
                objs[i] = getDefaultPrimiticeValue(cls[i]);
            }
            c.setAccessible(true);
            return (T) c.newInstance(objs);
        }
    }
    return null;
}

From source file:ShowClass.java

public static void printMethodOrConstructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
        Method m = (Method) member;
        returntype = m.getReturnType();/*from   w w w.  ja v  a2 s .  c  o  m*/
        parameters = m.getParameterTypes();
        exceptions = m.getExceptionTypes();
        System.out.print(
                "  " + modifiers(member.getModifiers()) + typeName(returntype) + " " + member.getName() + "(");
    } else {
        Constructor c = (Constructor) member;
        parameters = c.getParameterTypes();
        exceptions = c.getExceptionTypes();
        System.out.print("  " + modifiers(member.getModifiers()) + typeName(c.getDeclaringClass()) + "(");
    }

    for (int i = 0; i < parameters.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typeName(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
        System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typeName(exceptions[i]));
    }
    System.out.println(";");
}

From source file:Main.java

public static void print_method_or_constructor(Member member) {
    Class returntype = null, parameters[], exceptions[];
    if (member instanceof Method) {
        Method m = (Method) member;
        returntype = m.getReturnType();/*from   w w w  .j a va2 s  .  com*/
        parameters = m.getParameterTypes();
        exceptions = m.getExceptionTypes();
        System.out.print(
                "  " + modifiers(member.getModifiers()) + typename(returntype) + " " + member.getName() + "(");
    } else {
        Constructor c = (Constructor) member;
        parameters = c.getParameterTypes();
        exceptions = c.getExceptionTypes();
        System.out.print("  " + modifiers(member.getModifiers()) + typename(c.getDeclaringClass()) + "(");
    }
    for (int i = 0; i < parameters.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(parameters[i]));
    }
    System.out.print(")");
    if (exceptions.length > 0)
        System.out.print(" throws ");
    for (int i = 0; i < exceptions.length; i++) {
        if (i > 0)
            System.out.print(", ");
        System.out.print(typename(exceptions[i]));
    }
    System.out.println(";");
}