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)