57

Suppose that I have a .class file, can I get all the methods included in that class ?

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417

3 Answers3

70

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);
        }
    }
}
iono
  • 2,575
  • 1
  • 28
  • 36
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • So, I replace 'Class' with myClass name? – Eng.Fouad Mar 10 '11 at 22:14
  • You would use it from the command line `java DumpMethods com.mypackage.MyClass` – corsiKa Mar 10 '11 at 22:22
  • You also could replace `args[0]` with `"com.mypackage.MyClass"`. If it's using the default package, you can just use `"MyClass"`. – corsiKa Mar 10 '11 at 22:23
  • 1
    @Eng I edited it to be entirely self-contained. As you see, the class DumpMethods has 4 methods (foo, bar, baz, and main) and these are all represented in the output. For some fun, replace `"DumpMethods"` with `"java.util.List"`. :-) – corsiKa Mar 10 '11 at 22:27
  • What about inherited methods? See here in case it's a requirement: http://stackoverflow.com/a/31204747/363573 – Stephan Mar 06 '17 at 11:10
  • Those methods don't belong to the class, they belong to the parent. And if that's the requirement, then sure. You have to go up the chain. Tada! – corsiKa Mar 06 '17 at 15:04
42

To know about all methods use this statement in console:

javap -cp jar-file.jar packagename.classname

or

javap class-file.class packagename.classname

or for example:

javap java.lang.StringBuffer

Gerrit Griebel
  • 405
  • 3
  • 10
user2753164
  • 452
  • 5
  • 3
4
package tPoint;

import java.io.File;
import java.lang.reflect.Method;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class ReadClasses {

public static void main(String[] args) {

    try {
        Class c = Class.forName("tPoint" + ".Sample");
        Object obj = c.newInstance();
        Document doc = 
        DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new File("src/datasource.xml"));

        Method[] m = c.getDeclaredMethods();

        for (Method e : m) {
            String mName = e.getName();
            if (mName.startsWith("set")) {
                System.out.println(mName);
                e.invoke(obj, new 
          String(doc.getElementsByTagName(mName).item(0).getTextContent()));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}