Java SourceCode Parsing(Java原始碼分析器)

來源:互聯網
上載者:User

                                   Java源碼分析執行器(2009-5-1)(english version)
    一、功能及模組://抱歉,此項目雖然有意思,但未經整理,可讀性差,且處於停滯狀態。

    1. 識別java源碼的文法結構,產生結點類型為具體源碼成份的結點樹。
    2. 解析Java源碼並從指定的方法入口處開始運行,在啟動並執行過程中可以執行外部插入的代碼邏輯。類似於AOP,但所截獲的目標可以是方法也可以是任意細粒度的代碼。
    3. 解析Java源碼並從指定的方法入口處開始執行,在執行過程中對不合理的代碼進行預警。預警功能是我興趣所在,將得到大力加強。
    4. 靜態處理:
       ① 對源碼進行合法性檢查,以保證其符合具體項目中的要求。類似於Java代碼缺陷自動分析工具“Findbugs”.
       ② 執行某些複雜的重構任務,如將數組改為java.util.List,改變方法調用中的參數秩序,如將方法count(int a, String b)改寫為count(String b, int a)。

  二、主要概念:

    1. 文法樹: Java源碼的文法樹由若干小規模的文法樹合并而成。抽象文法樹在本質上是圖而非樹。
    2. 匹配樹:是一顆嚴格的樹。由匹配引擎參照文法樹對源碼代碼進行識別後產生。
    3. 匹配樹的JavaBean化: 匹配樹描述了源碼的靜態形式。其bean化過程即對其中不同的源碼成分產生相應的JvaBean並在Bean中實現針對性的運行邏輯,並在此基礎上實現對Java代碼的動態解釋執行。(請參見源碼包bugsfind.bean.*)
    4. 結點類型:典型的結點類型有IfElse, For, DoWhile, Mehtod, Block, Expression(運算式)等等。
    5. 有源Bean及無源類, 實際項目中,並非所有類都有源碼,需要解決有源系統同無源系統之間的互動問題。……對有源碼的類產生相應的有源Bean……在讀取源碼並步步執行的過程中,當遇見無源類(不存在源碼的類)時,需保證有源bean及無源類共用它們所共同操作的資料。
  有源Bean有對應的無來源物件,這使得無源類可以調用有源Bean。
  有源Bean可直接調用無源類, 因為有源Bean的調用細節是透明的,可以針對具體的計算細節採用具體的方法:
        ①運算子計算,如: a+=2, b!=c+d,...可通過字串分析來等價實現。 
        ②系統功能調用,如檔案讀寫,螢幕輸出, ...可通過反射機制來行等價實現。
        ③資料在不同的資料結構間的轉移。如List.toArray(),...可通過反射機制來行等價實現。

 

  三、當前開發狀態VS前景:

      此項目的結構、以及細節已基本成形, 有待於進行全面測試以及進一步在功能上進行擴充。
      請參見測試案例 demo.TestCase_all.java, demo.TestCase_typical.java.
      通過對以上測試案例及相關測試對象的改寫,有助於進一步瞭解此項目。
      反射機制的能力也是有限的,要保證大程式的正確運行,需要對原始碼做部分變化,但這樣的變化可以做到無損或者可逆。主要的變化如:對其它包下的非公有的內部類明確產生建構函式, 否則反射機制無法運行其建構函式。

      尚不能識別Enum(JDK5.0)以及個別的運算子(詳情可見bugsfinder.exp.OperatorsJ.java)。  

      本項目根據實際需要可以為以下任務提供有力的參考:

        ① javascript的源碼分析與JS編輯器。

        ② 各種指令碼(比如SQL)的分析、自動化構造、原始碼正常化檢測。

 

  四、測試案例: //java version: jdk1.5.0_04

     解壓本項目後,點擊項目根目錄下的批次檔startDemo.bat 可運行一個簡潔的測試案例。
     bugsfind.demo和bugsfind.test包下有更詳細的測試案例。——運行之前需匯入工程到eclipse中,然後修改設定檔bugsfind._config.config.properties,將sourceCodeLocation參數值設為當前工程的原始碼根目錄路徑。

  五、下載
            本java工程可從此處下載: http://code.google.com/p/java-parser-executor/downloads/list 
            作者: weilai2@163.com, 中國上海(浦東), 10:46 2009-5-1

 (關鍵字:java源碼分析器,java原始碼分析器,原始碼樹形化,java原始碼檢錯器, java原始碼解釋執行器,編譯技術)

 

                  Java source-code parser&&executor(2009-5-1)

  ***1. function and module:    //sorry, this project is interesting but source code is a mess
 1.1. recognize the structure of java source-code, produce a node-tree.
 1.2. parse the source, execute form specified method. custom execute logic can be inserted into this process. AOP can intercept a method, via this way, we can intercept any clause in a method, that is to say, more fine-grained.
 1.3  parse the source, execute form specified method. and try to find out illogical code. this finding-out is my interest, and hopes to be enhanced in the future.
 1.4  static process:
    1.4.1. check the source-code, assure it fits the rule of concrete project. just like "findBugs". (http://findbugs.sourceforge.net/).
    1.4.2. fulfill some complex refactor task. e.g. refactor array to java.util.List,  refactor the order of method parameter. e.g. change method count(int a, String b) to method count(String b, int a)。

  ***2. terms:
 2.1. grammar tree: grammar of java code is merged from some more smaller grammar. in essential, abstract grammar tree is a diagram, no a tree.
 2.2. match-tree: is a strict tree.  after parsing a certain java code-fragment, the  matching-engine will create a tree as a result.
 2.3. match-tree to javaBeans: match-tree is certain java code's static form. after javabean-lize, I add corresponding execution-logic to every grammar-element, based on this, we can run some java-code not via bytecode but via source-code. (please see source-package bugsfind.bean.* )
 2.4. type of node. typical node type is: IfElse, For, DoWhile, Method, Block, Expression,..etc.
 2.5. source bean and no-source class: in a practical project, some cod has source, some hasn't, we must face such problem: source-class interactive-with no-source-class. in some way, I realize it(not strong, but useful).

  ***3. current progress:
   structure and details has basically formed, it is to be fully-test and waiting for more extension in function.
    test case demo.TestCase_all.java, demo.TestCase_typical.java is useful to get more conception on this project.

  ***4. test case & running: //java version: jdk1.5.0_04
   click batch-file startDemo.bat, a simple test will be start.
   there are more test under source-package bugsfind.demo* and bugsfind.test.*. but before first running, should import this project into eclipse, then, config file "bugsfind._config.config.properties" should be modified: set sourceCodeLocation = "root directory path of source-code in current project".

  

  ***5. download:

  this Java project can be download from

            http://code.google.com/p/java-parser-executor/downloads/list 

  your opinion is welcome!

  author: weilai2@163.com,  Shanghai(Pudong), china.
  10:46 2009-5-1

 ~~~~~~~~~~~end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.