Javassist: Enhanced Java Reflection tool to get method parameter names

Source: Internet
Author: User

Java reflection is not able to get the parameter name of the method. Like what:

[Java] view Plaincopyprint?

    1. Public String concatstring (String str1,string str2) {

    2. return str1+str2;

    3. }

public string concatstring (string str1,string str2) {return str1+str2; }

to get the parameter name "STR1", and "str1", it is not possible to use the reflection from the JDK. But we use third-party packagesJavaassistcan be obtained.

[Java] view Plaincopyprint?

  1. Public Static void Main (string[] args) {

  2. Class clazz = testjavaassist. class;

  3. Try {

  4. Classpool pool = Classpool.getdefault ();

  5. Ctclass cc = Pool.get (Clazz.getname ());

  6. Ctmethod cm = Cc.getdeclaredmethod ("concatstring");

  7. //Use Javaassist's reflection method to get the parameter name of the method

  8. MethodInfo MethodInfo = Cm.getmethodinfo ();

  9. CodeAttribute CodeAttribute = Methodinfo.getcodeattribute ();

  10. Localvariableattribute attr = (localvariableattribute) codeattribute.getattribute (Localvariableattribute.tag);

  11. if (attr = = null) {

  12. //Exception

  13. }

  14. string[] Paramnames = new string[cm.getparametertypes (). length];

  15. int pos = modifier.isstatic (Cm.getmodifiers ())? 0 : 1;

  16. for (int i = 0; i < paramnames.length; i++)

  17. Paramnames[i] = attr.variablename (i + POS);

  18. //Paramnames is the name of the parameter

  19. for (int i = 0; i < paramnames.length; i++) {

  20. System.out.println (Paramnames[i]);

  21. }

  22. } catch (Notfoundexception e) {

  23. E.printstacktrace ();

  24. }

  25. }

Public static void main (String[] args)  {Class clazz =  Testjavaassist.class;try {classpool pool = classpool.getdefault (); Ctclass cc = pool.get (Clazz.getname ()); Ctmethod cm = cc.getdeclaredmethod ("concatstring");//  gets the parameter name of the method using the Javaassist reflection method MethodInfo  methodinfo = cm.getmethodinfo (); Codeattribute codeattribute = methodinfo.getcodeattribute (); localvariableattribute attr =  (Localvariableattribute)  codeattribute.getattribute ( Localvariableattribute.tag);if  (attr == null)  {// exception}string[] paramnames  = new string[cm.getparametertypes (). Length];int pos = modifier.isstatic ( Cm.getmodifiers ())  ? 0 : 1;for  (int i = 0; i <  paramnames.length; i++) Paramnames[i] = attr.variablename (i + pos);//  Paramnames is the parameter name for  (int i = 0; i < paramnames.length; i++)  {System.out.println ( Paramnames[i]);}}  catch  (notfoundexception e)  {e.printstacktrace ();}}

Javassist: Enhanced Java Reflection tool to get method parameter names

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.