The reflection mechanism of Java "introductory version (3)"

Source: Internet
Author: User

Java Reflection
Java.lang.Class
Object-oriented principles generally believe that everything is the object
A. Class name.
B. If the object exists, the object can be direct. GetClass
C.class.forname ("Full name of the class");




Example:


Package Corelesson;



Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;

Import Javax.swing.JButton;


public class ClassDemo3 {
public static void Main (string[] args) {
ClassDemo3.class.getClassLoader () Gets the class loader object
System.out.println (ClassDemo3.class.getClassLoader (). GetClass (). GetName ());
Printclassmessage (New JButton ());
}
public static void Printclassmessage (Object obj) {
To get the class information, first get the class type, 3 ways to choose according to the existing conditions
Class C = Obj.getclass ();
Get class Name
System.out.println (C.getname ());
Gets the class name that does not contain the package name
System.out.println (C.getsimplename ());
Gets the method of the class--the object of the Java.lang.reflect.Method class
/*getmethod gets all the public's methods
* The parent class inherits the same
* Getdeclaredmethods () is a method of declaring itself, without asking access rights
* The inheritance cannot be reached.
* */
Method[] ms = C.getmethods ();
for (int i = 0;i < ms.length;i++) {
Gets the class type of the method's return value type


Class returntype = Ms[i].getreturntype ();
System.out.print (Returntype.getname () + "");
System.out.print (Ms[i].getname () + "(");


Gets the parameter list class type for the method
class[] Paramstype = Ms[i].getparametertypes ();
for (int j = 0;j <paramstype.length;j++) {
System.out.print (paramstype[j]+ ",");
}
System.out.println (")");


}
System.out.println ("+++++++++++++++++++++++++++++++++");
/* Member variables are also object Java.lang.reflect.Field;
* Getdeclatredfields gets all the member variables of its own declaration
* GetFields method gets the member variable for all public
*
*
* */
field[] fs = C.getdeclaredfields ();
for (Field Field:fs) {
Gets the class type of the member variable
Class FieldType = Field.gettype ();
Gets the type and name of the member variable
System.out.println (Fieldtype.getname () + "" +field.getname ());


}
System.out.println ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
/* All constructors are objects of the Java.lang.reflect.Constructor class
* Getdeclaredconstructors (Get All)
* GetConstructor Get the public
*
* */
Constructor[] cs = c.getdeclaredconstructors ();
for (Constructor Constructor:cs) {
System.out.print (Constructor.getname () + "(");
Gets the class type of the argument list of the constructor
class[] Paramstype = Constructor.getparametertypes ();
for (Class Class1:paramstype) {
System.out.print (Class1.getname () + ",");
}
System.out.println (")");
}


}
}


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

The reflection mechanism of Java "introductory version (3)"

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.