zoukankan      html  css  js  c++  java
  • java之通过反射,来获得某对象的所有方法(类方法提取器)

    参考Thinging in Java

    在编程时, 如果不记得一个类是否有某个方法,或者不知道一个类究竟能做些什么,而又不想通过索引或 类的层次结构去查找jdk文档,这时通过反射的小工具能节省很多时间。

    浏览实现了类定义的源代码或是其jdk文档,只能找到在 这个类定义中 被定义或被覆盖的方法。但,对你来说,可能有数十个更有用的方法都是继承自 基类的。要找出 这些方法,可能会很乏味而且费时间。幸运的是,反射机制 提供了一种方法,使我们能够编写可以自动展示接口的简单工具。

    下面就是其工作方式:

    package com.westward;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import java.util.regex.Pattern;
    
    public class ShowMethods {
    
        private static String usage= 
                "usage:
    " +
                "ShowMethods qualified.class.name
    "+ 
                "To show all methods in class or:
    "+
                "ShowMethods qualified.class.name word
    "+
                "To search for methods involving 'word'";
        private static Pattern p= Pattern.compile("\w+\.");
        public static void main(String[] args) {
            if (args.length<1) {
                System.out.println(usage);
                System.exit(0);
            }
            int lines= 0;
            try {
                Class<?> c= Class.forName(args[0]);
                Method[] methods= c.getMethods();
                Constructor[] ctors= c.getConstructors();
                if (args.length==1) {
                    for (Method method : methods) {
                        System.out.println(p.matcher(method.toString()).replaceAll(""));
                        System.out.println(method);
                    }
                    for (Constructor ctor : ctors) {
                        System.out.println(p.matcher(ctor.toString()).replaceAll(""));
                        System.out.println(ctor);
                    }
                    lines= methods.length+ ctors.length;
                }else {
                    for (Method method : methods) {
                        if (method.toString().indexOf(args[1]) != -1) {
                            System.out.println(p.matcher(method.toString()).replaceAll(""));
                            System.out.println(method);
                            lines++;
                        }
                    }
                    for (Constructor ctor : ctors) {
                        if (ctor.toString().indexOf(args[1]) != -1) {
                            System.out.println(p.matcher(ctor.toString()).replaceAll(""));
                            System.out.println(ctor);
                            lines++;
                        }
                    }
                }
            } catch (ClassNotFoundException e) {
                System.out.println("No such class: " + e);
            }
        }
    
    }

    input:com.westward.ShowMethods

    output:
    public static void main(String[])
    public static void com.westward.ShowMethods.main(java.lang.String[])
    public final void wait(long,int) throws InterruptedException
    public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
    public final native void wait(long) throws InterruptedException
    public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
    public final void wait() throws InterruptedException
    public final void java.lang.Object.wait() throws java.lang.InterruptedException
    public boolean equals(Object)
    public boolean java.lang.Object.equals(java.lang.Object)
    public String toString()
    public java.lang.String java.lang.Object.toString()
    public native int hashCode()
    public native int java.lang.Object.hashCode()
    public final native Class getClass()
    public final native java.lang.Class java.lang.Object.getClass()
    public final native void notify()
    public final native void java.lang.Object.notify()
    public final native void notifyAll()
    public final native void java.lang.Object.notifyAll()
    public ShowMethods()
    public com.westward.ShowMethods()

  • 相关阅读:
    [Clr via C#读书笔记]Cp4类型基础
    [Clr via C#读书笔记]Cp3共享程序集和强命名程
    [Clr via C#读书笔记]Cp2生成打包部署和管理应用程序和类型
    [Clr via C#读书笔记]Cp1CLR执行模型
    试用Markdown来写东西
    字符编码的总结
    常去的网站
    Click Once使用总结
    【LevelDB源码阅读】Slice
    【程序员面试金典】面试题 01.05. 一次编辑
  • 原文地址:https://www.cnblogs.com/westward/p/5426048.html
Copyright © 2011-2022 走看看