Reflection mechanism, reflection mechanism in java

Source: Internet
Author: User

Reflection mechanism, reflection mechanism in java

1. What is the reflection mechanism?

The reflection mechanism is used to access information of classes or objects that have been loaded into the JVM.

Second Class 1. The first step to use the reflection mechanism is to obtain the objects of a Class or object in the JVM. There are three methods: (1) Use the fully qualified Class name to call the forName Method for creation:
Class c = Class. forName ("fully qualified Class name ");
(2) Use the object of this class to obtain:
Object obj=new Object();Class c=obj.getClass();
(3) directly create using the Class Name:
Class c=Object.class.
2. Related methods:
  • NewInstance (): Call the no-argument constructor to create an object. The class can only create an object through the no-argument constructor. If you want to create an object through the constructor with parameters, you must use the constructor.
  • Constructor [] getConstructors: obtains a Constructor with all the public permissions.
  • Constructor [] getConstructor (Class... parameterTypes): obtains the Constructor with specified type parameters.
  • Constructor [] getDeclaredConstructors: obtains all Constructor methods, including private methods.
  • Method [] getMethods: obtain all public methods.
  • Field [] getFields: obtains all member variables with the public permission.
Three Constructor
  • Class [] getParameterTypes: return the types of parameters in the constructor in an array in the declared order.
  • Int getParameterCount: returns the number of parameters in the constructor.
  • NewInstance (Object... initargs): input the initial value creation Object to this constructor.
  • GetModifiers: returns an integer representing the constructor modifier. An integer contains information about multiple permission modifiers. Call static method parsing again to view the modifier of the constructor.
  • SetAccessible (true): converts a private constructor to a public constructor to create an object.

The objective of using the reflection mechanism to access constructor is to create an object using the constructor with parameters.

Four Methods
  • GetName: Get method name.
  • GetReturnType: return value type.
  • GetParameterTypes: Get the parameter type.
  • GetModifiers: Get the integer that can parse the modifier.
  • Invoke (obj, args...): Use the args parameter to execute this method in the obj object. The specific method must be attached to the object.

The purpose of accessing the methods in an object through the reflection mechanism is to execute the methods.

Five fields
  • GetName: get the name of the member variable.
  • GetType: Get the type of the member variable.
  • Get (obj): get the value of the member variable in the specified object obj.
  • SetObject (obj, args): assign a value to args for this variable in the specified object obj. Specific parameters must be attached to objects.
  • SetAccessible (true): converts a variable from the private type to the public type.

The purpose of accessing a member variable through the reflection mechanism is to obtain the value of the member variable in the specified object or assign values to the member variable, that is, the value and value assignment.

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.