Can I get all methods of a class?

后端 未结 5 1035
梦如初夏
梦如初夏 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();
        }
    }
    
    }
    

提交回复
热议问题