This is the 19th day of my participation in the More text Challenge. For details, see more text Challenge


Related articles

Java Annotations and Reflection series: Java Annotations and reflection series


preface

Importance in a word: Almost all of the underlying framework is implemented with reflection!

An overview,

Dynamic language: a type of language that can change its structure at run time. More generally, the code can change its structure according to certain conditions at run time. Main dynamic languages: Object-c, C#, JavaScript, PHP, Python, and other static languages: In contrast to dynamic languages, languages whose runtime structure is immutable are static languages. Such as Java, C, C++

But Java can do that through the reflection mechanismQuasi dynamic language!!!!!!!!!

Second, reflex mechanism

The Reflection mechanism allows a program to obtain the internal information of any class during execution with the help of the Reflection API and to directly manipulate the internal properties and methods of any object.

1, Java. Reflection

How do we use an object during development?

The normal way is to introduce the desired package class name → instantiate it via new → obtain the instantiated object

Reflection: instantiate the object → getClass() method → get the full “package class” name

What are the advantages and disadvantages of this approach?

Advantages: You can create objects and compile them dynamically, giving you a lot of flexibility

Disadvantages: Performance impact, using reflection is basically an interpreted operation and is always slower than normal operation.

2, several ways to obtain Class objects

1) getClass

Given an instance of a Class, call the getClass () method on that instance to get the Class object

First let’s look at the root of all evil:

As you can see, there’s a getClass method, so the Object class has, which proves that all classes should have this method!

That is, you can use object reflection to find the name of the class.

(2) Class. The Class

If the specific class is known, the method is the most secure and reliable, and the program performance is the highest

(3) class.forname

If the full Class name of a Class is known and the Class is in the classpath, it can be obtained by the Class Class static method forName (), which may throw a ClassNotFoundException

(4) the name of the class Type

If it isBuilt-in basic data typesYou can just use the class name. Type

(5) class. GetSuperclass ()

Use ClassLoader to get the type of the parent class.

According to the calss object can get the object of the calss object’s parent class!

3. Summary:

All the code looks like this:

		Person person = new Person();

        // Obtain the class object by instantiating the object
        Class c1 = person.getClass();
        System.out.println("c1 "+c1.hashCode());

        // Obtain the Class object from.class
        Class c2 = Person.class;
        System.out.println("c2 "+c2.hashCode());

        // Get the class object by forName
        Class c3 = Class.forName("Com. Dbright. Java reflection. Person");
        System.out.println("c3 "+c3.hashCode());

        // Pass the class name.type
        Class c4 = Integer.TYPE;
        Class c41 = Integer.class;
        System.out.println("c4 "+c4.hashCode());
        System.out.println("c41 "+c41.hashCode());

        / / object. GetSuperclass ()
        Class c5 = c1.getSuperclass();
        System.out.println("c5 "+c5.hashCode());
Copy the code

The execution result is as follows:

The above results can be divided into three parts:

C1, C2, and C3 are obtained in three different ways, but their hashCode values are the same.

The basic Java type, which can be obtained from the. Type method, has the same effect as the. Class method based on the hashCode value.

3, we can get the parent class object!

Results:

Objects look in the mirror to get information about a class’s properties, methods, and constructors, and what interfaces a class implements. For each Class, the JRE reserves an immutable object of type Class for it.

  • Class itself is also a Class
  • Class objects can only be created by the system
  • A loaded Class will have only one Class instance in the JVM
  • A Class object corresponds to a.class file loaded into the JVM
  • Each Class instance remembers which Class instance it was generated by
  • Class gives you a complete view of all the loaded structures in a Class
  • The Class Class is the root of Reflection, and for any Class you want to load and run dynamically, you have to get the corresponding Class object first

4. Small extensions

Class of all types

// Class of all types
public class ReflectionTest{
    public static void main(String[] args) {
        Class c1 = Object.class;  / / class Java. Lang. Object
        Class c2 = Comparable.class;  // Interface java.lang.Comparable
        Class c3 = String[].class;  Class [ljava.lang.string;
        Class c4 = int[][].class;  Class [[I
        Class c5 = Override.class;  // Annotate interface java.lang.Override
        Class c6 = ElementType.class;  / / the enumeration class Java. Lang. The annotation. ElementType
        Class c7 = Integer.class;  / / basic data types class Java. Lang. The annotation. ElementType
        Class c8 = void.class;  //void void
        Class c9 = Class.class;  //Class class java.lang.Class

        //Alt + left: Select all and copy and paste
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
        System.out.println(c4);
        System.out.println(c5);
        System.out.println(c6);
        System.out.println(c7);
        System.out.println(c8);
        System.out.println(c9);

        // As long as the element type is the same as only, it is the same Class
        int[] a = new int[10];
        int[] b = new int[100]; System.out.println(a.getClass().hashCode()); System.out.println(b.getClass().hashCode()); }}Copy the code

The execution result is as follows:


I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah