0

I am developing an eclipse plugin that runs code violation checker on the difference of two versions of a file. Right now I am using diff.exe to get the difference between the two files. But as diff.exe is an extrenal app, I realized that its better to use eclipse built-in compare tool to get the file difference. So I used org.eclipse.compare and reached up to this point:

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;
}

Now the problem is I am not sure what exactly to be passed in the constructor TokenComparator(String). The document says this constructor Creates a TokenComparator for the given string. But it is not written what exactly to be passed in this constructor, a file path, a literal value or something else? When I'm passing a file path or a string literal I am getting NullPointerException on the next line of finding differences.

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)

Someone please tell what is right way to proceed.

2 Answers 2

0

If the question is What value the token comparators constructor takes then the answer is it takes the input string to compare. Specified in javadoc here http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcompare%2Fcontentmergeviewer%2FTokenComparator.html

TokenComparator(String text) Creates a TokenComparator for the given string.

And the null pointer yo are getting is because in function isCappingDisabled it tries to open the compare plugin which seems to be null. You seem to be missing a direct dependency to the plugin "org.eclipse.compare.core"

3
  • I added the org.eclipse.compare.core(3.5.200) in the dependencies tab of my plugin's Manifest.MF but I'm still facing the same NPE issue. Have I missed something?
    – Peeyush
    Jan 6, 2013 at 8:41
  • Are you running this as an eclipse RCP/plugin?
    – codejammer
    Jan 9, 2013 at 5:55
  • Please see comments on Kellindil's answer.
    – Peeyush
    Jan 9, 2013 at 15:17
0

The org.eclipse.compare plugin was never meant to be used in standalone : many of its functionalities require a running instance of Eclipse. Furthermore, it mixes core and UI code within the same plugin, which will lead to unexpected behavior if you are not very careful about what you use and what dependencies are actually available in your environment.

You mentionned that you were developping an Eclipse plugin. However, the NPE you get indicates that you are not running your code as an Eclipse plugin, but rather as a standard Java program. In an Eclipse environment, ComparePlugin.getDefault() cannot return null : the plugin needs to be started for that call to return anything but null.... and the mere loading of the ComparePlugin class within Eclipse is enough to start it.

The answer will be a choice :

  1. You need your code to run as a standalone Java program out of Eclipse. In such an event, you cannot use org.eclipse.compare and diff.exe is probably your best choice (or you could switch to an implementation of diff that was implemented in Java in order to be independent of the platform).
  2. You do not need your program to work in a standalone environment, only as an Eclipse plugin. In this case, you can keep the code you're using. However, when you run your code, you have to launch it as a new "Eclipse application" instead of "Java Application". You might want to look at a tutorial on how to develop Eclipse plugins for this, This simple tutorial from Lars Vogel shows how to run a new Eclipse Application to test an Hello World plugin. You will need a similar code, with a menu entry to launch your plugin somewhere (right-click on a file then select "check violations" in your case?).
2
  • Kellindil Thanks for solving this, I deliberately ran the comparison code in standalone just to see the behavior quickly. But I forgot that plugin has to be executed in eclipse environment. Silly me!! When I ran the plugin as Eclipse application the comparison started working. Now as the problem is fixed I could realize that RangeDifferencer.findDifferences is not the correct way to compare 2 files (Please correct me if I am wrong). Actually I am looking for a simple method that accepts two versions of the same file and gives the file difference(something like Patch object) as output.
    – Peeyush
    Jan 9, 2013 at 15:13
  • @Peeyush I depend on the APIs of org.eclipse.compare for my own project... but I am not comparing "text" files myself, so never tried to create a patch with these APIs. The "patch" files are different depending on the repository you compare with (CVS, Git...) and the repositories usually provide their own create patch actions. If all you want is a regular "diff" format patch between two local files, the package org.eclipse.compare.patch (notably PatchBuilder) may be what you need? You should change the question to "is there a 'diff' functionality in Eclipse" though :).
    – Kellindil
    Jan 9, 2013 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.