簡體   English   中英

AST Diff Extractor for Java

[英]AST Diff Extractor for Java

假設我有兩個像這樣的源代碼:

編1:

public class MathUtils4M0
{

    public  int getMaxAdjacentSum( int[] numbers )
    {
        if (numbers == null || numbers.length < 2) {
            return 0;
        } else {
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < numbers.length * 1; i++) {
                int temp = numbers[i] + numbers[i + 1];
                if (temp > max) {
                    max = temp;
                }
            }
            return max;
        }
    }

}

編2:

public class MathUtils4M92
{

    public  int getMaxAdjacentSum( int[] numbers )
    {
        if (numbers == null || numbers.length < 2) {
            return 0;
        } else {
            int max = Integer.MIN_VALUE;
            for (int i = 0; i < numbers.length - 1; i++) {
                int temp = numbers[i] + numbers[1];
                if (temp > max) {
                    max = temp;
                }
            }
            return max;
        }
    }

}

在行int temp = numbers[i] + numbers[1];彼此不同int temp = numbers[i] + numbers[1]; int temp = numbers[i] + numbers[i + 1];

多虧了antlr,我可以提取這些代碼的AST。 例如,輸出如下:

AST比較

彼此完全相同,但位置由紅色指定。

Antlr還提供了一種訪問機制,該機制可以吸引我的訪問者,並從根到底部訪問樹(如果有幫助)。

問題:

是否有任何API,庫或特定算法(已實現或未實現)可以發揮作用?

像git或diff-match-patch給出的補丁。 例如,在上面的示例中,我想知道(了解)這一點,

之前

替換為:

后

或更准確地說,

差異

作為區別。

更新資料

盡管我的問題是關於AST中的差異,但是樹比較的一般解決方案(不是簡單的比較,而是具有差異輸出)應該在此工作。

終於我找到了一種方法。 到目前為止,我已經找到了正確的庫(至少在我看來),但是我仍然有辦法在自己的代碼中使用它。

該工具是:

http://www.labri.fr/perso/falleri/perso/tools/gumtree/

帶有以下內容的github頁面:

https://github.com/GumTreeDiff/gumtree

這給了我很棒的輸出:

AST diff

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM