使用 EMF 模型比较两个源代码文件
在我的应用程序中,我应该比较两个源代码文件以查看是否发生了更改,然后突出显示这些更改。为此我想到使用 EMF 比较。我的应用程序是一个独立的应用程序,不用作插件或类似的东西。它应该在没有 Eclipse 的情况下运行。因此我链接了所有必要的库并尝试使用 EMF 比较。
现在的问题是我不知道如何构建两个模型来相互比较两个源代码文件。在我作为第一种方法编写的以下代码片段中,源代码文件作为文件(Test1.java 和 Test2.java)传递,但实际上这两个文件的源代码都存储在方法参数指示的字符串中。
所以我的问题基本上是如何基于包含 Java 源代码的 String 生成两个模型,以便我可以使用这两个模型进行相互比较?
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));
}
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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,您可以使用 Modisco 项目 中的 Java 元模型。
使用它您可以将java文件反序列化为EMF模型然后进行比较。
EMFText 项目 也有 Java 模型实现。
试试吧!
You can use Java metamodel from Modisco project, I think.
With it you can deserialize java files into EMF model and then compare.
EMFText project also has Java model implementation.
Give 'em a try!
我认为您在这里使用了错误的技术。 AFAIK,EMF 不支持可用于解析 Java 源代码并将解析树构建为 EMF 模型的解析器生成器。
IMO,更好的想法是使用现有的 Java 解析器生成器(ANTLR、JavaCC 等)和现有的 Java 语法之一,然后根据生成的解析器生成的解析树实现比较。
I think you are using the wrong technology here. AFAIK, EMF doesn't support a parser generator that you could use to parse Java source code and build parse trees as EMF models.
IMO, a better idea would be to use one of the existing Java parser generators (ANTLR, JavaCC, etc) and an existing Java grammar, then implement your comparison based on the parse trees that the generated parser produces.