SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Source Templates for Conditions and Transformations

The “Copy Facts To Clipboard” dialog is a tool that makes it very easy to create

  • a condition that searches for code with the same structure as some available piece of source code or
  • a transformation that generates code with the same structure as some available piece of source code.

Opening the Dialog

After selecting some Java source code1) you can open the “Copy Facts To Clipboard” dialog …

… either through the editor context menu … or through the main JTransformer menu

Dialog Structure

The “Copy To Clipboard” dialog is divided into three parts:

  • The "Choose Type" options: Here you can select what you would like to copy:
    • Copy facts: Copy the Prolog facts for the chosen source code, all IDs are hardcoded
    • Copy conditions: Copy a conditions that matches the selection, all IDs are replaced by variables. Here you can chose to include source locations information.
    • Copy transformation: Copy a transformation that creates exactly the same code.
    • Copy CTC assertion: see Conditional Transformation Core
  • You can also choose to include
    • the zero-argument constructor that the Java compiler synthesizes automatically for classes that declare no explicit contructor.
  • The variable table: The variable table dispalys all logic variables contained in the generated condition / transformation. Their automatically created names (VV1, VV2, etc) are displayed in the “Old Name” column. In the column “New Name” you can assign more meaningful variable names. These must conform to Prolog variable syntax (which is checked by the dialog).
  • The preview box: The preview box shows exactly what will be copied to the clipboard. It is automaticly updated when you rename a variable or change the type options.

An Example

To add the source code project for this example do the follwing:

  1. Open “File → New → Example… → JTransformer Example Projects
  2. Select “Copy to clipboard example” and click “Finish”
The Scenario

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?

The Solution

With the help of JTransformer's “Copy Fact To Clipboard” Dialog detecting structurally identical or similar code is a trivial task:

  • In the Java Editor select the copied method. In our sample code linked above this is the method “hello_copy()” in “MyClass”
  • From the context menu select the “Copy Facts To Clipboard” action.
  • In the Copy to Clipboard dialog that appears select “Copy condition”. Since we are interested in finding the method that we copied from, change the name of the method id variable V1 to MethodID. This, of course, is not compulsory but makes the created condition easier to understand. After having done so the preview box will automatically update and the dialog will look like this:

  • Click on “Copy”. Now the condition facts are in your clipboard.
  • Create a new file in your project and name it “find_copy.pl”2). Paste the condition facts in the empty file.
  • To be able to call this complex condition easily, turn it into the body of a predicate definition:
    • type “find_copy(MethodID) :-” in front of the condition to add a

predicate head that is parameterized by the ID of the method you want to find.

  • make sure that the condition is terminated by a period (”.“).
  • Now adjust your new predicate definiton to suit your needs:
  • Because you want to find a method with identical structure but different method and variable names, replace all hard-coded names and IDs with new prolog variables. In particular:
    • replace the CompilationUnit ID in the first MethodT (in this example 17191) by an underscore “_”
    • replace the method name 'hello_copy' by a variable MethodName 3).
    • replace all occurrences of the field name 'field_copy' with the same Prolog variable, for example FieldName.
    • The return strings 'hello' and 'goodbye' are again specific to your method. Since we don't use them anywhere else we can replace them by an underscore “_”.4)
  • Add the MethodName variable as a second parameter to the predicate head to make the output more readable.

This is how the predicate should look now:5)

Your structural clone detection predicate is ready!

The Prolog Editor is part of the Prolog Development Tool plugin. For more information you can visit the Prolog Editor Documentation
The Results

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!

The big picture

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.

1)
consider, that your file had to be saved to copy facts from it
2)
This step is also not really neccesary , one could directly paste the condition into the prolog console.
3)
Remember, Prolog variables must start with upper case letters
4)
You could also use names like ReturnString1, which would result in a “Singleton Variables”-Warning
5)
You can find the find_copy_sample.pl in the Zip-File
research/jtransformer/tutorial_old/copy_to_clipboard.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2019