Core Java notes (five)

reflection

 

effect:

  • In the run-time analysis capability class

  • View objects at run time

  • To achieve a common operation code array

  • Use Method objects

 

A, Class 

 

During the program runs, the runtime system for all Java objects always maintain a runtime (runtime) type of identification, tracking this information belongs to the class of each object, the type of information to select the appropriate method for performing virtual opportunity to use the runtime. Class class save this information, the object can be obtained by a Class Object getClass method defined in the object class represents a particular attribute.

By getName method returns the name of the class:

Employee e = new Employee("Babara", ...);
System.out.println(e.getClass().getName() + ": " + e.getName());  //  Employee: Babara   

Static methods may be obtained by forName Class object corresponding to the class name:

String className = "java.util.Random";
Class cl = Class.forName(className);

Actually represents a Class object type is a (not necessarily a class), assuming that T is an arbitrary Java type, representative of the corresponding Class object T.class:

Class cl1 = Random.class;
Class cl2 = int.class;
Class cl3 = Double[].class;
// ...

Virtual machine management for each type a Class object, can be compared using == operator:

if (e.getClass() == Employee.class)  // ...

Can create an instance of a class using the newInstance method dynamically (without parameters if the class is not a constructor, an exception is thrown):

String str = "java.util.Random";
Object m = Class.forName(str).newInstance();  // m = new Random();  

Class class is actually a generic class .

 

API - java.lang.Class

  • forName

  • getName

  • newInstance

  • getField
  • getFields / getMethods / getConstructors

  • getDeclareFields / getDeclareMethods / getDeclareConstructors


Second, check the structure of the class

 

java.lang.reflect package have three classes Field, Method, Constructor fields are used to describe the class, method, constructor, as well as to reflect Modifier Modifier constructor method.

 

API - java.lang.reflect.Constructor

  • getDeclaringClass

  • getExceptionTypes

  • getModifiers

  • getName
  • getParameterTypes

  • GetReturnType

 

The following is a test Demo book, enter the class name, the class can output all domain names and signatures methods and constructors:

import java.util.*;
import java.lang.reflect.*;

public class ReflectionTest {
    public static void main(String[] args) {
        // 读取类名
        String name;
        if (args.length > 0) {
            name = args[0];
        } else {
            Scanner in = new Scanner(System.in);
            System.out.println("输入类名(如:java.util.Data):");
            name = in.next();
        }

        // 打印类名和超类名
        try {
            Class cl = Class.forName(name);
            Class supercl = cl.getSuperclass();
            String modifiers = Modifier.toString(cl.getModifiers());
            if (modifiers.length() > 0) {
                System.out.print(modifiers + " ");
            }
            System.out.print("class " + name);
            if (supercl != null && supercl != Object.class) {
                System.out.print(" extends " + supercl.getName());
            }
System.out.print(" {\n"); printFields(cl); System.out.println(); printConstructors(cl); System.out.println(); printMethods(cl); System.out.println(
"}"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.exit(0); } public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for (Field f : fields) { Class type = f.getType(); String name = f.getName(); System.out.print("\t"); String modifiers = Modifier.toString(f.getModifiers()); if (modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.println(type.getName() + " " + name + ";"); } } public static void printConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors(); for (Constructor c : constructors) { String name = c.getName(); System.out.print("\t"); String modifiers = Modifier.toString(c.getModifiers()); if (modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.print(name + " "); printParameters(c.getParameterTypes()); } } public static void printMethods(Class cl) { Method[] methods = cl.getDeclaredMethods(); for (Method m : methods) { Class retType = m.getReturnType(); String name = m.getName(); System.out.print("\t"); String modifiers = Modifier.toString(m.getModifiers()); if (modifiers.length() > 0) { System.out.print(modifiers + " "); } System.out.print(retType.getName() + " " + name); printParameters(m.getParameterTypes()); } } public static void printParameters(Class[] paramTypes) { System.out.print("("); for (int i = 0; i < paramTypes.length; i++) { if (i > 0) { System.out.print(", "); } System.out.print(paramTypes[i].getName()); } System.out.println(");"); } }

Operation:

 

Guess you like

Origin www.cnblogs.com/zzzt20/p/11460511.html