Home
Java
javaTutorial
Java example of obtaining the parameter type of a reflection method based on the method name



Java example of obtaining the parameter type of a reflection method based on the method name
java reflection
/** * 根据方法名称取得反射方法的参数类型(没有考虑同名重载方法使用时注意) * @param obj 类实例 * @param methodName 方法名 * @return * @throws ClassNotFoundException */ public static Class[] getMethodParamTypes(Object classInstance, String methodName) throws ClassNotFoundException{ Class[] paramTypes = null; Method[] methods = classInstance.getClass().getMethods();//全部方法 for (int i = 0; i< methods.length; i++) { if(methodName.equals(methods[i].getName())){//和传入方法名匹配 Class[] params = methods[i].getParameterTypes(); paramTypes = new Class[ params.length] ; for (int j = 0; j < params.length; j++) { paramTypes[j] = Class.forName(params[j].getName()); } break; } } return paramTypes; } //取得方法测试(Test类大家还是任意写吧,这里不列举了) Method m = Test.class.newInstance().getClass().getDeclaredMethod("方法名称", getMethodParamTypes(Test.class.newInstance(),"方法名称"));
Copy after login
For more java examples of obtaining the parameter type of a reflection method based on the method name, please pay attention to the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Saving in R.E.P.O. Explained (And Save Files)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1385
52

