Clone wiki

jackpot30-demo-examples / Home

Jackpot 3.0 Examples

Motivation Example

Java test file: Script file: o.o.u.Utilities.hint


In NetBeans there is method Utilities.loadImage(String). As many more image-related methods have been added to Utilities, a new class ImageUtilities has been introduced, and the image-related utilities have been put there. The original methods have been deprecated and delegate to the new methods. But, how to we support the transition from the old methods to the new methods for our API clients? Can't this be done semi-automatically.

This is where the Jackpot language comes to play: it allows us to easily create a custom refactoring that will allow the API clients to easily upgrade their code.

In this case the script is:


Where $loc is a variable: it will be bound to anything that will appear in the source code at the corresponding place. Note the rule is ended with a double-semicolon (;;). this allows to place several rules in one file.


Java test file: Script file: j.s.ImageIcon.hint


One of the utility methods in ImageUtilities is image2Icon, which is a replacement for new javax.swing.ImageIcon(Image, which may bypass the icon creation is some cases.

Based on the previous script, we could try to write:

new javax.swing.ImageIcon($image)

This would work correctly in rewriting:

Image image = ...;
Icon icon = new javax.swing.ImageIcon(image);


Image image = ...;
Icon icon = ImageUtilities.image2Icon(image);

But, it would also attempt to rewrite:

String fileName = ...;
Icon icon = new javax.swing.ImageIcon(fileName);


String fileName = ...;
Icon icon = ImageUtilities.image2Icon(fileName);

which is not correct, and would break the code. We need to specify that the expression that will be bound to variable $image needs to be of type java.awt.Image. We do that as follows:

new javax.swing.ImageIcon($image) :: $image instanceof java.awt.Image

The instanceof is a condition. The conditions are written behind a ::. The warning and fix is used only when the conditions are true.

Variable Binding

Note: examples from now on use common J2SE antipatterns and updates to describe more features of the Jackpot rules language.

Java test file: Script file:


A common mistake is to assign a field's into the field itself, like this:

public Assignment2Itself(String aValue) {
    this.value = aValue;

A simple Jackpot rule to detect this situation is:

$var = $var

The engine will automatically ensure, that the code that corresponds to one occurrence of $var will be the "same" as the code that corresponds to the other occurrence. I.e., this rule will mark any of value = value, this.value = value, value = this.value, this.value = this.value, but not value = aValue. It will unfortunately also match in the following case:

String[] array = ...;
int i = 0;
array[i++] = array[i++];

Which is not very appropriate. We can use the elementKindMatches condition to limit the warning to cases where $var represents a field: :: elementKindMatches($var, ElementKind.FIELD) or possibly any variable: :: elementKindMatches($var, ElementKind.FIELD, ElementKind.PARAMETER, ElementKind.LOCAL_VARIALBE, ElementKind.EXCEPTION_PARAMETER).

Note that this example shows that the rule does not have to define a transformation: this rule will only produce a warning.