Home

Jackpot 3.0 Examples

Motivation Example

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

Description:

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:

org.openide.util.Utilities.loadImage($loc)
=>
org.openide.util.ImageUtilities.loadImage($loc)
;;

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.

Conditions

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

Description:

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)
=>
org.openide.util.ImageUtilities.image2Icon($image)
;;

This would work correctly in rewriting:

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

to

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

But, it would also attempt to rewrite:

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

to

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
=>
org.openide.util.ImageUtilities.image2Icon($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: Assignment2Itself.java Script file: assignment.to.itself.hint

Description:

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.

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.