Java by reflection, to get all the methods of an object (class method Extractor)

Source: Internet
Author: User
Tags class definition

Reference thinging in Java

When you are programming, you can save a lot of time by reflecting a gadget if you don't remember whether a class has a method, or if you don't know what a class can do, and you don't want to find the JDK document through the index or class hierarchy.

Browsing implements the source code of the class definition or its JDK document, and only finds methods that are defined or overridden in the class definition. However, for you, there are probably dozens of more useful ways to inherit from the base class. Finding these methods can be tedious and time-consuming. Fortunately, the reflection mechanism provides a way for us to write simple tools that can automatically present an interface.

Here's how it works:

 PackageCom.westward;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Method;ImportJava.util.regex.Pattern; Public classShowmethods {Private StaticString usage= "usage:\n" + "Showmethods qualified.class.name\n" + "to show all methods I N class or:\n "+" Showmethods qualified.class.name word\n "+" to search for methods involving ' word ' "; Private StaticPattern p= pattern.compile ("\\w+\\.");  Public Static voidMain (string[] args) {if(args.length<1) {System.out.println (usage); System.exit (0); }        intlines= 0; Try{Class<?> c= Class.forName (args[0]); Method[] Methods=C.getmethods (); Constructor[] Ctors=c.getconstructors (); if(args.length==1) {                 for(Method method:methods) {System.out.println (P.matcher (method.tostring ()). ReplaceAll (""));                System.out.println (method); }                 for(Constructor ctor:ctors) {System.out.println (P.matcher (ctor.tostring ()). ReplaceAll (""));                System.out.println (ctor); } lines= methods.length+ctors.length; }Else {                 for(Method method:methods) {if(Method.tostring (). IndexOf (args[1])! =-1) {System.out.println (P.matcher (method.tostring ()). ReplaceAll (""));                        System.out.println (method); Lines++; }                }                 for(Constructor ctor:ctors) {if(Ctor.tostring (). IndexOf (args[1])! =-1) {System.out.println (P.matcher (ctor.tostring ()). ReplaceAll (""));                        System.out.println (ctor); Lines++; }                }            }        } Catch(ClassNotFoundException e) {System.out.println ("No Such class:" +e); }    }}

Input:com.westward.ShowMethods

Output
public static void Main (string[])
public static void Com.westward.ShowMethods.main (java.lang.string[])
Public final void Wait (Long,int) throws Interruptedexception
Public final void Java.lang.Object.wait (Long,int) throws Java.lang.InterruptedException
Public final native void wait (long) throws Interruptedexception
Public final native void Java.lang.Object.wait (long) throws Java.lang.InterruptedException
Public final void Wait () throws Interruptedexception
Public final void Java.lang.Object.wait () throws Java.lang.InterruptedException
public boolean equals (Object)
public boolean java.lang.Object.equals (Java.lang.Object)
Public String toString ()
Public java.lang.String java.lang.Object.toString ()
public native int Hashcode ()
public native int Java.lang.Object.hashCode ()
Public final native Class GetClass ()
Public final native Java.lang.Class Java.lang.Object.getClass ()
Public final native void Notify ()
Public final native void Java.lang.Object.notify ()
Public final native void Notifyall ()
Public final native void Java.lang.Object.notifyAll ()
Public Showmethods ()
Public Com.westward.ShowMethods ()

Java by reflection, to get all the methods of an object (class method Extractor)

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.