Java reflection class loader and java reflection Class Loader

Source: Internet
Author: User

Java reflection class loader and java reflection Class Loader

Preface:

The java language allows indirect Class operations in a programmatic manner. After the Class file is loaded by the Class loader, a metadata object describing the Class structure will be formed in the jvm, the metadata object can be used to obtain the structure information of the Class, such as constructors, attributes, and methods,

Java allows users to indirectly call the Class object function through the meta information object related to the Class, which opens up a way to operate Class objects in a programmatic way.

Instance:

The following Car class has two constructors, one method and three attributes.

Package reflect; public class Car {private String brand; // brand private String color; // color private int maxSpeed; // speed public Car () {} public Car (String brand, string color, int maxSpeed) {this. brand = brand; this. color = color; this. maxSpeed = maxSpeed;} public void say () {System. out. println ("brand:" + brand + ", color:" + color + ", maxSpeed:" + maxSpeed) ;}// the set and get methods are omitted.

Generally, we use the following code to create a Car instance:

Car car = new Car (); car. setBrand ("Hongqi ");

In this way, the target class is directly called in the traditional method. Below we can use the java reflection mechanism to manipulate the target class in an indirect way:

Public class ReflectTest {public static Car initbydefaconconst () throws Throwable {// 1. get the Car Class Object ClassLoader loader = Thread through the class loader. currentThread (). getContextClassLoader (); // use the fully qualified name to find the class Class clazz = loader. loadClass ("reflect. car ");/* Class clazz = Class. forName ("reflect. car "); * // another way to get class // 2. get the default Constructor object of the class and use it to instantiate Car Constructor cons = clazz. getDeclaredConstructor (Class []) null); Car car = (Car) cons. newInstance (); // 3. set the property Method setBrand = clazz by using the reflection Method. getMethod ("setBrand", String. class); setBrand. invoke (car, ""); Method setColor = clazz. getMethod ("setColor", String. class); setColor. invoke (car, "black"); Method setMaxSpeed = clazz. getMethod ("setMaxSpeed", int. class); setMaxSpeed. invoke (car, 200); return car;} @ Test public void test () throws Throwable {Car car = initByDefaultConst (); car. say ();}}

Run the above program and print the following on the console: brand: Red Flag, color: Black, maxSpeed: 200

The above program only uses a few APIs. the java reflection package provides us with a lot of methods, which can be further checked.

Other extensions:

Working Mechanism of ClassLoader:

The class loader is to find the class code file and construct a component that represents the object in the jvm. in java, the Class Loader loads a class into the jvm, follow these steps:

1. Load: Find and import Class files

2. Link: perform verification, preparation, and resolution steps. The resolution steps are optional.

2.1. Check: Check whether the data loaded into the Class file is correct.

2.2. Preparation: allocate storage space to static variables of the class

2.3. Resolution: convert a symbolic reference to a direct reference

3. Initialization: Initialize the static variables and code blocks of the class.

The JVM load class uses the "full responsibility delegation mechanism ",

"Full responsibility" means that when a ClassLoader loads a class (unless explicitly using another ClassLoader), the class dependent on and referenced by this class is also loaded by this ClassLoader;

The "delegation mechanism" refers to entrusting the parent loader to find the target class. It can only find and load the target class from its own class path if it cannot be found.

This is from the security point of view. Imagine if someone has compiled a malicious basic class (such as java. lang. string) and load it into the JVM, which can cause terrible consequences,

However, java. lang. String is always loaded by the root loader, which avoids the above security risks.

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.