我正在开发一个 Eclipse 插件,它对文件的两个版本的差异运行代码违规检查器。现在我正在使用diff.exe来获取两个文件之间的差异。但是由于 diff.exe 是一个外部应用程序,我意识到最好使用 eclipse 内置的比较工具来获取文件差异。所以我使用了org.eclipse.compare并达到了这一点:
public static List<Patch> compare(String old, String recent) {
try{
IRangeComparator left = new TokenComparator(old); //what exactly to be passed in this constructor, a file path, a literal value or something else?
IRangeComparator right = new TokenComparator(recent);
RangeDifference[] diffs = RangeDifferencer.findDifferences(left, right); // This line is throwing NPE
//..
// Process RangeDifferences into Collection of Patch collection
//..
}catch(Exception e){}
//Returns a collection of file differences.
return null;
}
现在的问题是我不确定要在构造函数TokenComparator(String)中传递什么。文档说这个 构造函数为给定的字符串创建一个 TokenComparator。但是并没有写出到底要在这个构造函数中传递什么,文件路径,文字值或其他什么?当我传递文件路径或字符串文字时,我在查找差异的下一行得到 NullPointerException。
java.lang.NullPointerException
at org.eclipse.compare.internal.core.LCS.isCappingDisabled(LCS.java:98)
at org.eclipse.compare.internal.core.LCS.longestCommonSubsequence(LCS.java:55)
at org.eclipse.compare.rangedifferencer.RangeComparatorLCS.longestCommonSubsequence(RangeComparatorLCS.java:186)
at org.eclipse.compare.rangedifferencer.RangeComparatorLCS.findDifferences(RangeComparatorLCS.java:31)
at org.eclipse.compare.rangedifferencer.RangeDifferencer.findDifferences(RangeDifferencer.java:98)
at org.eclipse.compare.rangedifferencer.RangeDifferencer.findDifferences(RangeDifferencer.java:82)
at org.eclipse.compare.rangedifferencer.RangeDifferencer.findDifferences(RangeDifferencer.java:67)
at com.dassault_systemes.eclipseplugin.codemonview.util.CodeMonDiff.compare(CodeMonDiff.java:48)
at com.dassault_systemes.eclipseplugin.codemonview.util.CodeMonDiff.main(CodeMonDiff.java:56)
有人请告诉什么是正确的方法。