A program that displays a class synopsis for the named class : Constructor « Reflection « Java Tutorial






/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */
//package je3.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/** A program that displays a class synopsis for the named class */
public class ShowClass {
  /** The main method. Print info about the named class */
  public static void main(String[] args) throws ClassNotFoundException {
    Class c = Class.forName(args[0]);
    print_class(c);
  }

  /**
   * Display the modifiers, name, superclass and interfaces of a class or
   * interface. Then go and list all constructors, fields, and methods.
   */
  public static void print_class(Class c) {
    // Print modifiers, type (class or interface), name and superclass.
    if (c.isInterface()) {
      // The modifiers will include the "interface" keyword here...
      System.out.print(Modifier.toString(c.getModifiers()) + " " + typename(c));
    } else if (c.getSuperclass() != null) {
      System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c) + " extends "
          + typename(c.getSuperclass()));
    } else {
      System.out.print(Modifier.toString(c.getModifiers()) + " class " + typename(c));
    }

    // Print interfaces or super-interfaces of the class or interface.
    Class[] interfaces = c.getInterfaces();
    if ((interfaces != null) && (interfaces.length > 0)) {
      if (c.isInterface())
        System.out.print(" extends ");
      else
        System.out.print(" implements ");
      for (int i = 0; i < interfaces.length; i++) {
        if (i > 0)
          System.out.print(", ");
        System.out.print(typename(interfaces[i]));
      }
    }

    System.out.println(" {"); // Begin class member listing.

    // Now look up and display the members of the class.
    System.out.println("  // Constructors");
    Constructor[] constructors = c.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++)
      // Display constructors.
      print_method_or_constructor(constructors[i]);

    System.out.println("  // Fields");
    Field[] fields = c.getDeclaredFields(); // Look up fields.
    for (int i = 0; i < fields.length; i++)
      // Display them.
      print_field(fields[i]);

    System.out.println("  // Methods");
    Method[] methods = c.getDeclaredMethods(); // Look up methods.
    for (int i = 0; i < methods.length; i++)
      // Display them.
      print_method_or_constructor(methods[i]);

    System.out.println("}"); // End class member listing.
  }

  /** Return the name of an interface or primitive type, handling arrays. */
  public static String typename(Class t) {
    String brackets = "";
    while (t.isArray()) {
      brackets += "[]";
      t = t.getComponentType();
    }
    String name = t.getName();
    int pos = name.lastIndexOf('.');
    if (pos != -1)
      name = name.substring(pos + 1);
    return name + brackets;
  }

  /** Return a string version of modifiers, handling spaces nicely. */
  public static String modifiers(int m) {
    if (m == 0)
      return "";
    else
      return Modifier.toString(m) + " ";
  }

  /** Print the modifiers, type, and name of a field */
  public static void print_field(Field f) {
    System.out.println("  " + modifiers(f.getModifiers()) + typename(f.getType()) + " "
        + f.getName() + ";");
  }

  /**
   * Print the modifiers, return type, name, parameter types and exception type
   * of a method or constructor. Note the use of the Member interface to allow
   * this method to work with both Method and Constructor objects
   */
  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();
      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(";");
  }
}








7.3.Constructor
7.3.1.Get all constructors or by parameters
7.3.2.Demonstrates use of Constructor objects
7.3.3.A program that displays a class synopsis for the named class
7.3.4.Create new instance from Constructor
7.3.5.Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED
7.3.6.Load class with Class.forName
7.3.7.Call Private constructor
7.3.8.Get constructor by parameter type
7.3.9.Invoke a constructor which throws Exception
7.3.10.Passing a parameter to the constructor and calling a method dynamically
7.3.11.Getting a Constructor of a Class Object: By obtaining a list of all Constructors object
7.3.12.Getting a Constructor of a Class Object: By obtaining a particular Constructor object.
7.3.13.Get a compatible constructor for the given value type
7.3.14.Has Declared Constructor