Java Portability
Rules available in this category:
- Do_not_define_program_logic_using_Thread_scheduling
- Avoid_Native_calls
- Avoid_enum_as_field_name
- Avoid_enum_as_local_variable_name
- Avoid_enum_as_method_parameter_name
- Avoid_assert_as_field_name
- Avoid_assert_as_local_variable_name
- Avoid_assert_as_method_parameter_name
- Avoid_hardcoded_position_for_GUI_elements
- Always_use_File.pathSeparator_or_File.pathSeparatorChar
- Avoid_standard_output_input_error
- Avoid_hardcoded_line_separator
- Avoid_System_getenv
- Avoid_call_to_Runtime_exec
- Avoid_hardcoded_numerics_for_Color
- Avoid_eclipse_internal_package
- Avoid_sun_classes
- Avoid_java_awt_peer
Rule 1: Do_not_define_program_logic_using_Thread_scheduling
Severity:
High
Rule:
Do not rely on thread scheduling to define program logic.
Reason:
Do not rely on thread scheduling to define program logic.
Usage Example:
package com.rule;
public class Do_not_define_program_logic_using_Thread_scheduling_violation
{
public Do_not_define_program_logic_using_Thread_scheduling_violation()
{
Thread newThread = new Thread();
newThread.setPriority(Thread.MAX_PRIORITY); // VIOLATION
newThread.start();
}
}
Should be written as:
Do not define program logic using Thread scheduling
Reference:
http://www.javapractices.com/Topic38.cjp
http://www.javapractices.com/Topic53.cjp
Rule 2: Avoid_Native_calls
Severity:
High
Rule:
Avoid defining native method as it requires different libraries to run the application in different OS.
Reason:
Avoid defining native method as it requires different libraries to run the application in different OS.
Usage Example:
package com.rule;
class Avoid_Native_calls_violation
{
native void method(String command); // VIOLATION
}
Should be written as:
Avoid using Native methods
Reference:
David Flanagan: "Java in a Nutshell". O'Reilly
Rule 3: Avoid_enum_as_field_name
Severity:
High
Rule:
Since enum is a keyword in jdk 1.5, it might cause compilation errors.
Reason:
Since enum is a keyword in jdk 1.5, it might cause compilation errors.
Usage Example:
public class Test
{
String enum = "foo"; // VIOLATION
}
Should be written as:
public class Test
{
String strFoo = "foo"; // FIXED
}
Reference:
Not Available.
Rule 4: Avoid_enum_as_local_variable_name
Severity:
High
Rule:
Since enum is a keyword in jdk 1.5, it might cause compilation errors.
Reason:
Since enum is a keyword in jdk 1.5, it might cause compilation errors.
Usage Example:
public class Test
{
public void fubar()
{
String enum = "foo"; // VIOLATION
}
}
Should be written as:
public class Test
{
public void fubar()
{
String strFoo = "foo"; // FIXED
}
}
Reference:
Not Available.
Rule 5: Avoid_enum_as_method_parameter_name
Severity:
High
Rule:
Since enum is a keyword in jdk 1.5, it might cause compilation errors.
Reason:
Since enum is a keyword in jdk 1.5, it might cause compilation errors.
Usage Example:
public class Test
{
public void fubar(String enum) // VIOLATION
{
//...
}
}
Should be written as:
public class Test
{
public void fubar(String strFoo) // FIXED
{
//...
}
}
Reference:
Not Available.
Rule 6: Avoid_assert_as_field_name
Severity:
High
Rule:
Since assert is a keyword in jdk 1.5, it might cause compilation errors.
Reason:
Since assert is a keyword in jdk 1.5, it might cause compilation errors.
Usage Example:
public class Test
{
String assert = "foo"; // VIOLATION
}
Should be written as:
public class Test
{
String strFoo = "foo"; // FIXED
}
Reference:
Not Available.
Rule 7: Avoid_assert_as_local_variable_name
Severity:
High
Rule:
Since assert is a keyword in jdk 1.5, it might cause compilation errors.
Reason:
Since assert is a keyword in jdk 1.5, it might cause compilation errors.
Usage Example:
public class Test
{
public void fubar()
{
String assert = "foo"; // VIOLATION
}
}
Should be written as:
public class Test
{
public void fubar()
{
String strFoo = "foo"; // FIXED
}
}
Reference:
Not Available.
Rule 8: Avoid_assert_as_method_parameter_name
Severity:
High
Rule:
Since assert is a keyword in jdk 1.5, it might cause compilation errors.
Reason:
Since assert is a keyword in jdk 1.5, it might cause compilation errors.
Usage Example:
public class Test
{
public void fubar(String assert) // VIOLATION
{
//...
}
}
Should be written as:
public class Test
{
public void fubar(String strFoo) // FIXED
{
//...
}
}
Reference:
Not Available.
Rule 9: Avoid_hardcoded_position_for_GUI_elements
Severity:
Low
Rule:
Do not use hard-coded positions for size of a GUI element.
Reason:
Do not use hard-coded positions for size of a GUI element.
Usage Example:
import javax.swing.JLabel;
class Avoid_hardcoded_position_for_GUI_element_violation
{
public void method()
{
JLabel lbl = new JLabel("Name");
lbl.setBounds(0, 0, 20, 20); // VIOLATION
}
}
Should be written as:
Don't use hard-coded positions for a component. Use Layout manager instead.
Reference:
Reference Not Available.
Rule 10: Always_use_File.pathSeparator_or_File.pathSeparatorChar
Severity:
Low
Rule:
The path separator character is system-dependent.
Reason:
The path separator character is system-dependent.
Usage Example:
class Test
{
private static final String ex= "This"+";"+//VIOLATION
';'+//VIOLATION
":"+//VIOLATION
':';//VIOLATION
}
Should be written as:
class Test
{
private static final String ex= "This"+File.pathSeparator+ //FIXED
File.pathSeparatorChar+//FIXED
File.pathSeparator+ //FIXED
File.pathSeparatorChar;//FIXED
}
Reference:
Not Available.
Rule 11: Avoid_standard_output_input_error
Severity:
High
Rule:
Avoid using the standard input, output and error streams in programs.
Reason:
Avoid using the standard input, output and error streams in programs.
Usage Example:
package com.rule;
class Avoid_standard_output_input_error_violation
{
public void setValue(Object obj)
{
obj.getClass();
System.out.println(); // VIOLATION
//....
}
}
Should be written as:
package com.rule;
class Avoid_standard_output_input_error_correction
{
public void setValue(Object obj)
{
obj.getClass();
//System.out.println(); // CORRECTION
//....
}
}
Reference:
http://www-4.ibm.com/software/webservers/appserv/ws_bestpractices.pdf
Rule 12: Avoid_hardcoded_line_separator
Severity:
High
Rule:
Reports an error if a constant string is used for line separator characters in the program.
Reason:
Reports an error if a constant string is used for line separator characters in the program.
Usage Example:
package com.rule;
class Avoid_hardcoded_line_separator_violation
{
public void method ()
{
String strNewLine = "\n"; // VIOLATION
System.out.println(strNewLine);
}
}
Should be written as:
package com.rule;
class Avoid_hardcoded_line_separator_correction
{
public void method ()
{
String strLineSeperator = "line.separator";
String strNewLine = System.getProperty(strLineSeperator); // CORRECTION
System.out.println(strNewLine);
}
}
Reference:
David Flanagan: "Java in a Nutshell". O'Reilly
Rule 13: Avoid_System_getenv
Severity:
High
Rule:
'System.getenv ()' has been "deprecated" because it is not portable. This is because not all platforms have environment variables.
Reason:
'System.getenv ()' has been "deprecated" because it is not portable. This is because not all platforms have environment variables.
Usage Example:
package com.rule;
class Avoid_System_getenv_violation
{
void method (String name)
{
System.getenv (name); // VIOLATION
}
}
Should be written as:
package com.rule;
class Avoid_System_getenv_correction
{
void method (String name)
{
System.getProperty (name); // CORRECTION
}
}
Reference:
David Flanagan: "Java in a Nutshell". O'Reilly
Rule 14: Avoid_call_to_Runtime_exec
Severity:
High
Rule:
Calling the 'Runtime.getRuntime().exec()' method may not be portable because there is no guarantee on how the native OS command will behave on different platforms.
Reason:
Calling the 'Runtime.getRuntime().exec()' method may not be portable because there is no guarantee on how the native OS command will behave on different platforms.
Usage Example:
package com.rule;
class Avoid_call_to_Runtime_exe_violation
{
public void method(String command)
{
try
{
Runtime.getRuntime().exec(command); // VIOLATION
}
catch (java.io.IOException ex)
{
ex.printStackTrace();
}
}
}
Should be written as:
Avoid using 'Runtime.getRuntime().exec()' method.
Reference:
David Flanagan: "Java in a Nutshell". O'Reilly
Rule 15: Avoid_hardcoded_numerics_for_Color
Severity:
Low
Rule:
Do not create objects of class java.awt.Color.
Reason:
Do not create objects of class java.awt.Color.
Usage Example:
package com.rule;
import java.awt.Color;
public class Avoid_hardcoded_numerics_for_Color_violation
{
Color color = new Color(0, 0, 0); // Violation
}
Should be written as:
package com.rule;
import java.awt.Color;
public class Avoid_hardcoded_numerics_for_Color_correction
{
Color color = Color.black; // Correction
}
Reference:
http://www.javapractices.com/Topic38.cjp
Rule 16: Avoid_eclipse_internal_package
Severity:
Low
Rule:
Avoid using org.eclipse.*.internal.* since the API is not guaranteed to be stable or backwards compatible.
Reason:
Avoid using org.eclipse.*.internal.* since the API is not guaranteed to be stable or backwards compatible.
Usage Example:
import org.eclipse.jdt.internal.core.SourceType; // VIOLATION
import org.eclipse.jdt.internal.compiler.env.ISourceType; // VIOLATION
public class Test
{
public boolean isInterface( SourceType o )
{
if ( o instanceof SourceType )
{
try
{
ISourceType type = (ISourceType)o.getElementInfo();
if ( type.isInterface() )
{
return true;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
return false;
}
}
Should be written as:
Consult Eclipse Help, Eclipse discussion boards, or contact the team that provided the code for suggestions.
Reference:
No references available.
Rule 17: Avoid_sun_classes
Severity:
Low
Rule:
Avoid using the sun.* classes wherever possible.
Reason:
Avoid using the sun.* classes wherever possible.
Usage Example:
package com.rule;
import sun.tools.asm.Label
public class Avoid_sun_classes_violation
{
Label l = new Label(); // VIOLATION
}
Should be written as:
Avoid using sun classes.
Reference:
Reference Not Available.
Rule 18: Avoid_java_awt_peer
Severity:
High
Rule:
Do not use the 'java.awt.peer' package directly.
Reason:
Do not use the 'java.awt.peer' package directly.
Usage Example:
package com.rule;
import java.awt.peer.ComponentPeer; // VIOLATION
class Avoid_java_awt_peer_violation
{
}
Should be written as:
Do not use the 'java.awt.peer' package directly.
Reference:
David Flanagan: "Java in a Nutshell". O'Reilly