简体   繁体   English

我可以获取一个类的所有方法吗?

[英]Can I get all methods of a class?

假设我有一个 .class 文件,我可以获得该类中包含的所有方法吗?

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.直接来自来源:http ://java.sun.com/developer/technicalArticles/ALT/Reflection/然后我将其修改为自包含的,不需要命令行中的任何内容。 ;-) ;-)

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

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

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

您可以使用反射 API

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 反射:如何获取 java 类的所有 getter 方法并调用它们 - Java Reflection: How can I get the all getter methods of a java class and invoke them 如何使用特定的Annotation获取所有方法的javadoc,方法名称和包/类名? - How can I get the javadoc of all methods with a specific Annotation, with the method name and package/class name? 如何获取Java类的所有公共静态方法? - How do I get all public static methods of a class in Java? 如何在一个类中循环所有方法和对象? (Java)的 - How can I loop all methods and objects in a class? (java) 我如何运行外部类的所有方法 - How can I run all methods of an external class 如何在事件调度线程上运行一个类的所有方法 - How can I run all methods of a class on Event Dispatch Thread 如何避免激活类中的所有方法? - How can I avoid activating all the methods on a class? 如何获得接口必须实现的所有方法? - How can I get all the methods which have to implement of an interface? 我可以在同一类的两个方法中使用@GET批注吗? - Can I use @GET annotations in two methods in same class? 如何在类或包中的所有类或JDB中的正则表达式上的所有方法上设置断点? - How can I set breakpoints on all methods in class or in all class in package or by regex in JDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM