当前位置:网站首页>Execute any method of any class through reflection
Execute any method of any class through reflection
2022-07-02 12:41:00 【Pastoral ing】
Execute any method of any class through reflection
Java The reflection mechanism of : When the program is running, you can call
Class aClass = Class.forName(className);
Object obj = aClass.newInstance(); Equireflective API Get the internal information of any class ( Field 、 Such method ), And can operate these internal information
We're calling Class.forName(className) When , Will load a Class Object of type , This object is passed in className, That is to say, we can pass this Class Object operation of type className Methods 、 Properties, etc .
1、 Project structure

2、 Two classes for demonstration
package com.zhou.reflect.pet;
/** * @author DELL * @version 1.0 * @Description * @date 2022/5/24 16:58 */
public class Cat {
public void sound(){
System.out.println(" cat ...");
}
}
package com.zhou.reflect.pet;
/** * @author DELL * @version 1.0 * @Description * @date 2022/5/24 16:54 */
public class Dog {
public void sound(){
System.out.println(" Wang Wang Wang ...");
}
}
3、properties
# Here, configure the class path to be called
className=com.zhou.reflect.pet.Cat
# Method to call
methodName=sound
4、 The main implementation
package com.zhou.reflect;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.lang.reflect.Method;
import java.util.Properties;
/** * @author DELL * @version 1.0 * @Description * @date 2022/5/24 17:05 */
public class ReflectFactory {
public static void main(String[] args) {
/* * demand : Do not modify the code of this class , Automatically call any method of any class * */
try {
//1、 obtain resource In the catalog properties
ClassPathResource classPathResource = new ClassPathResource("/application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(classPathResource);
//2、 Get the contents of the configuration file
String className = properties.getProperty("className");
String methodName = properties.getProperty("methodName");
//3、 Get the object of the class
Class aClass = Class.forName(className);
//4、 Initialize this object
Object obj = aClass.newInstance();
//5、 Get the method of the class
Method method = aClass.getMethod(methodName);
//6、 Execute this method
method.invoke(obj);
} catch (Exception ex) {
System.out.println(ex);
}
}
}
5、 Execution results
Calls configured in the configuration file Cat Of sound Method , The output is “ cat …”
Calls configured in the configuration file dog Of sound Method , The output is “ Wang Wang Wang …”
边栏推荐
- LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
- Openssh remote enumeration username vulnerability (cve-2018-15473)
- Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
- In development, why do you find someone who is paid more than you but doesn't write any code?
- 深拷贝 事件总线
- Embedded Software Engineer career planning
- 堆 AcWing 838. 堆排序
- Multiply LCA (nearest common ancestor)
- "As a junior college student, I found out how difficult it is to counter attack after graduation."
- SparkContext: Error initializing SparkContext解决方法
猜你喜欢

Lekao.com: experience sharing of junior economists and previous candidates in customs clearance

Lekao: 22 year first-class fire engineer "technical practice" knowledge points
![[ybtoj advanced training guidance] judgment overflow [error]](/img/be/bbe357ac2f2a8839afc5af47db88d0.jpg)
[ybtoj advanced training guidance] judgment overflow [error]

Adding database driver to sqoop of cdh6

The differences and relationships among port, targetport, nodeport and containerport in kubenetes

SparkContext: Error initializing SparkContext解决方法

Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer

Direct control PTZ PTZ PTZ PTZ camera debugging (c)

Rust search server, rust quick service finding tutorial

中国交通标志检测数据集
随机推荐
Performance tuning project case
[ybtoj advanced training guidance] judgment overflow [error]
bellman-ford AcWing 853. 有边数限制的最短路
Rust search server, rust quick service finding tutorial
std::vector批量导入快速去重方法
2.7 binary tree, post order traversal - [FBI tree]
Sse/avx instruction set and API of SIMD
Redis introduction, scenario and data type
Shutter encapsulated button
趣味 面试题
线性DP AcWing 897. 最长公共子序列
染色法判定二分图 AcWing 860. 染色法判定二分图
SparkContext: Error initializing SparkContext解决方法
区间DP AcWing 282. 石子合并
浏览器node事件循环
[ybtoj advanced training guide] similar string [string] [simulation]
线性DP AcWing 898. 数字三角形
8 examples of using date commands
What data types does redis have and their application scenarios
Win10 system OmniPeek wireless packet capturing network card driver failed to install due to digital signature problem solution