Suppose that I have a .class file, can I get all the methods included in that class ?
Method 1 – Using the dir() function to list methods in a class. To list the methods for this class, one approach is to use the dir() function in Python. The dir() function will return all functions and properties of the class. Let's see what happens if we try it for MyClass .
You can use the built in dir() function to get a list of all the attributes a module has. Try this at the command line to see how it works. Also, you can use the hasattr(module_name, "attr_name") function to find out if a module has a specific attribute. See the Guide to Python introspection for more information.
There are basically three types of methods in Python: Instance Method. Class Method. Static Method.
Inside a Class, we can define the following three types of methods.
Straight from the source: http://java.sun.com/developer/technicalArticles/ALT/Reflection/ Then I modified it to be self contained, not requiring anything from the command line. ;-)
import java.lang.reflect.*; /** Compile with this: C:\Documents and Settings\glow\My Documents\j>javac DumpMethods.java Run like this, and results follow C:\Documents and Settings\glow\My Documents\j>java DumpMethods public void DumpMethods.foo() public int DumpMethods.bar() public java.lang.String DumpMethods.baz() public static void DumpMethods.main(java.lang.String[]) */ public class DumpMethods { public void foo() { } public int bar() { return 12; } public String baz() { return ""; } public static void main(String args[]) { try { Class thisClass = DumpMethods.class; Method[] methods = thisClass.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { System.out.println(methods[i].toString()); } } catch (Throwable e) { System.err.println(e); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With