Can I get all methods of a class?

JavaClassMethods

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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);
        }
    }
}

Solution 2 - Java

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

Solution 3 - Java

public static Method[] getAccessibleMethods(Class clazz) {
    List<Method> result = new ArrayList<Method>();
    while (clazz != null) {
        for (Method method : clazz.getDeclaredMethods()) {
            int modifiers = method.getModifiers();
            if (Modifier.isPublic(modifiers) || Modifier.isProtected(modifiers)) {
                result.add(method);
            }
        }
        clazz = clazz.getSuperclass();
    }
    return result.toArray(new Method[result.size()]);
}

Solution 4 - Java

You can use the Reflection API

Solution 5 - Java

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();
	}
}

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEng.FouadView Question on Stackoverflow
Solution 1 - JavacorsiKaView Answer on Stackoverflow
Solution 2 - Javauser2753164View Answer on Stackoverflow
Solution 3 - JavaShridutt KothariView Answer on Stackoverflow
Solution 4 - JavalinepoglView Answer on Stackoverflow
Solution 5 - JavaM BollojulaView Answer on Stackoverflow