利用java的反射機製得到類的資訊

來源:互聯網
上載者:User

在java中與c#中都提供了反射機制,利用這種反射機制可以做很多有用的事情,比如實現AOP等功能,最近看了一篇關於實現AOP的內部機理的Blog,所以,開始看了一些反射的文章,這裡我把我寫的一些代碼拿出來,供大家分享,不久還會提供C#的利用反射機製得到類的資訊的代碼。

程式啟動並執行命令如下:(我把我的JAVA程式放到了c:/java下面了)

運行介面的部分如下:

 

詳細的代碼如下:

 

import java.lang.reflect.*;
import java.util.regex.*;
public class ClassInfo 
    {
        static void printinfor(Class cc)
        {
            System.out.println("類名->"+cc.getName());
            
        }
        private static Pattern p=Pattern.compile("/w+/.");
        public static void main(String args[])
        {
            System.out.println("************************************************");
            System.out.println("******************類的詳細資料提取**************");
            System.out.println("   使用方法:java ClassInfo  類名(類的全名)");
            System.out.println("   例如:java ClassInfo String");            
            System.out.println("   目前支援類的簡寫的支援");            
            System.out.println("************************************************");

            if(args.length<1)
            {
                System.out.println("用法錯誤");
                System.out.println("Example:java ClassInfo  String");
                System.exit(0);
            }
            Class c=null;
            try
            {
                c=Class.forName("java.lang."+args[0]);
                Method[] m=c.getMethods();
                Constructor[] ctor=c.getConstructors();
                
                if(args.length==1)
                {
            printinfor(c);
            Class []interarray=c.getInterfaces();
            for(int i=0;i<interarray.length;i++)
            System.out.println(c.getName()+"第"+(i+1)+"個"+"介面  "+interarray[i].getName());
            
            Class cy=c;
            int temp=1;        
            do
            {
                cy=cy.getSuperclass();
                System.out.print("第"+temp+"級父類  ");
                printinfor(cy);
                temp++;                
            }
            while(cy.getName()!="java.lang.Object");
                for(int i=0;i<m.length;i++)
                System.out.println("第"+(i+1)+"個方法"+p.matcher(m[i].toString()).replaceAll(""));
                for(int i=0;i<ctor.length;i++)
                System.out.println("第"+(i+1)+"建構函式"+p.matcher(ctor[i].toString()).replaceAll(""));
                }
                else
                {
                    System.out.println("該方法只能帶一個參數");
                }
            }
            catch(ClassNotFoundException e)
            {
                System.out.println("沒有找到給定類的資訊");
                System.exit(0);
            }            
        }
    }

注意其中的

c=Class.forName("java.lang."+args[0]);
    Method[] m=c.getMethods();
    Constructor[] ctor=c.getConstructors();

這個是反射代碼的核心。

理解反射代碼的核心在於,在java程式啟動並執行時候,關於類的中繼資料都存放在一個特殊的類裡面,這個類在java裡被稱為“Class”,注意,這裡的Class的C是大寫,與平時的class可是不一樣的。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.