Use Javassist to obtain the method parameter name, parameter type, and method access type in the Java class.

Source: Internet
Author: User
Http://www.csg.is.titech.ac.jp /~ CHBA/Javassist/
Package COM. vl; import Java. lang. reflect. method; import into sist. classpool; import policsist. ctclass; import into sist. ctmethod; import ipvsist. modifier; import ipvsist. notfoundexception; import ipvsist. bytecode. accessflag; import ipvsist. bytecode. codeattribute; import ipvsist. bytecode. localvariableattribute; import parameter sist. bytecode. methodinfo; import COM. vl. app. PTZ; import COM. vl. app. streamsutil; import COM. vl. SQL. userdao; Public   Class Vlinterface { Public   Static   Void Main (string [] ARGs) throws notfoundexception {classpool pool = classpool. getdefault (); Class [] cs = New Class [] {PTZ. Class , Userdao. Class , Streamsutil. Class }; String [] prefixstrings = New String [] {" PTZ "," User "," Streamsutil "}; For ( Int I = 0; I <CS. length; I ++) {ctclass; Class clz = cs [I]; ctclass = pool. Get (Clz. getname (); method [] Methods = clz. getdeclaredmethods ();For (Method M: Methods) {string name = m. getname (); ctmethod CM = ctclass. getdeclaredmethod (name); methodinfo = cm. getmethodinfo (); Class [] parametertypes = m. getparametertypes (); codeattribute = methodinfo. getcodeattribute (); localvariableattribute ATTR = (localvariableattribute) codeattribute. getattribute (localvariableattribute. tag ); If (ATTR = Null ){ // Exception } If (CM. getmethodinfo (). getaccessflags ()! = Accessflag. Public) Continue ; // System. Out. println (); String [] paramnames =New String [cm. getparametertypes (). Length]; Int Pos = modifier. isstatic (CM. getmodifiers ())? 0: 1; For ( Int J = 0; j <paramnames. length; j ++) paramnames [J] = ATTR. variablename (J + POS ); // Paramnames is the parameter name System. Out . Printf (" \ "% S. % s \": \ r \ n \ treturn: % s \ t (Parameters ", Prefixstrings [I], name, M. getreturntype (). getsimplename ()); For ( Int I1 = 0; I1 <paramnames. length; I1 ++) {system.Out . Printf (" % D: [% S % s] ", I1 + 1, parametertypes [I1]. getsimplename (), paramnames [I1]);} // For (class class1: parametertypes ){  // System. Out. printf ("[% s],", class1.getname ());  //} System. Out . Println (" ) \ R \ n ");}}}}
Related Article

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.