Java reflection mechanism

The Java reflection mechanism mainly provides the following functions:
judging the class to which any object belongs at runtime.
Constructs an object of any class at runtime.
Determine the member variables and methods of any class at runtime.
Call a method on any object at runtime.


Commonly used classes for reflection mechanism:
Class Class: Represents a class.
Field class: represents the member variables of the class (member variables are also called properties of the class).
Method class: represents the method of the class.
Constructor class: represents the constructor of the class.
Array class: Provides static methods for dynamically creating arrays and accessing elements of arrays.

// Query all ongoing activities
		T7046[] result = getValidActivity();
		if (result != null && result.length > 0) {
			for (T7046 data : result) {
				String allClassName = className.replace("@", data.F12);
				try {
					/*1. Get Class according to the class name*/
					/*2. Get its constructor according to Class*/
					Constructor<?> constructor = Class.forName(allClassName).getConstructor(ServiceResource.class);
					/*3. Use class loading to create objects*/
					Object activityObject = constructor.newInstance(serviceResource);
					/*4. Get the creation class*/
					Class<? extends Object> activityClass = activityObject.getClass();
					/*5. Dynamically call the specified method*/
					String temp = (String)activityClass.getMethod("executeActivity", Object[].class).invoke(activityObject, (Object)params);
					if(!StringHelper.isEmpty(temp)){
						message = temp;
					}
				} catch (ClassNotFoundException e) {
					System.out.println("Class: "+allClassName+" does not exist, the activity cannot be executed!");
					continue;
				}
			}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326939955&siteId=291194637