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

public static void main(String[] args) {
    try {//  ww w. j av  a2 s. c om
        Class c = Class.forName("java.util.ArrayList");

        Constructor constructors[] = c.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.print(constructors[i].getName() + ": ");
            Class parameters[];
            parameters = constructors[i].getParameterTypes();
            for (int j = 0; j < parameters.length; j++) {
                String s = parameters[j].getName();
                s = s.substring(s.lastIndexOf(".") + 1, s.length());
                System.out.print(s + " ");
            }
            System.out.println("");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];

    field.setAccessible(true);/*w  w w.  j a v  a  2 s .c o  m*/
    constructor.setAccessible(true);
    method.setAccessible(true);

}

From source file:ReflectionDemo1.java

public static void main(String args[]) {
    try {/*from  w  w w  .  j  ava  2s  .c  o  m*/
        Class c = Class.forName("java.awt.Dimension");
        System.out.println("Constructors:");
        Constructor constructors[] = c.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.println(" " + constructors[i]);
        }

        System.out.println("Fields:");
        Field fields[] = c.getFields();
        for (int i = 0; i < fields.length; i++) {
            System.out.println(" " + fields[i]);
        }

        System.out.println("Methods:");
        Method methods[] = c.getMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(" " + methods[i]);
        }
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }
}

From source file:ShowMethods.java

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println(usage);
        System.exit(0);//from   ww w .  j a  va  2  s . c o m
    }
    int lines = 0;
    try {
        Class c = Class.forName(args[0]);
        Method[] m = c.getMethods();
        Constructor[] ctor = c.getConstructors();
        if (args.length == 1) {
            for (int i = 0; i < m.length; i++)
                System.out.println(p.matcher(m[i].toString()).replaceAll(""));
            for (int i = 0; i < ctor.length; i++)
                System.out.println(p.matcher(ctor[i].toString()).replaceAll(""));
            lines = m.length + ctor.length;
        } else {
            for (int i = 0; i < m.length; i++)
                if (m[i].toString().indexOf(args[1]) != -1) {
                    System.out.println(p.matcher(m[i].toString()).replaceAll(""));
                    lines++;
                }
            for (int i = 0; i < ctor.length; i++)
                if (ctor[i].toString().indexOf(args[1]) != -1) {
                    System.out.println(p.matcher(ctor[i].toString()).replaceAll(""));
                    lines++;
                }
        }
    } catch (ClassNotFoundException e) {
        System.out.println("No such class: " + e);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;//w w w  .  ja  v  a  2 s  .  c o  m

    name = cls.getName();
    System.out.println(name);
    name = cls.getName() + "." + field.getName();
    System.out.println(name);
    name = constructor.getName();
    System.out.println(name);
    name = cls.getName() + "." + method.getName();
    System.out.println(name);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;/*w w  w  . j  a  v  a  2s . co  m*/

    name = cls.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = field.getName();
    System.out.println(name);
    name = constructor.getName().substring(cls.getPackage().getName().length() + 1);
    System.out.println(name);
    name = method.getName();
    System.out.println(name);

}

From source file:MyClass.java

public static void main(String[] args) {
    Class<MyClass> c = MyClass.class;

    System.out.println("Constructors for " + c.getName());
    Constructor[] constructors = c.getConstructors();
    ArrayList<String> constructDescList = getConstructorsDesciption(constructors);
    for (String desc : constructDescList) {
        System.out.println(desc);
    }/*from  w  ww  .j  a v  a2s . c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    Class cls = Class.forName("java.lang.String");
    System.out.println("Panel Constructors =");

    /* returns the array of Constructor objects representing the public 
    constructors of this class */
    Constructor c[] = cls.getConstructors();
    for (int i = 0; i < c.length; i++) {
        System.out.println(c[i]);
    }//from   www. jav a 2  s  . c om

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("c:/mysql-connector-java-5.1.18-bin.jar");
    URLClassLoader urlCl = new URLClassLoader(new URL[] { f.toURL() }, System.class.getClassLoader());

    Class mySqlDriver = urlCl.loadClass("com.mysql.jdbc.Driver");
    System.out.println(mySqlDriver.newInstance());
    System.out.println("Is this interface? = " + mySqlDriver.isInterface());

    Class interfaces[] = mySqlDriver.getInterfaces();
    int i = 1;/*from   w  w w. j  a va  2s.com*/
    for (Class _interface : interfaces) {
        System.out.println("Implemented Interface Name " + (i++) + " = " + _interface.getName());
    }

    Constructor constructors[] = mySqlDriver.getConstructors();
    for (Constructor constructor : constructors) {
        System.out.println("Constructor Name = " + constructor.getName());
        System.out.println("Is Constructor Accessible? = " + constructor.isAccessible());
    }
}

From source file:SampleConstructor.java

static void showConstructors(Object o) {
    Class c = o.getClass();
    Constructor[] theConstructors = c.getConstructors();
    for (int i = 0; i < theConstructors.length; i++) {
        System.out.print("( ");
        Class[] parameterTypes = theConstructors[i].getParameterTypes();
        for (int k = 0; k < parameterTypes.length; k++) {
            String parameterString = parameterTypes[k].getName();
            System.out.print(parameterString + " ");
        }/*from   w  ww  .  ja  va2 s. c om*/
        System.out.println(")");
    }
}