print Constructor - Java Reflection

Java examples for Reflection:Constructor

Description

print Constructor

Demo Code


//package com.java2s;

import java.lang.reflect.*;
import static java.lang.System.out;

public class Main {
    public static void printConstructor(Class<?> c) {
        out.format("Constructors => %n");
        Constructor[] cons = c.getDeclaredConstructors();

        if (cons.length == 0) {
            out.format("  --%s", "No declared constructor found");
        } else {/* w w  w  .  ja  v  a2s.  com*/
            for (Constructor con : cons) {
                Parameter[] params = con.getParameters();

                StringBuffer sb = new StringBuffer();
                if (params.length != 0) {
                    int i = 0;
                    for (; i < params.length - 1; i++) {
                        sb.append(params[i].getType().getCanonicalName()
                                + " " + params[i].getName());
                        sb.append(", ");
                    }
                    sb.append(params[i].getType().getCanonicalName() + " "
                            + params[i].getName());
                }
                out.format("  %s %s(%s)%n",
                        Modifier.toString(con.getModifiers()),
                        con.getName(), sb.toString());
            }
        }
        out.format("%n%n");
    }
}

Related Tutorials