The “Copy Facts To Clipboard” dialog is a tool that makes it very easy to create
After selecting some Java source code1) you can open the “Copy Facts To Clipboard” dialog …
The “Copy To Clipboard” dialog is divided into three parts:
To add the source code project for this example do the follwing:
The deadline for the release was only a couple of hours away and there was still this small detail that wasn't implemented yet. You knew that the required functionality already existed in an internal class but there was no time to refactor the code to move it where you needed it now. So you quickly copied the method, only changing the method name and some variable names, to suit its new context. Knowing that code copying is bad you honestly intended to refactor immediately after the successful release.
However, after the release everybody took one month off. Coming back from the vacation you remembered your little sin and decided to refactor the cloned code before anybody noticed. Only the vacation was so relaxing you forgot from which part of the internal code you copied! You tried the Eclipse search options but because of the renaming of the copied code they couldn't find anything. So what can you do?
With the help of JTransformer's “Copy Fact To Clipboard” Dialog detecting structurally identical or similar code is a trivial task:
predicate head that is parameterized by the ID of the method you want to find.
This is how the predicate should look now:5)
Your structural clone detection predicate is ready!
All you need to do now is “consult” the “find_copy.pl” file and bring the Prolog Console in focus. This can be done through the Prolog Menu or by hiting F9. Now you can simply call your predicate! Your results should be similar to:
Now select the returned MethodID choose “Open in Editor” from the context menu. And there it is! The method “hello()” in “AlexisClass” from which you copied in the first place. Now you can refactor the code and, due to JTransformer nobody will ever know!
With the above steps you have created a customized detection predicate for a particular “level 2 code clone” (that is, a clone that is identical up to renamings). All you needed to do yourself was to generalise the automatically generated condition by removing a few of the (too strong) constraints that it initially contained.