Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » MoDisco » Compare two source code files using EMF
Compare two source code files using EMF [message #764597] Mon, 12 December 2011 13:59 Go to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 6
Registered: December 2011
Junior Member
I´m not sure if this is the right place to ask this, but I couldn´t find any better place.

In my application I should compare two source code files to see if something has changed and then highlight those changes. For that i thought of using EMF compare. My application is a standalone application and is not used as a plugin or something similar. It should run without eclipse. Therefore I linked all the necessary libraries and tried to use EMF compare.

The problem now is that I don´t know how to build the two models that I have to use to compare the two source code files against each other. In the following code snippet I wrote as a first approach, the source code files are passed as files (Test1.java and Test2.java) but actually the source code of both files are stored in a string as the method parameters indicate.

So my question is basically how can I generate two models based on String that contain Java source code so that I can use these two models to compare against each other?

	public void compare(String source1, String source2) throws IOException, InterruptedException {
		
		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("java", new ResourceFactoryImpl());
		
		XSDEcoreBuilder builder = new XSDEcoreBuilder();
		
		Collection<EObject> model1 = builder.generate(URI.createFileURI("Test1.java"));
		Collection<EObject> model2 = builder.generate(URI.createFileURI("Test2.java"));
		
		final MatchModel match = MatchService.doMatch(model1.iterator().next(), model2.iterator().next(), Collections.<String, Object> emptyMap());
		final DiffModel diff = DiffService.doDiff(match, false);
		
		final List<DiffElement> differences = new ArrayList<DiffElement>(diff.getOwnedElements());
		
		System.out.println("MatchModel :\n");
		 	System.out.println(ModelUtils.serialize(match));
		 	System.out.println("DiffModel :\n");
		 	System.out.println(ModelUtils.serialize(diff));
	}
Re: Compare two source code files using EMF [message #764606 is a reply to message #764597] Mon, 12 December 2011 14:16 Go to previous messageGo to next message
Nicolas Bros is currently offline Nicolas BrosFriend
Messages: 49
Registered: July 2009
Member
Hi,

If you want to compare files at the text level, you could try to see if org.eclipse.compare meets your requirements:

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcompare%2Fpackage-summary.html

I you want to use EMF Compare to compare Java sources at a more semantic level, then you could use the MoDisco Java discoverer to create a model from your Java source files:
http://download.eclipse.org/modeling/mdt/modisco/doc/org.eclipse.modisco.java.doc/mediawiki/java_discoverer/plugin_dev.html

You won't be able to discoverer a String directly using the MoDisco Java discoverer, though. You will have to create a (temporary) JDT project containing your source files.
Re: Compare two source code files using EMF [message #765042 is a reply to message #764606] Tue, 13 December 2011 10:07 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 6
Registered: December 2011
Junior Member
Thanks for your help. I'm not sure what you mean with text level. I want to to it in the same way as various diff tools in repositories like svn, git, etc. do it. I already tried the compare package, but I couldn't find a documentation or tutorial or anything like that, that shows how to use it in a standalone application.

The MoDisco Java discoverer sounds really great and the tool I'm looking for, but, again I can't find any explanation how to create these temporary projects based on some code strings.
Re: Compare two source code files using EMF [message #765053 is a reply to message #765042] Tue, 13 December 2011 10:29 Go to previous message
Nicolas Bros is currently offline Nicolas BrosFriend
Messages: 49
Registered: July 2009
Member
Quote:
I'm not sure what you mean with text level

I mean that it would compare the two sources character by character, without trying to "understand" the differences at a higher level of abstraction (i.e. whether the difference corresponds to a method, field, etc.)

Quote:
but, again I can't find any explanation how to create these temporary projects based on some code strings.

Well, usually Java source files in Eclipse are contained in Java projects, managed by the JDT. Since the MoDisco Java discoverer is based on the JDT, it needs to have a Java project to work with.
So, if your sources are already in a Java project in your workspace, then you can simply launch the MoDisco java discoverer on it.
It not, then you will need to create a Java project programmatically. Maybe the easiest way would be to first create a Java project manually, and make it a template that your code would copy into the workspace when needed. Or, you can use the JDT APIs to configure a JDT project.

For example, see the method "createPluginProject" in this MoDisco utility class, that creates projects with the JDT and PDE natures:
https://dev.eclipse.org/svnroot/modeling/org.eclipse.mdt.modisco/plugins/trunk/org.eclipse.gmt.modisco.infra.common.core/src/org/eclipse/gmt/modisco/infra/common/core/internal/utils/ProjectUtils.java
Previous Topic:Languages Supported by MoDisco
Next Topic:Auto-Reverse Engineering
Goto Forum:
  


Current Time: Wed Apr 24 17:09:47 GMT 2024

Powered by FUDForum. Page generated in 0.04188 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top