Java Reflection mechanism

Source: Internet
Author: User

The method is obtained by reflection:

Method keyword

Meaning

Getdeclaredmethods()

Get all the methods

Getreturntype()

Get the return type of the method

Getparametertypes()

Get the incoming parameter type for a method

Getdeclaredmethod ("Method name", Parameter type. Class,......)

Get a specific approach

Constructor method keyword

Meaning

Getdeclaredconstructors()

Get all the construction methods

Getdeclaredconstructor (parameter type. Class,......)

Get a specific construction method

Parent class and Parent interface

Meaning

Getsuperclass()

Gets the parent class of a class

Getinterfaces()

Get an interface for a class implementation

code example:

@Test Public voidFun ()throwsclassnotfoundexception{//Building ClassesClass<?> cls = Class.forName ("Fanshe. User "); //Get Propertiesfield[] Fields =Cls.getdeclaredfields ();  for(Field field:fields) {System.out.println (modifier.tostring (Field.getmodifiers ())+ "\ n" +//get the property's adornmentsField.gettype (). Getsimplename () + "\ n" +//get the type of the propertyField.getname ()//get the name of the property                    ); }                //Get the methodMethod[] Methods =Cls.getdeclaredmethods ();  for(Method method:methods) {System.out.println (modifier.tostring (Method.getmodifiers ()) /c9>+ "\ n" +//get the modification of a methodMethod.getreturntype (). Getsimplename () + "\ n" +//gets the type of the return valueMethod.getname ()//Get the name                    );            } System.out.println (Cls.getsimplename ()); }

Java Reflection mechanism

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.