Difference between revisions of "Globalyzer 5 Java Rules"

From Lingoport Wiki
Jump to: navigation, search
(Code Snippet)
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
  +
 
= Introduction =
 
= Introduction =
  +
Globalyzer 5 introduced new types of rules based on a improved, in-depth i18n parsing.
   
  +
Say that you want to filter strings passed as parameters to a method called '''get''', which is a fairly common method name. With Globalyzer 5, you can specify the class name on which the rule should be applied. Let's say the ''I18nUtil'' class and the ''UIUtil'' both have a ''''get'''' method.
= User Interface =
 
  +
* You can specify a string filter method on '''get''' for variables of type ''I18nUtil'' and
  +
* You can specify a string detection method on '''get''' for variables of type ''UIUtil''.
  +
* Static methods are also handled (''I18nUtil.get("string")'' for example)
  +
* You can also have an overarching '''get''' rule for all variable types by leaving the ''Class or Variable Type(s)'' field empty, as opposed to listing all the class types on which a detection or a filter must apply.
  +
  +
= User Interface =
  +
Globalyzer 5 Java allows for class names to be specified as part of the rules. The following UI shows how to configure a new <code>String Method Filter</code> for the <code>smalljava</code> illustrative rule set.
  +
  +
[[File:RS with Class or Variable Type.png]]
  +
* '''Name''': That the name of the rule. It could be something like <code>i18n get</code>
  +
* '''Pattern''': That is the pattern which would match the method name. It could be something as simple as <code>get</code>
  +
* '''Class or Variable Type(s)''': This is a pattern which would match the class name. It could be something like <code>company.project.util.I18nUtil</code>
  +
* '''Description''': The description or the reason for this filter could be something like "I18nUtil get method string parameters do not need to be externalized into a resource bundle for i18n purposes"
  +
* '''Help Page''': The link to a more verbose help page which may indicate the context and the reason for the filter.
  +
  +
'''Note''': The value of '''Class/Variable Type(s)''' is a string delimited list of fully qualified classes and types. If the field is empty, the methods are filtered/detected on all variables or classes accordingly.
   
 
= Type of Rules =
 
= Type of Rules =
  +
The first rules to be impacted are Java String Method Filters and Java String Method Detection rules. Passing a String parameter to a Java method has the added level of knowing the variable class name. Filtering on a method called "setText" will now allow to differentiate between classes or objects of different types with that method being invoked. This will make for better rules and finer results.
  +
  +
As Lingoport explores other possibilities, other rules and other programming languages will be covered.
   
 
= Example =
 
= Example =
   
 
== Code Snippet ==
 
== Code Snippet ==
<code>
 
 
<pre>
 
<pre>
import company.project.Dbg;
+
import company.project.util.Dbg; // A fully qualified class name
import company.project.Label;
+
import company.project.ui.*; // Label is in the company.project.ui package.
 
[...]
 
[...]
 
Dbg dbg = Dbg.getInstance();
 
Dbg dbg = Dbg.getInstance();
 
Label lbl = new Label();
 
Label lbl = new Label();
 
[...]
 
[...]
dbg.setText("{0}: {1} action taken.");
+
dbg.setText("Create User action taken.");
 
[...]
 
[...]
 
label.setText("Menu");
 
label.setText("Menu");
 
</pre>
 
</pre>
  +
</code>
 
   
 
This snippet of code does have strings.
 
This snippet of code does have strings.
   
The class company.project.Dbg is a debug class and the text method puts the String parameter into a database for support purposes. That string is not visible to the end user. In that instance, the 'setText' method on a variable of type company.project.Dbg should be filtered.
+
* The class <code>company.project.Dbg</code> is a debug class and the text method puts the String parameter into a database for support purposes. That string is not visible to the end user. In that instance, the <code>setText</code> method on a variable of type <code>company.project.util.Dbg</code> should be filtered. The Strings in statements like <code>Dbg.setText("a string");</code> or <code>company.project.util.Dbg.setText("another string");</code> would also be filtered.
   
The variable lbl of class company.project.Label represents a text area in the User Unterface and the setText method passes a user visible string. The string "Menu" passed to this setText method should be flagged: It needs to be externalized out of the code into a resource bundle.
+
* The variable lbl of class <code>company.project.ui.Label</code> represents a text area in the User Interface and the <code>setText</code> method passes a user visible string. The string <code>Menu</code> passed to this <code>setText</code> method should be flagged: It needs to be externalized out of the code into a resource bundle.
   
 
== Rule ==
 
== Rule ==
  +
The String Method Filter rule would be configured the following way:
  +
* Name: '''Debug setText'''
  +
* Pattern: '''setText'''
  +
* Class or Variable Type(s): '''company.project.util.Dbg'''
  +
* Description: '''String parameters passed to the Debug setText must not be externalized and translated.'''
  +
* Help Page: <blank>
  +
  +
The String Method Detection rule would be configured the following way:
  +
* Name: '''User Interface Label setText'''
  +
* Pattern: '''setText'''
  +
* Class or Variable Type(s): '''company.project.ui.Label'''
  +
* Description: '''String parameters passed to the Label setText method must be externalized and translated.'''
  +
* Help Page: <blank>
   
 
== Result ==
 
== Result ==
  +
When the rules are applied:
  +
* The string passed to the Dbg variable ''setText'' method is filtered out and does not show up as a candidate issue
  +
* The string passed to the Label variable ''setText'' method is detected and does show up as a candidate issue
  +
  +
  +
[[File:Gz 5 Result.png]]

Latest revision as of 16:52, 14 March 2018

Introduction

Globalyzer 5 introduced new types of rules based on a improved, in-depth i18n parsing.

Say that you want to filter strings passed as parameters to a method called get, which is a fairly common method name. With Globalyzer 5, you can specify the class name on which the rule should be applied. Let's say the I18nUtil class and the UIUtil both have a 'get' method.

  • You can specify a string filter method on get for variables of type I18nUtil and
  • You can specify a string detection method on get for variables of type UIUtil.
  • Static methods are also handled (I18nUtil.get("string") for example)
  • You can also have an overarching get rule for all variable types by leaving the Class or Variable Type(s) field empty, as opposed to listing all the class types on which a detection or a filter must apply.

User Interface

Globalyzer 5 Java allows for class names to be specified as part of the rules. The following UI shows how to configure a new String Method Filter for the smalljava illustrative rule set.

RS with Class or Variable Type.png

  • Name: That the name of the rule. It could be something like i18n get
  • Pattern: That is the pattern which would match the method name. It could be something as simple as get
  • Class or Variable Type(s): This is a pattern which would match the class name. It could be something like company.project.util.I18nUtil
  • Description: The description or the reason for this filter could be something like "I18nUtil get method string parameters do not need to be externalized into a resource bundle for i18n purposes"
  • Help Page: The link to a more verbose help page which may indicate the context and the reason for the filter.

Note: The value of Class/Variable Type(s) is a string delimited list of fully qualified classes and types. If the field is empty, the methods are filtered/detected on all variables or classes accordingly.

Type of Rules

The first rules to be impacted are Java String Method Filters and Java String Method Detection rules. Passing a String parameter to a Java method has the added level of knowing the variable class name. Filtering on a method called "setText" will now allow to differentiate between classes or objects of different types with that method being invoked. This will make for better rules and finer results.

As Lingoport explores other possibilities, other rules and other programming languages will be covered.

Example

Code Snippet

import company.project.util.Dbg;  // A fully qualified class name
import company.project.ui.*; // Label is in the company.project.ui package.
 [...]
 Dbg dbg = Dbg.getInstance();
 Label lbl = new Label();
 [...]
 dbg.setText("Create User action taken.");
 [...]
 label.setText("Menu");


This snippet of code does have strings.

  • The class company.project.Dbg is a debug class and the text method puts the String parameter into a database for support purposes. That string is not visible to the end user. In that instance, the setText method on a variable of type company.project.util.Dbg should be filtered. The Strings in statements like Dbg.setText("a string"); or company.project.util.Dbg.setText("another string"); would also be filtered.
  • The variable lbl of class company.project.ui.Label represents a text area in the User Interface and the setText method passes a user visible string. The string Menu passed to this setText method should be flagged: It needs to be externalized out of the code into a resource bundle.

Rule

The String Method Filter rule would be configured the following way:

  • Name: Debug setText
  • Pattern: setText
  • Class or Variable Type(s): company.project.util.Dbg
  • Description: String parameters passed to the Debug setText must not be externalized and translated.
  • Help Page: <blank>

The String Method Detection rule would be configured the following way:

  • Name: User Interface Label setText
  • Pattern: setText
  • Class or Variable Type(s): company.project.ui.Label
  • Description: String parameters passed to the Label setText method must be externalized and translated.
  • Help Page: <blank>

Result

When the rules are applied:

  • The string passed to the Dbg variable setText method is filtered out and does not show up as a candidate issue
  • The string passed to the Label variable setText method is detected and does show up as a candidate issue


Gz 5 Result.png