Java Reflection Common API Rollup

Source: Internet
Author: User
Tags modifier

"The Java reflection mechanism is in a running state where all the properties and methods of the class are known to any class, and any of its methods and properties can be called for any object"

The acquisition of a class object

1. Getting through objects

New Object (); Obj.getclass ();

2. Get through the class name

Object. class;

3. Get through the path name of the class

Class.forName ("Com.metadata.Student");

Ii. instantiation and constructors of classes

The obtained class object can be instantiated directly from the Clazz.newinstance () method, but requires the target class to have a default parameterless constructor, or an exception will be thrown.

In cases where the class does not have a default parameterless constructor, or if a specific constructor is required to instantiate, it needs to be done through the newinstance () of the constructor class.

1. Get the public constructor, excluding the parent class

Class
public constructor<?> public constructor<t> getconstructor (class<?> ..... Parametertypes)

2. Get the current class constructor, ignore modifiers

// Class  Public Constructor<?>[] getdeclaredconstructors () public constructor<t> Getdeclaredconstructor (CLASS<?> parametertypes)

Constructor call

// Constructor  Public T newinstance (Object ... initargs)
Ignore modifier, Force call
public void Setaccessible (Boolean flag)

Third, the acquisition of class member variables

1. Get public variables, including the parent class

// Class  Public field[] GetFields ()  Public Field GetField (String name)

2. Gets the current class member variable, ignoring the modifier

// Class  Public field[] Getdeclaredfields ()  Public Field Getdeclaredfield (String name)

Member variable Assignment

// Field // obj As an instance object  Public void Set (Object obj,object value)

Ignore modifier, Force call
public void Setaccessible (Boolean flag)

Iv. acquisition of class methods

1. Get the public method, including the parent class

// Class  Public method[] GetMethods ()  Public Method GetMethod (String name,                        Class<?> .... parametertypes)

2. Gets the current class method, ignoring the modifier

// Class  Public method[] Getdeclaredmethods ()  Public Method Getdeclaredmethod (String name,                                Class<?> .... parametertypes)

Method invocation

// Method // obj instantiates an object for a class if it is a static method, obj is nullinvoke (Object obj, object ... args)

Ignore modifier, Force call
public void Setaccessible (Boolean flag)

Java Reflection Common API Rollup

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.