Anonymous avatar Anonymous committed bfd60e3

Adding Variable Binding (aka Assignment2Itself) description

Comments (0)

Files changed (1)

 }}}
 
 Where {{{$loc}}} is a variable: it will be bound to anything that will appear in
-the source code at the corresponding place.
+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
 
 }}}
 
 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}}}.
+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 {{{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.
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.