Java Internationalization (i18n)
Rules available in this category:
- Avoid_string_concat_method
- Avoid_getCurrencyInstance_method
- Avoid_single_char_with_logical_comparison
- Use_constants_for_char_literals
- Avoid_Using_String_Split
- Avoid_Using_String_IndexOf
- Avoid_Using_toString_to_convert_BigInteger_to_String
- Avoid_Using_toString_to_convert_BigDecimal_to_String
- Avoid_Using_Constructor_String_Using_Bytes
- Avoid_Using_String_compareTo_Method
- Avoid_Using_Method_String_equals
- Avoid_Using_Method_String_getBytes
- Avoid_calling_java_lang_Byte_toString
- Avoid_calling_java_lang_Short_toString
- Avoid_using_java_io_FileReader
- Avoid_using_java_io_FileWriter
- Avoid_using_java_io_InputStreamReader_with_default_character_set
- Avoid_using_java_io_OutputStreamWriter_with_default_character_set
- Avoid_using_java_lang_Character_toLowerCase
- Avoid_using_java_lang_Character_toUpperCase
- Avoid_calling_java_util_Date_toString
- Avoid_using_parseByte_on_java_lang_Byte
- Avoid_using_charAt_on_java_lang_String
- Avoid_using_valueOf_on_java_lang_String
- Avoid_using_parseFloat_on_java_lang_Float
- Avoid_using_parseInt_on_java_lang_Integer
- Avoid_using_parseLong_on_java_lang_Long
- Avoid_using_parseShort_on_java_lang_Short
- Avoid_calling_toString
- Avoid_using_java_awt_TextArea_directly
- Avoid_using_java_awt_TextField_directly
- Avoid_calling_Time_toString
- Avoid_String_compareTo_method
- Do_not_use_String_compareToIgnoreCase
- Avoid_non_internationalized_string
- Avoid_String_equals_method
- Always_use_locale_specific_case_conversion
- Do_not_use_String_CASE_INSENSITIVE_ORDER
Rule 1: Avoid_string_concat_method
Severity:
Medium
Rule:
Disallows string concatenation using 'String.concat ()' in an internationalized environment
Reason:
Disallows string concatenation using 'String.concat ()' in an internationalized environment
Usage Example:
package com.rule;
class Avoid_string_concate_method_violation
{
public void print()
{
//.............
String s = "";
s = s.concat("AppPerfect"); // VIOLATION
//....
}
}
Should be written as:
Use MessageFormat to concatenate strings in internationalized
environment. MessageFormat provides a means to produce concatenated messages in language-neutral way.
Reference:
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Rule 2: Avoid_getCurrencyInstance_method
Severity:
Medium
Rule:
This method will return outdated currency for individual European countries, instead of returning the Euro.
Reason:
This method will return outdated currency for individual European countries, instead of returning the Euro.
Usage Example:
import java.text.NumberFormat;
import java.util.Locale;
public class Test
{
public static void main(String[] args)
{
double value = 12345.678;
Locale defaultLocale = Locale.getDefault();
NumberFormat nf =
NumberFormat.getCurrencyInstance(defaultLocale); // VIOLATION
String formattedValue = nf.format(value);
System.out.println(formattedValue);
}
}
Should be written as:
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Currency;
public class Test
{
public static void main(String[] args)
{
double value = 12345.678;
Currency currency = Currency.getInstance("EUR"); // CORRECTION
Locale defaultLocale = Locale.getDefault();
NumberFormat nf = NumberFormat.getCurrencyInstance(defaultLocale);
nf.setCurrency(currency);
String formattedValue = nf.format(value);
System.out.println(formattedValue);
}
}
Reference:
No references available.
Rule 3: Avoid_single_char_with_logical_comparison
Severity:
Medium
Rule:
Avoid using single character with logic operators in an internationalized environment
Reason:
Avoid using single character with logic operators in an internationalized environment
Usage Example:
package com.rule;
class Avoid_single_char_with_logical_comparison_violation
{
public void doAction()
{
char ch;
// ...
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) // Violation
{
// ...
}
}
}
Should be written as:
package com.rule;
class Avoid_single_char_with_logical_comparison_correction
{
public void doAction()
{
char ch;
// ...
if (Character.isLetter(ch)) // CORRECTION
{
// ...
}
}
}
Reference:
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Rule 4: Use_constants_for_char_literals
Severity:
Medium
Rule:
Put single character literals in constants.
Reason:
Put single character literals in constants.
Usage Example:
package com.rule;
class Use_constants_for_char_literals_violation
{
public char method(int i)
{
char cResult = 'N'; // VIOLATION
if(i == 0)
{
cResult = 'Y'; // VIOLATION
}
return cResult;
}
}
Should be written as:
package com.rule;
class Use_constants_for_char_literals_correction
{
public static final char YES = 'Y'; // CORRECTION
public static final char NO = 'N'; // CORRECTION
public char method(int i)
{
char cResult = NO;
if(i == 0)
{
cResult = YES;
}
return cResult;
}
}
Reference:
http://java.sun.com/docs/books/tutorial/i18n/
Rule 5: Avoid_Using_String_Split
Severity:
Medium
Rule:
Split doesn't support strings containing combining characters and strings containing characters
which are canonically equivalent.
Reason:
Split doesn't support strings containing combining characters and strings containing characters
which are canonically equivalent.
Usage Example:
public class Test extends {
public void myMethod()
{
String value = new String("Text Message" );
String str = new String("compare");
str.split(value); //violation
}
}
Should be written as:
public class Test extends {
public void myMethod()
{
String value = new String("Text Message" );
String str = new String("compare");
str.split(value); //Fixed
}
}
Reference:
Rule 6: Avoid_Using_String_IndexOf
Severity:
Medium
Rule:
IndexOf does not properly support certain unicode characters before J2SE 1.5.0.
Reason:
IndexOf does not properly support certain unicode characters before J2SE 1.5.0.
Usage Example:
public class Test extends ClassLoader{
public void myMethod()
{
String value = new String("Text Message" );
String s = new String("Text");
String str = new String("compare");
if(s.indexOf(3) == value.indexOf(3,3)) //violation
{
System.out.println("Test.myMethod()" + value.indexOf(3)); //violation
}
}
}
Should be written as:
public class Test extends ClassLoader{
public void myMethod()
{
String value = new String("Text Message" );
String s = new String("Text");
String str = new String("compare");
if(s.indexOf(3) == value.indexOf(3,3)) //violation
{
System.out.println("Test.myMethod()" + value.indexOf(3)); //violation
}
}
}
Reference:
http://oss.software.ibm.com/icu/userguide/searchString.html
Rule 7: Avoid_Using_toString_to_convert_BigInteger_to_String
Severity:
Medium
Rule:
It may not correctly handle all locales.
Reason:
It may not correctly handle all locales.
Usage Example:
package base;
import java.math.BigInteger;
public class Test extends {
public void myMethod() {
BigInteger big = new BigInteger("15");
System.out.println(big.toString()); //violation
}
}
Should be written as:
package base;
import java.math.BigInteger;
public class Test extends {
public void myMethod() {
BigInteger big = new BigInteger("15");
System.out.println(big); //Fixed
}
}
Reference:
No Refrences Available.
Rule 8: Avoid_Using_toString_to_convert_BigDecimal_to_String
Severity:
Medium
Rule:
It may not correctly handle all locales.
Reason:
It may not correctly handle all locales.
Usage Example:
package base;
import java.math.BigInteger;
import java.util.Date;
public class Test extends {
public void myMethod() {
BigInteger big = new BigInteger("15");
System.out.println(big.toString()); //violation
}
}
Should be written as:
package base;
import java.math.BigInteger;
import java.util.Date;
public class Test extends {
public void myMethod() {
BigInteger big = new BigInteger("15");
System.out.println(big); //Fixed
}
}
Reference:
No Refrences available.
Rule 9: Avoid_Using_Constructor_String_Using_Bytes
Severity:
Medium
Rule:
These constructors interpret the byte array according to the default character set and may produce unexpected results
Reason:
These constructors interpret the byte array according to the default character set and may produce unexpected results
Usage Example:
package base;
public class Test
{
public void myMethod() {
String value = new String("Text Message");
String str = new String(value.getBytes()); //violation
}
}
Should be written as:
package base;
public class Test
{
public void myMethod() {
String value = new String("Text Message");
String str = new String(value.getBytes()); //Fixed
}
}
Reference:
No Refrences Available.
Rule 10: Avoid_Using_String_compareTo_Method
Severity:
Medium
Rule:
These methods do not properly handle locale-sensitive strings and string with Unicode supplementary characters.
Reason:
These methods do not properly handle locale-sensitive strings and string with Unicode supplementary characters.
Usage Example:
package base;
public class Test extends ClassLoader{
public void myMethod() {
String value = new String("Text Message");
String str = new String("compare");
value.compareTo(str); //violation
value.compareTo((Object)"hello"); //violation
value.compareToIgnoreCase(str); //violation
}
}
Should be written as:
package base;
public class Test extends ClassLoader{
public void myMethod() {
String value = new String("Text Message");
String str = new String("compare");
value.compareTo(str); //violation
value.compareTo((Object)"hello"); //violation
value.compareToIgnoreCase(str); //violation
}
}
Reference:
No Reference Available.
Rule 11: Avoid_Using_Method_String_equals
Severity:
Medium
Rule:
These methods are not locale-sensitive.
Reason:
These methods are not locale-sensitive.
Usage Example:
package base;
public class Test
{
public void myMethod()
{
String value = new String("Text Message");
String str = new String("compare");
if(str.equals(value) //violation
{
System.out.println("match");
}
}
}
Should be written as:
package base;
public class Test
{
public void myMethod()
{
String value = new String("Text Message");
String str = new String("compare");
if(str.equals(value) //fixed
{
System.out.println("match");
}
}
}
Reference:
No References available.
Rule 12: Avoid_Using_Method_String_getBytes
Severity:
Medium
Rule:
If the string-to-byte conversion using the default character set fails, the data will be lost.
Reason:
If the string-to-byte conversion using the default character set fails, the data will be lost.
Usage Example:
package base;
public class Test
{
public void myMethod() {
String value = new String("Text Message");
String s = new String(value.getBytes()); //violation
}
}
Should be written as:
package base;
public class Test
{
public void myMethod() {
String value = new String("Text Message");
String s = new String(value.getBytes()); //violation
}
}
Reference:
No References available.
Rule 13: Avoid_calling_java_lang_Byte_toString
Severity:
Medium
Rule:
Avoid calling toString() on an object of type java.lang.Byte.
Reason:
Avoid calling toString() on an object of type java.lang.Byte.
Usage Example:
public class BTS
{
public static void main( String[] args )
{
byte b = (byte) 0x80;
Byte B = new Byte( b );
System.out.println(Byte.toString( b ) ); // VIOLATION
System.out.println( B.toString() ); // VIOLATION
}
}
Should be written as:
public class BTS
{
public static void main(String[] args)
{
byte b = (byte) 0x80;
Byte B = new Byte(b);
java.text.NumberFormat f;
java.util.Locale l;
l = java.util.Locale.ENGLISH;
f = java.text.NumberFormat.getNumberInstance(l);
System.out.println(" English: " + f.format(b)); //$NON-NLS-1$
System.out.println(" English: " + f.format(B)); //$NON-NLS-1$
l = new java.util.Locale("ar", "AE"); //$NON-NLS-1$ //$NON-NLS-2$
f = java.text.NumberFormat.getNumberInstance(l);
System.out.println(" Arabic: " + f.format(b)); //$NON-NLS-1$
System.out.println(" Arabic: " + f.format(B)); //$NON-NLS-1$
l = new java.util.Locale("mk", "MK"); //$NON-NLS-1$ //$NON-NLS-2$
f = java.text.NumberFormat.getNumberInstance(l);
System.out.println("Macedonia: " + f.format(b)); //$NON-NLS-1$
System.out.println("Macedonia: " + f.format(B)); //$NON-NLS-1$
}
}
Reference:
No references available.
Rule 14: Avoid_calling_java_lang_Short_toString
Severity:
Medium
Rule:
Avoid calling toString() on an object of type java.lang.Short.
Reason:
Avoid calling toString() on an object of type java.lang.Short.
Usage Example:
public class BTS
{
public static void main( String[] args )
{
short s = -12345;
Short S = new Short(s);
System.out.println(Short.toString(s)); // VIOLATION
System.out.println(S.toString()); // VIOLATION
}
}
Should be written as:
public class BTS
{
public static void main(String[] args)
{
short n = -12345;
Short N = new Short(n);
java.text.NumberFormat f;
java.util.Locale l;
l = java.util.Locale.ENGLISH;
f = java.text.NumberFormat.getNumberInstance(l);
System.out.println(Messages.getString("ShortToString_Solution1.0") + f.format(n)); //$NON-NLS-1$
System.out.println(" English: " + f.format(N)); //$NON-NLS-1$
l = new java.util.Locale("ar", "AE"); //$NON-NLS-1$ //$NON-NLS-2$
f = java.text.NumberFormat.getNumberInstance(l);
System.out.println(" Arabic: " + f.format(n)); //$NON-NLS-1$
System.out.println(" Arabic: " + f.format(N)); //$NON-NLS-1$
l = new java.util.Locale("mk", "MK"); //$NON-NLS-1$ //$NON-NLS-2$
f = java.text.NumberFormat.getNumberInstance(l);
System.out.println("Macedonia: " + f.format(n)); //$NON-NLS-1$
System.out.println("Macedonia: " + f.format(N)); //$NON-NLS-1$
}
}
Reference:
No references available.
Rule 15: Avoid_using_java_io_FileReader
Severity:
Medium
Rule:
Avoid using java.io.FileReader because the constructors have no charset parameters.
Reason:
Avoid using java.io.FileReader because the constructors have no charset parameters.
Usage Example:
public class NoFileReader
{
public static void main( String[] args )
{
// Assume default charset ISO-8859-1 and file with UTF-16BE content
FileReader fileReader = null;
try
{
fileReader = new java.io.FileReader( "file1" ); //$NON-NLS-1$// VIOLATION
System.out.println( fileReader.read() );
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
Should be written as:
public class NoFileReader
{
public static void main( String[] args )
{
// Assume default charset ISO-8859-1 and file with UTF-16BE content
InputStreamReader reader = null;
try
{
reader = new InputStreamReader( new FileInputStream("file1"), "UTF-16BE");// CORRECTION
System.out.println( reader.read() );
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
Reference:
No references available.
Rule 16: Avoid_using_java_io_FileWriter
Severity:
Medium
Rule:
Avoid using java.io.FileWriter because the constructors have no charset parameters.
Reason:
Avoid using java.io.FileWriter because the constructors have no charset parameters.
Usage Example:
public class NoFileWriter
{
public static void main( String[] args )
{
FileWriter fileWriter = null;
try
{
fileWriter = new FileWriter("file1"); //$NON-NLS-1$ // VIOLATION
fileWriter.write( "\uceba" ); //$NON-NLS-1$
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
Should be written as:
public class NoFileWriter
{
public static void main( String[] args )
{
OutputStreamWriter writer = null;
try
{
writer = new OutputStreamWriter(new FileOutputStream("file1"), //$NON-NLS-1$
"UTF-16BE"); //$NON-NLS-1$ // CORRECTION
writer.write( "\uceba" ); //$NON-NLS-1$
writer.flush();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
Reference:
No references available.
Rule 17: Avoid_using_java_io_InputStreamReader_with_default_character_set
Severity:
Medium
Rule:
Avoid using java.io.InputStreamReader with the default character set.
Reason:
Avoid using java.io.InputStreamReader with the default character set.
Usage Example:
public class ISR
{
public static void main(String[] args)
{
try
{
String fileName = "UnicodeSample.txt"; //$NON-NLS-1$
java.io.FileInputStream fileInput = null;
java.io.InputStreamReader reader = null;
fileInput = new java.io.FileInputStream(fileName);
reader = new java.io.InputStreamReader(fileInput);//UTF-16BE // VIOLATION
System.out.println(reader.read());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Should be written as:
public class ISR
{
public static void main(String[] args)
{
try
{
java.io.FileInputStream fileInput;
java.io.InputStreamReader reader;
fileInput = new java.io.FileInputStream("file1"); //$NON-NLS-1$
reader = new java.io.InputStreamReader(fileInput,"UTF-16BE"); //$NON-NLS-1$ // CORRECTION
System.out.println(reader.read());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Reference:
No references available.
Rule 18: Avoid_using_java_io_OutputStreamWriter_with_default_character_set
Severity:
Medium
Rule:
Avoid using java.io.OutputStreamWriter with the default character set.
Reason:
Avoid using java.io.OutputStreamWriter with the default character set.
Usage Example:
public class OSR
{
public static final void main(String[] args)
{
try
{
String str = "\uceba"; //UTF-16BE //$NON-NLS-1$
FileOutputStream fileOutput = new FileOutputStream("file1"); //UTF-16BE //$NON-NLS-1$
OutputStreamWriter writer = new OutputStreamWriter(fileOutput); // VIOLATION
writer.write(str);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Should be written as:
public class OSR
{
public static final void main(String[] args)
{
String str = "\uceba"; //str is a UTF-16BE //$NON-NLS-1$
try
{
FileOutputStream fileOutput = new FileOutputStream("test.txt"); //$NON-NLS-1$
OutputStreamWriter write = new OutputStreamWriter(fileOutput,
java.nio.charset.Charset.forName("UTF-16BE").newEncoder()); //$NON-NLS-1$
write.write(str);
write.flush();
write.close();
}
catch (java.io.FileNotFoundException e1)
{
System.out.println(e1.toString());
}
catch (java.io.IOException e2)
{
System.out.println(e2.toString());
}
}
}
Reference:
No references available.
Rule 19: Avoid_using_java_lang_Character_toLowerCase
Severity:
Medium
Rule:
Avoid using java.lang.Character.toLowerCase, because it does not support supplementary characters properly.
Reason:
Avoid using java.lang.Character.toLowerCase, because it does not support supplementary characters properly.
Usage Example:
public class CtoLower
{
public static void main(String[] args)
{
char ch = '\u4e2d';
System.out.println(Character.toLowerCase(ch)); // VIOLATION
}
}
Should be written as:
public class CtoLower
{
public static void main(String[] args)
{
int c1 = 0x10400;
int c2 = Character.toLowerCase(c1); // CORRECTION
}
}
Reference:
http://java.sun.com/developer/technicalArticles/Intl/Supplementary/
Rule 20: Avoid_using_java_lang_Character_toUpperCase
Severity:
Medium
Rule:
Avoid using java.lang.Character.toUpperCase, because it does not support supplementary characters properly.
Reason:
Avoid using java.lang.Character.toUpperCase, because it does not support supplementary characters properly.
Usage Example:
public class CtoUpper
{
public static void main(String[] args)
{
char ch = '\u4e2d';
System.out.println(Character.toUpperCase(ch)); // VIOLATION
}
}
Should be written as:
public class CtoUpper
{
public static void main(String[] args)
{
int c1 = 0x10428;
int c2 = Character.toUpperCase(c1); // CORRECTION
}
}
Reference:
http://java.sun.com/developer/technicalArticles/Intl/Supplementary/
Rule 21: Avoid_calling_java_util_Date_toString
Severity:
Medium
Rule:
Avoid calling toString() on an object of type java.util.Date.
Reason:
Avoid calling toString() on an object of type java.util.Date.
Usage Example:
public class DTS
{
public static void main( String[] args )
{
java.util.Date date = new java.util.Date();
System.out.println( date.toString() ); // VIOLATION
}
}
Should be written as:
public class DTS
{
public static void main(String[] args)
{
Date myDate = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL,
java.util.Locale.GERMAN);
System.out.println( df.format(myDate) );
}
}
Reference:
No references available.
Rule 22: Avoid_using_parseByte_on_java_lang_Byte
Severity:
Medium
Rule:
Avoid calling parseByte() on an object of type java.lang.Byte.
Reason:
Avoid calling parseByte() on an object of type java.lang.Byte.
Usage Example:
public class PB
{
public static void main( String[] args )
{
String strByte = "126-"; //126- is -126 in ar_EG //$NON-NLS-1$
try
{
System.out.println( Byte.parseByte(strByte ) );
}
catch ( Exception e )
{
// NumberFormatException
e.printStackTrace();
}
}
}
Should be written as:
public class PB
{
public static void main(String[] args)
{
java.util.Locale loc = new Locale("ar", "EG"); //$NON-NLS-1$ //$NON-NLS-2$
String strByte = "126-";; //$NON-NLS-1$
try
{
byte data = java.text.NumberFormat.getNumberInstance(loc).parse(
strByte).byteValue();
System.out.println(data);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
Reference:
No references available.
Rule 23: Avoid_using_charAt_on_java_lang_String
Severity:
Medium
Rule:
Avoid calling charAt() on an object of type java.lang.String.
Reason:
Avoid calling charAt() on an object of type java.lang.String.
Usage Example:
public class NoCharAt
{
public static void main(String[] args)
{
String str = "ABC\ud800\udc00";
char ch = str.charAt(3); // VIOLATION
System.out.println("String.charAt(3) = 0x" + Integer.toHexString(ch));
System.out.println("The output should be \"String.charAt(3) = 0x10000\""); }
}
Should be written as:
public class NoCharAt
{
public static void main(String[] args)
{
String str = "ABC\ud800\udc00";
int codepoint = str.codePointAt(3); // CORRECTION
System.out.print("String.codePointAt(3) = 0x");
System.out.println(Integer.toHexString(codepoint));
}
}
Reference:
http://java.sun.com/developer/technicalArticles/Intl/Supplementary/
Rule 24: Avoid_using_valueOf_on_java_lang_String
Severity:
Medium
Rule:
Avoid calling valueOf() on an object of type java.lang.String.
Reason:
Avoid calling valueOf() on an object of type java.lang.String.
Usage Example:
public class NoValueOf
{
public static void main(String[] args)
{
double d = 1234567.89;
long l = 123456789;
int i = 1234567;
float f = (float) 12345.678;
System.out.println(String.valueOf(d));
System.out.println(String.valueOf(f));
System.out.println(String.valueOf(l));
System.out.println(String.valueOf(i));
}
}
Should be written as:
public class NoValueOf
{
public static void main(String[] args)
{
NumberFormat nf_en = NumberFormat.getNumberInstance(new Locale("en", "US"));
double d = 1234567.89;
float f = (float) 12345.678;
long l = 123456789;
int i = 1234567;
System.out.println(nf_en.format(d));
System.out.println(nf_en.format(f));
System.out.println(nf_en.format(l));
System.out.println(nf_en.format(i));
}
}
Reference:
No references available.
Rule 25: Avoid_using_parseFloat_on_java_lang_Float
Severity:
Medium
Rule:
Avoid calling parseFloat() on an object of type java.lang.Float.
Reason:
Avoid calling parseFloat() on an object of type java.lang.Float.
Usage Example:
public class PB
{
public static void main( String[] args )
{
public static void main( String[] args )
{
String strFloat = "4 125,53"; //4 125,53 is 4125.53 in fr_FR //$NON-NLS-1$
try
{
System.out.println( Float.parseFloat( strFloat ) );
}
catch (Exception e)
{
// NumberFormatException
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
}
Should be written as:
public class PB
{
public static void main(String[] args)
{
String strFloat = "4 125,53"; // Note: the space used in string is no-break space instead of space. //$NON-NLS-1$
try
{
float data = java.text.NumberFormat.getNumberInstance(java.util.Locale.FRENCH).parse(strFloat).floatValue();
System.out.println(data);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
Reference:
No references available.
Rule 26: Avoid_using_parseInt_on_java_lang_Integer
Severity:
Medium
Rule:
Avoid calling parseInt() on an object of type java.lang.Integer.
Reason:
Avoid calling parseInt() on an object of type java.lang.Integer.
Usage Example:
public class PB
{
public static void main ( String[] args )
{
String strInteger = "125-"; // 125- is -125 in locale ar_EG //$NON-NLS-1$
try
{
System.out.println( Integer.parseInt(strInteger) );
}
catch (Exception e)
{
// NumberFormatException
e.printStackTrace();
System.out.println( e.getMessage() );
}
}
}
Should be written as:
public class PB
{
public static void main(String[] args)
{
java.util.Locale loc = new Locale("ar", "EG"); //$NON-NLS-1$ //$NON-NLS-2$
String strInteger = "125-"; //$NON-NLS-1$
try
{
int data = java.text.NumberFormat.getIntegerInstance(loc).parse(
strInteger).intValue();
System.out.println(data);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
Reference:
No references available.
Rule 27: Avoid_using_parseLong_on_java_lang_Long
Severity:
Medium
Rule:
Avoid calling parseLong() on an object of type java.lang.Long.
Reason:
Avoid calling parseLong() on an object of type java.lang.Long.
Usage Example:
public class PB
{
public static void main ( String[] args )
{
String strLong = "12345-"; // 12345- is -12345 in locale ar_EG //$NON-NLS-1$
try
{
System.out.println( Long.parseLong( strLong ) );
}
catch (Exception e)
{
// NumberFormatException
e.printStackTrace();
System.out.println( e.getMessage() );
}
}
}
Should be written as:
public class PB
{
public static void main(String[] args)
{
String strLong = "12345-"; //$NON-NLS-1$
try
{
long data = java.text.NumberFormat.getNumberInstance(java.util.Locale.FRENCH).parse(strLong).longValue();
System.out.println(data);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
Reference:
No references available.
Rule 28: Avoid_using_parseShort_on_java_lang_Short
Severity:
Medium
Rule:
Avoid calling parseShort() on an object of type java.lang.Short.
Reason:
Avoid calling parseShort() on an object of type java.lang.Short.
Usage Example:
public class PB
{
public static void main ( String[] args )
{
String strShort = "125-"; // 125- is -125 in locale ar_EG //$NON-NLS-1$
try
{
System.out.println( Short.parseShort(strShort) );
}
catch (Exception e)
{
// NumberFormatException
e.printStackTrace();
System.out.println( e.getMessage() );
}
}
}
Should be written as:
public class PB
{
public static void main(String[] args)
{
java.util.Locale loc = new Locale("ar", "AE"); //$NON-NLS-1$ //$NON-NLS-2$
String strShort = "125-"; //$NON-NLS-1$
try
{
short data = java.text.NumberFormat.getNumberInstance(loc).parse(strShort).shortValue();
System.out.println(data);
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
Reference:
No references available.
Rule 29: Avoid_calling_toString
Severity:
Medium
Rule:
Do not use toString() Method on Numerical data types in an internationalized environment e.g. Integer, Float..etc
Reason:
Do not use toString() Method on Numerical data types in an internationalized environment e.g. Integer, Float..etc
Usage Example:
package com.rule;
class Avoid_calling_toString_violation
{
public void method()
{
final long ONE = 1;
String str = new Long(ONE).toString(); // VIOLATION
}
}
Should be written as:
package com.rule;
class Avoid_calling_toString_correction
{
public void method()
{
final long ONE = 1;
String str = NumberFormat.getInstance(Locale.getDefault()).format(ONE); // CORRECTION
}
}
Reference:
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Rule 30: Avoid_using_java_awt_TextArea_directly
Severity:
Medium
Rule:
java.awt.TextArea has certain incompatiblities relating to strings.
Reason:
java.awt.TextArea has certain incompatiblities relating to strings.
Usage Example:
public class BadAwt
{
public static void main(String[] args)
{
String CJKString = "\u4e1c\u897f\u5357\u5317\u4e2d";
java.awt.TextArea textarea = new java.awt.TextArea(); // VIOLATION
textarea.setText(CJKString);
}
}
Should be written as:
public class BadAwt
{
public static void main(String[] args)
{
String CJKString = "\u4e1c\u897f\u5357\u5317\u4e2d";
javax.swing.JTextArea textarea = new javax.swing.JTextArea();// CORRECTION
textarea.setText(CJKString);
}
}
Reference:
No references available.
Rule 31: Avoid_using_java_awt_TextField_directly
Severity:
Medium
Rule:
java.awt.TextField has certain incompatiblities relating to strings.
Reason:
java.awt.TextField has certain incompatiblities relating to strings.
Usage Example:
public class BadAwt
{
public static void main(String[] args)
{
String CJKString = "\u4e1c\u897f\u5357\u5317\u4e2d";
java.awt.TextField textfield = new java.awt.TextField(); // VIOLATION
textfield.setText(CJKString);
}
}
Should be written as:
public class BadAwt
{
public static void main(String[] args)
{
String CJKString = "\u4e1c\u897f\u5357\u5317\u4e2d";
javax.swing.JTextField textfield = new javax.swing.JTextField();// CORRECTION
textfield.setText(CJKString);
}
}
Reference:
No references available.
Rule 32: Avoid_calling_Time_toString
Severity:
Medium
Rule:
Do not call Time.toString() to avoid issues related to internationalization
Reason:
Do not call Time.toString() to avoid issues related to internationalization
Usage Example:
package com.rule;
import java.sql.Time;
class Avoid_calling_Time_toString_violation
{
void printTime(Time t)
{
System.out.println(t.toString()); // VIOLATION
}
}
Should be written as:
package com.rule;
import java.sql.Time;
import java.text.DateFormat;
import java.util.Locale;
class Avoid_calling_Time_toString_correction
{
void printTime(Time t)
{
DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault());
System.out.println(timeFormatter.format(t)); // CORRECTION
}
}
Reference:
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Rule 33: Avoid_String_compareTo_method
Severity:
Medium
Rule:
Avoid String.compareTo() method to avoid issues related to internationalization
Reason:
Avoid String.compareTo() method to avoid issues related to internationalization
Usage Example:
package com.rule;
class Avoid_String_compareTo_method_violation
{
String name = "myname";
boolean isSame(String nm)
{
return name.compareTo(nm) == 0; // VIOLATION
}
}
Should be written as:
package com.rule;
import java.text.Collator;
class Avoid_String_compareTo_method_correction
{
String name = "myname";
boolean isSame(String nm)
{
Collator myCollator = Collator.getInstance ();
return (myCollator.compare(name, nm) < 0); // CORRECTION
}
}
Reference:
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Rule 34: Do_not_use_String_compareToIgnoreCase
Severity:
Medium
Rule:
Do not use compareToIgnoreCase in internationalized environment.
Reason:
Do not use compareToIgnoreCase in internationalized environment.
Usage Example:
package com.rule;
public class Do_not_use_String_compareToIgnoreCase_violation
{
public void method()
{
"String".compareToIgnoreCase("String"); // VIOLATION
}
}
Should be written as:
package com.rule;
public class Do_not_use_String_compareToIgnoreCase_correction
{
public void method()
{
new MyCollator().compare("String", "String"); // CORRECTION
}
class MyCollator extends java.text.Collator
{
public int compare(String source, String target)
{
return 0;
}
public java.text.CollationKey getCollationKey(String source)
{
return null;
}
public int hashCode()
{
return 0;
}
}
}
Reference:
http://www.javaworld.com/jw-01-1999/jw-01-internationalize-p2.html
http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#compareToIgnoreCase(java.lang.String)
Rule 35: Avoid_non_internationalized_string
Severity:
Medium
Rule:
Put String literals in constants.
Reason:
Put String literals in constants.
Usage Example:
package com.rule;
class Avoid_non_internationalized_string_violation
{
public void showStatus(Label lbl, boolean bStatus)
{
if (bStatus)
{
lbl.setText("Process was completed succesfully!");
}
else
{
lbl.setText("Process failed!");
}
}
}
Should be written as:
Create a resource file and load it using ResourceBundle class.
http://java.sun.com/docs/books/tutorial/i18n/resbundle/index.html
Reference:
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Rule 36: Avoid_String_equals_method
Severity:
Medium
Rule:
Do not use String.equals() method to avoid issues related to internationlization
Reason:
Do not use String.equals() method to avoid issues related to internationlization
Usage Example:
package com.rule;
class Avoid_String_equals_method_violation
{
String name = "myname";
boolean isSame(String nm)
{
return name.equals(nm); // VIOLATION
}
}
Should be written as:
package com.rule;
import java.text.Collator;
class Avoid_String_equals_method_correction
{
String name = "myname";
boolean isSame(String nm)
{
Collator myCollator = Collator.getInstance ();
return (myCollator.compare(name, nm) == 0); // CORRECTION
}
}
Reference:
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
Rule 37: Always_use_locale_specific_case_conversion
Severity:
Medium
Rule:
When using String.toUpperCase() or String.toLowerCase(), always specify the locale.
Reason:
When using String.toUpperCase() or String.toLowerCase(), always specify the locale.
Usage Example:
package com.rule;
public class Always_use_locale_specific_case_conversion_violation
{
private static final String ID = "SOME_ID";
public boolean hasID(String str)
{
return ID.toLowerCase().equals(str); // VIOLATION
}
}
Should be written as:
package com.rule;
import java.util.Locale;
public class Always_use_locale_specific_case_conversion_correction
{
private static final String ID = "SOME_ID";
public boolean hasID(String str, String langStr)
{
Locale l = new Locale(langStr);
return ID.toLowerCase(l).equals(str); // CORRECTION
}
}
Reference:
No reference available.
Rule 38: Do_not_use_String_CASE_INSENSITIVE_ORDER
Severity:
Medium
Rule:
Do not use String.CASE_INSENSITIVE_ORDER in internationalized environment.
Reason:
Do not use String.CASE_INSENSITIVE_ORDER in internationalized environment.
Usage Example:
package com.rule;
public class Do_not_use_String_CASE_INSENSITIVE_ORDER_violation
{
public void method()
{
String.CASE_INSENSITIVE_ORDER.compare("String", "String"); // VIOLATION
}
}
Should be written as:
package com.rule;
public class Do_not_use_String_CASE_INSENSITIVE_ORDER_correction
{
public void method()
{
new MyCollator().compare("String", "String"); // CORRECTION
}
class MyCollator extends java.text.Collator
{
public int compare(String source, String target)
{
return 0;
}
public java.text.CollationKey getCollationKey(String source)
{
return null;
}
public int hashCode()
{
return 0;
}
}
}
Reference:
http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#CASE_INSENSITIVE_ORDER