Reflection, new features of interface, Lambda expression

reflex

concept

Java reflection mechanism is to know all the properties and methods of any class in the running state; For any object, you can call any of its methods and properties; This kind of dynamically obtained information and the function of dynamically calling the method of the object are called the reflection mechanism of Java language.

In the process of program execution, the dynamic call does not write the code to call other classes

Class loader

Hand over the compiled class binary bytecode file to the jvm for execution

Three default class loaders

**Start class loader: * * load and execute the class file that starts the jvm

**Extension loader: * * load and execute the java running basic jar package

**System loader: * * load and execute the class file we wrote

Class object class

Everything in java is an object. When using reflection, all custom classes are treated as class objects, which are class objects

Three ways to get class objects

Class c1=Class.forName("class name");

Use the class static method to load the specified class object through the class loader internally

Class c2 = class name class;

Get the class object of the specified class through the class attribute of the specified class

Class c3=obj.getClass();

Get the specified class object through the getClass method of the specified class object

		// Class can represent all classes. Unlike object, object is the parent class of all classes
		// All classes are objects of Class
		// How to get a class object
		// 1. Use Class static method to get
		Class c1 = Class.forName("com.yunhe.day1106.Student");

		// 2. Use the class attribute of the class to obtain (to obtain the class object in this way, the variable must be declared and saved)
		Class c2 = Student.class;

		// 3. Use the getClass method of the specified class object to get
		Student s = new Student();
		Class c3 = s.getClass();
		
	
		Object newInstance = c1.newInstance();//Call the parameterless constructor of the class represented by the specified class object
		System.out.println(newInstance);

Reflection acquisition construction method

Through the method of obtaining the construction method provided by the clas class, the return construction method object represents the construction method of the specified class

Constructor getConstructor(Class<?>... parameterTypes)
Returns a Constructor object that reflects the specified public Constructor of the Class represented by this Class object.
Constructor<?>[] getConstructors()
Returns an array containing some Constructor objects that reflect all the common Constructor methods of the Class represented by this Class object.
Constructor getDeclaredConstructor(Class<?>... parameterTypes)
Returns a Constructor object that reflects the specified Constructor of the Class or interface represented by this Class object.
Constructor<?>[] getDeclaredConstructors()
Returns an array of Constructor objects that reflect all construction methods declared by the Class represented by this Class object.

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
//Get construction method through reflection
public class ConstructorTest {
	public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
			InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Class<?> c = Class.forName("com.yunhe.day1106.Student");

		// Gets the class object of the specified class through reflection
		// Get the constructor object of the specified class through the class object
		// getConstructor(Class<?>... parameterTypes)
		// Obtain the construction method of the specified number and type of parameters through parameters
		Constructor<?> c1 = c.getConstructor();// Construction method without parameter disclosure
		Object c1o = c1.newInstance();
		// System.out.println(c1o);
		// Execute the construction method and pass in the data required for execution
		Constructor<?> c2 = c.getConstructor(String.class);// Gets the constructor whose exposed parameter type is String
		Object c2o = c2.newInstance("Zhang San");
		// System.out.println(c2o);

		// Constructor<?>[] getConstructors()
		// Gets all the exposed constructor methods of the class represented by the current class object
		Constructor<?>[] constructors = c.getConstructors();
		// for (Constructor<?> constructor : constructors) {
		// System.out.println(constructor);
		// }

		// Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
		// Gets the constructor of the specified parameter (including private constructor)
		Constructor<?> c3 = c.getDeclaredConstructor(String.class, int.class);
		c3.setAccessible(true);// Empowering private construction methods
		Object c3o = c3.newInstance("Zhang San", 18);
		System.out.println(c3o);

		// Constructor<?>[] getDeclaredConstructors()
		// Returns an array of Constructor objects that reflect all construction methods declared by the Class represented by this Class object.

		Constructor<?>[] declaredConstructors = c.getDeclaredConstructors();
		for (Constructor<?> constructor : declaredConstructors) {
			System.out.println(constructor);
		}

	}

}

Reflection get properties

Field getField(String name)
Returns a Field object that reflects the specified public member Field of the Class or interface represented by this Class object.
Field[] getFields()
Returns an array containing some Field objects that reflect all accessible public fields of the Class or interface represented by this Class object.
Field getDeclaredField(String name)
Returns a Field object that reflects the specified declared Field of the Class or interface represented by this Class object.
Field[] getDeclaredFields()
Returns an array of Field objects that reflect all fields declared by the Class or interface represented by this Class object.

import java.lang.reflect.Field;
import java.util.Arrays;

//Get properties through reflection
public class FieldTest {
	public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException {
		Class<?> c = Student.class;
		Object o = c.newInstance();
		// Field getField(String name)
		// Gets the public property of the specified name of the class represented by the specified class object
		Field field = c.getField("name");
		//System.out.println(field);
		
		// Field[] getFields()
		// Returns an array containing some Field objects that reflect all accessible public fields of the Class or interface represented by this Class object.
		Field[] fields = c.getFields();
		//System.out.println(Arrays.toString(fields));

		
		// Field getDeclaredField(String name)
		// Gets the property object (including private property) with the specified name of the class represented by the specified class object
		Field declaredField = c.getDeclaredField("age");
		//System.out.println(declaredField);
		
		// Field[] getDeclaredFields()
		// Returns an array of Field objects that reflect all fields declared by the Class or interface represented by this Class object.
		Field[] declaredFields = c.getDeclaredFields();
		//System.out.println(Arrays.toString(declaredFields));
		System.out.println(o);
		
		//Use of Field property object
		//Weighting operation shall be carried out before using private attributes
		declaredField.setAccessible(true);
		//The set method assigns a value to the specified object
		declaredField.set(o, 18);
		field.set(o, "Zhang San");

		//get method gets the property of the specified object property
		Object object = field.get(o);
		System.out.println(object);	
	}
}

Reflection acquisition method

Method getMethod(String name, Class<?>... parameterTypes)
Returns a Method object that reflects the specified public member Method of the Class or interface represented by this Class object.
Method[] getMethods()
Returns an array containing some Method objects that reflect the public member methods of the Class or interface represented by this Class object (including those declared by the Class or interface and those inherited from the superclass and superinterface).
Method getDeclaredMethod(String name, Class<?>... parameterTypes)
Returns a Method object that reflects the specified declared Method of the Class or interface represented by this Class object.
Method[] getDeclaredMethods()
Returns an array of Method objects that reflect all the methods declared by the Class or interface represented by this Class object, including public, protected, default (package) access and private methods, but excluding inherited methods.

import java.lang.reflect.Method;

public class MethodTest {
	public static void main(String[] args) throws Exception {
		Class<?> c = Student.class;
		Object o = c.newInstance();
		// Method getMethod(String name, Class<?>... parameterTypes)
		// Return a Method object and get the Method object (public) with the specified name and the specified parameter list
		Method method = c.getMethod("setName", String.class);
		// System.out.println(method);

		// Method[] getMethods()
		// Returns an array containing some Method objects that reflect this Class
		// Returns all exposed methods (including methods that inherit the parent class)
		Method[] methods = c.getMethods();
		// for (Method method2 : methods) {
		// System.out.println(method2);
		// }

		// Method getDeclaredMethod(String name, Class<?>... parameterTypes)
		// Gets the specified method (including private)
		// Gets the method defined in the current class
		Method declaredMethod = c.getDeclaredMethod("a",String.class);
		// System.out.println(declaredMethod);

		// Method[] getDeclaredMethods()
		// Returns an array of Method objects that reflect this Class
		// All methods declared by the class or interface represented by the object, including public, protected, default (package) access and private methods, but excluding inherited methods.
		Method[] declaredMethods = c.getDeclaredMethods();
		// for (Method method2 : declaredMethods) {
		// System.out.println(method2);
		// }
		
		//Method of execution
		//Private methods need to be empowered before execution
		method.invoke(o, "Zhang San");
		
		declaredMethod.setAccessible(true);
		//Call the object and parameters of the incoming call using the method object involve method (no parameters, no writing)
		declaredMethod.invoke(o, "asd");
	}
}

summary

Reflection function: through the code writing of a certain format in advance, the class can be used dynamically during the running process of the program

Use of reflection:

1. Gets the class object of the specified class

① Use the classloader to load the full path of the specified class

② Get using the class attribute of the class

③ Get using the getclass method of the class object

2. Construction method

① Acquisition of construction methods: four methods are used to obtain construction methods

② Use of construction methods: weighting and creating objects

3. Attributes

① Acquisition of attributes: four methods are used to obtain attributes (note whether inherited attributes can be obtained when acquiring attributes)

② Use of attributes: weighting, assignment and obtaining values (pay attention to the objects and values that need to be assigned)

4. Method

① Method acquisition: four ways to obtain methods (pay attention to whether inherited methods can be obtained when obtaining methods)

② Method usage: weighting and execution (note the objects and parameters that need to be passed in to execute the method)

Tags: Java

Posted by jaku78 on Fri, 06 May 2022 10:39:13 +0300