Can I get all methods of a class?

后端 未结 5 1006
梦如初夏
梦如初夏 2020-11-29 05:12

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

相关标签:
5条回答
  • 2020-11-29 05:27
    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();
        }
    }
    
    }
    
    0 讨论(0)
  • 2020-11-29 05:46

    You can use the Reflection API

    0 讨论(0)
  • 2020-11-29 05:47
    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()]);
    }
    
    0 讨论(0)
  • 2020-11-29 05:49

    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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:50

    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

    0 讨论(0)
提交回复
热议问题