Java reflection Gets the method information (instance code) of an object through reflection _java

Source: Internet
Author: User
Tags reflection

The following code is a tool class

Package com.imooc.reflect;

Import Java.lang.reflect.Method; public class Classutil {public static void Printclassmessage (Object obj) {//To get information about the class, first get the class type class C = Obj.getcl
		Ass ();//Pass the object of which subclass, C is the class type of the subclass//Get the class name System.out.println ("Class name is:" +c.getname ()); * * Methods class, Method Object * A member method is a Methods object * GetMethods () method gets all the public functions, including the inherited from the parent class * Getdeclaredmethods () gets all
		This class declares its own method, the location of access rights * * * method[] methods = C.getmethods ();
			
			for (int i=0;i<methods.length;i++) {//the class type class ReturnType = Methods[i].getreturntype () of the return value type of the method.
			System.out.print (Returntype.getname () + "");
			Get the name of the method System.out.print (Methods[i].getname () + "(");
			Get the parameter type--"The Type de class type of the argument list" class[] Paramtype = Methods[i].getparametertypes ();
			for (Class class1:paramtype) {System.out.print (Class1.getname () + ",");
		} System.out.println (")"); The name of the//output://class is: java.lang.String//boolean equals (Java.lang.Object,)//java.lang.string toString ()//int has HCode ()//.....
 

The following code is a test class:

Package com.imooc.reflect;

public class Testclassutil {public

	static void Main (string[] args) {
		string string = "Hello";
		Classutil.printclassmessage (string);
		
		Integer n1 = 1;
		Classutil.printclassmessage (n1);
	}

Screenshot below:

The above is a small series for everyone to bring the Java reflection to get an object by reflection of the method information (instance code) of the whole content, hope to help everyone, a lot of support cloud Habitat Community ~

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.