public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException { //
Requirement: Execute all methods starting with test in the class
Scanner in = new Scanner(System.in); String className = in.nextLine(); //动态加载类 Class> clas = Class.forName(className); Method[] methods = clas.getMethods();//继承 公共方法 Method[] declaredMethods = clas.getDeclaredMethods();//非继承 所有方法 //创建一个不可重复集合set Set<method> set = new HashSet<method>(); for (Method method : methods) { set.add(method); } for (Method method : declaredMethods) { set.add(method); } //创建一个list集合存储以该类中所有以test开头的方法 List<method> list = new ArrayList<method>(); for (Method method : set) { //System.out.println(method); //打桩 if (method.getName().startsWith("test")) { list.add(method); } } for (Method method : list) { //System.out.println(method); //invoke是方法执行完毕返回的对象 Object invoke = method.invoke(clas.newInstance(), null); System.out.println("执行完毕"); } }</method></method></method></method>
The above is the detailed content of Example code about Java reflection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!