[英]Is it possible to compare two .java files methods and fields in all cases?
我目前正在上一個項目管理班,教授給了這個作業,以編程方式比較了所有情況下的兩種.java文件方法和字段。 我認為實際上是不可能的,但也許我錯了!
分配規范如下(我知道它非常模棱兩可)
In this assignment, you are required to write a comparison tool for two
versions of a Java source file.
Your program takes as input two .java files representing those two versions
and reports the following atomic changes:
1. AM: Add a new method
2. DM: Delete a method
3. CM: Change the body of a method (note: you need to handle the case where a method is
relocated within the body of its class)
4. AF: Add a field
5. DF: Delete a field
6. CFI: Change the definition of an instance field initializer (including (i) adding an initialization to a
field, (ii) deleting an initialization of a field, (iii) making changes to the initialized value of a field,
and (iv) making changes to a field modifier, e.g., private to public)
這就是我正在使用的方法,我的方法是使用反射,因為它可以執行所有操作,但可以檢測方法主體中的差異。
我曾考慮過可以創建一個解析器的想法,但這似乎很荒謬,尤其是對於項目管理中3個學分的本科生類。 諸如BeyondCompare之類的工具不會列出更改的方法或字段,僅列出不同的行,因此不符合要求。
我上交了這個作業,幾乎整個類都因為它的原因而失敗了,原因是“我們的代碼不適用於沒有給出外部依存關系的Java文件或不同項目中的Java文件”-完全正確,但是我我在想,不可能做。
我試圖確定一個具體的答案,說明為什么實際上不可能做到這一點,或者要學到一些新的知識來了解為什么這樣做是可能的,所以任何見解都會很棒。
您在這里出錯的是,您已經開始檢查.class文件(使用反射)。 上面列出的某些信息在該階段甚至不可用(泛型,內聯函數)。 您需要做的是將.java文件解析為文本。 這是真正解決問題的唯一方法。 一個非常高級的解決方案可能是編寫一個程序,該程序:
注意:如果這是一項任務,則不應使用任何人提供的解決方案,而需要自己完成。 如果使用其他人編寫的庫,則可能不會獲得任何分數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.