Java Reflection (ii) Ability to use reflection analysis classes

Source: Internet
Author: User
Tags modifiers

The source code for the class capable of using reflection analysis is as follows (from "Java Core Technology" Volume One):

Package Testreflection;import Java.util.*;import java.lang.reflect.*;/** * This class uses reflection to print out all the characteristics of a class * * @version 1.1 2004-02-2 1 * @author Cay Horstmann */public class Reflectiontest {public static void main (string[] args) {//Read a class name from a command-line parameter or user input Stri ng name;if (args.length > 0) name = args[0];else {Scanner in = new Scanner (system.in); System.out.println ("Enter class name (e.g. java.util.Date):"); name = In.next ();} try {//print class name and superclass name (if superclass is not object) class CL = Class.forName (name); Class SUPERCL = Cl.getsuperclass ();//Gets the class or interface modifier, but this modifier is encoded by the integer int mi = cl.getmodifiers (); SYSTEM.OUT.PRINTLN (MI);//Gets an integer representing the actual modifier string modifiers = modifier.tostring (mi); System.out.println (modifiers); if (Modifiers.length () > 0) {System.out.print (modifiers + "");} System.out.print ("class" + name); if (SUPERCL! = NULL && SUPERCL! = object.class) {System.out.print ("extends" + Supercl.getname ());} System.out.print ("\n{\n");p rintconstructors (CL); System.out.println ();p rintmethods (CL); System.out.println ();p rintfieLDS (CL); System.out.println ("}");} catch (ClassNotFoundException e) {e.printstacktrace ();}} /** * Print all constructors of a class * @param cl a class */public static void Printconstructors (class cl) {/** * constructor class is used to describe the constructor of the class; * cl. Getdeclaredconstructors () returns all constructors declared in the class, excluding */constructor[in the parent class] constructors = Cl.getdeclaredconstructors (); for ( Constructor c:constructors) {//Get constructor name String name = C.getname (); System.out.print ("");//Gets the constructor descriptor string modifiers = Modifier.tostring (C.getmodifiers ()); if (Modifiers.length () > 0 {System.out.print (modifiers + "");} System.out.print (name + "(");//print parameter Type list class[] paramtypes = C.getparametertypes (); for (int j = 0; J < Paramtypes.length; J + +) {if (J > 0) {System.out.print (",");} System.out.print (Paramtypes[j].getname ());} System.out.println (");");}} /** * Print all methods of a class * @param cl a class */public static void Printmethods (class cl) {/** * Method class is used to describe the methods of the class; * CL.GETDECLAREDM Ethods () returns all methods declared in the class, * excluding */method[in the parent class] methods = Cl.getdeclaredmethods (); for (Method M:methods) {//Gets the class object that describes the type of return type class RetType = M.getreturntype ();//Gets the method name String name = M.getname (); System.out.print ("");//print modifier, return type and method name string modifiers = modifier.tostring (M.getmodifiers ()); if (Modifiers.length () > 0) {System.out.print (modifiers + "");} System.out.print (Rettype.getname () + "+ name +" (");//print parameter Type list class[] paramtypes = M.getparametertypes (); for (int j = 0; J < Paramtypes.length; J + +) {if (J > 0) {System.out.print (",");} System.out.print (Paramtypes[j].getname ());} System.out.println (");");}} /** * Print all fields of a class * @param cl a class */public static void Printfields (class cl) {/** * Field class is used to describe the domain of the class; * Cl.getdeclaredfield S () returns all fields declared in the class, * excludes */field[in the parent class] field = Cl.getdeclaredfields (); for (Field f:fields) {//Get Class object class type that describes the type of domain = F.gettype ();//Gets the name of the domain string name = F.getname (); System.out.print ("");//Gets the domain descriptor string modifiers = Modifier.tostring (F.getmodifiers ()); if (Modifiers.length () > 0) { System.out.print (modifiers + "");} System.out.println (type.getname () + "+ name +"; ");}}} 

Here is a class to be analyzed:

Package Testreflection;public class Student extends person {private static int count;private String Stunum;public Student ( {super ();//TODO auto-generated constructor stubcount++;} Public Student (string name, int age, Boolean gender, String stunum) {Super (name, age, gender);//TODO auto-generated const Ructor stubthis.stunum = stunum;count++;} Public String Getstunum () {return stunum;} public void Setstunum (String stunum) {this.stunum = Stunum;} public static int GetCount () {return count;}}

The source code for the parent class of the student class is as follows:

Package Testreflection;public class Person {private String name;private int age;private boolean gender;public person () {// TODO auto-generated Constructor stub}public person (String name, Int. age, Boolean gender) {super (); this.name = NAME;THIS.A GE = Age;this.gender = gender;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} public Boolean Isgender () {return gender;} public void Setgender (Boolean gender) {This.gender = gender;}}

the results of the student class using the Reflectiontest class are as follows:

1
Public
public class testreflection. Student extends Testreflection. Person
{
Public testreflection. Student ();
Public testreflection. Student (java.lang.String, int, boolean, java.lang.String);

public static int GetCount ();
public void Setstunum (java.lang.String);
Public java.lang.String getstunum ();

private static int count;
Private java.lang.String Stunum;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Reflection (ii) Ability to use reflection analysis classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.