UnusedCode Rules (UnusedCode)
Rules available in this category:
- Avoid_unused_private_methods
- Avoid_unused_public_or_protected_methods
- Avoid_unused_package_private_methods
- Avoid_unused_public_or_protected_classes
- Avoid_unused_package_private_classes
- Avoid_unused_public_or_protected_fields
- Avoid_unused_package_private_fields
- Avoid_unused_labels
- Avoid_unused_methods_in_anonymous_class
- Avoid_empty_switch_statement
- Do_not_explicitly_extend_class_from_Object
- Avoid_Unused_Imports
- Avoid_imports_from_same_package
- Avoid_unused_private_fields
- Avoid_unused_private_classes_and_interfaces
- Avoid_unreacheable_if_cases
- Avoid_useless_method_overriding
- Always_use_caught_exception
- Avoid_unused_parameters
- Avoid_unused_local_variables
Rule 1: Avoid_unused_private_methods
Severity:
Low
Rule:
Unused private methods should be removed.
Reason:
Unused private methods should be removed.
Usage Example:
package com.rule;
class Avoid_unused_private_methods_violation
{
private void action() // VIOLATION
{
//...
}
public void method()
{
//...
}
}
Should be written as:
package com.rule;
class Avoid_unused_private_methods_correction
{
/*
private void action() // CORRECTION
{
//...
}
*/
public void method()
{
//...
}
}
Reference:
Reference Not Available.
Rule 2: Avoid_unused_public_or_protected_methods
Severity:
Low
Rule:
Unused public code might be accessed by attackers and that the code may implement features that bypass security.
Reason:
Unused public code might be accessed by attackers and that the code may implement features that bypass security.
Usage Example:
public class Test
{
public void fubar() // VIOLATION
{
//....
}
}
Should be written as:
public class Test
{
// FIXED (method removed)
}
Reference:
Not available.
Rule 3: Avoid_unused_package_private_methods
Severity:
Low
Rule:
Unused code might be accessed by attackers and that the code may implement features that bypass security.
Reason:
Unused code might be accessed by attackers and that the code may implement features that bypass security.
Usage Example:
public class Test
{
void fubar() // VIOLATION
{
//....
}
}
Should be written as:
public class Test
{
// FIXED (method removed)
}
Reference:
Not available.
Rule 4: Avoid_unused_public_or_protected_classes
Severity:
Low
Rule:
Unused public code might be accessed by attackers and that the code may implement features that bypass security.
Reason:
Unused public code might be accessed by attackers and that the code may implement features that bypass security.
Usage Example:
public class Test // VIOLATION
{
}
Should be written as:
// FIXED (class removed)
Reference:
Not available.
Rule 5: Avoid_unused_package_private_classes
Severity:
Low
Rule:
Unused code might be accessed by attackers and that the code may implement features that bypass security.
Reason:
Unused code might be accessed by attackers and that the code may implement features that bypass security.
Usage Example:
class Test // VIOLATION
{
}
Should be written as:
// FIXED (class removed)
Reference:
Not available.
Rule 6: Avoid_unused_public_or_protected_fields
Severity:
Low
Rule:
Unused public code might be accessed by attackers and that the code may implement features that bypass security.
Reason:
Unused public code might be accessed by attackers and that the code may implement features that bypass security.
Usage Example:
public class Test
{
public int myField; // VIOLATION
}
Should be written as:
public class Test
{
// FIXED (field removed)
}
Reference:
Not available.
Rule 7: Avoid_unused_package_private_fields
Severity:
Low
Rule:
Unused code might be accessed by attackers and that the code may implement features that bypass security.
Reason:
Unused code might be accessed by attackers and that the code may implement features that bypass security.
Usage Example:
public class Test
{
int myField; // VIOLATION
}
Should be written as:
public class Test
{
// FIXED (field removed)
}
Reference:
Not available.
Rule 8: Avoid_unused_labels
Severity:
Low
Rule:
Avoid unused labels.
Reason:
Avoid unused labels.
Usage Example:
package com.rule;
public class Avoid_unused_labels_violation
{
public static void method()
{
lbl2 : // VIOLATION
{
// DO NOTHING
}
}
}
Should be written as:
package com.rule;
public class Avoid_unused_labels_correction
{
public static void method()
{
// CORRECTION
{
// DO NOTHING
}
}
}
Reference:
Reference Not Available.
Rule 9: Avoid_unused_methods_in_anonymous_class
Severity:
Medium
Rule:
It could either be unused code or a possible bug.
Reason:
It could either be unused code or a possible bug.
Usage Example:
public class Test
{
public void fubar()
{
new SuperClass()
{
public void someMethod()
{
//...
}
public void uselessMethod() // VIOLATION
{
//...
}
};
new MyInterface()
{
public void someInterfaceMethod()
{
//...
}
public void anotherUselessMethod() // VIOLATION
{
//....
}
};
}
}
interface MyInterface
{
void someInterfaceMethod();
}
class SuperClass
{
public void someMethod()
{
}
}
Should be written as:
public class Test
{
public void fubar()
{
new SuperClass()
{
public void someMethod()
{
//...
}
// FIXED: method removed
};
new MyInterface()
{
public void someInterfaceMethod()
{
//...
}
// FIXED: method removed
};
}
}
interface MyInterface
{
void someInterfaceMethod();
}
class SuperClass
{
public void someMethod()
{
}
}
Reference:
Not Available.
Rule 10: Avoid_empty_switch_statement
Severity:
Low
Rule:
Do not use empty switch statements.
Reason:
Do not use empty switch statements.
Usage Example:
package com.rule;
public class Avoid_empty_switch_statement_violation
{
public void method()
{
int month = 10;
switch (month)// Violation
{
}
}
}
Should be written as:
Avoid empty switch statements.
Reference:
Reference Not Available.
Rule 11: Do_not_explicitly_extend_class_from_Object
Severity:
Low
Rule:
Do not explicitly extend a class from java.lang.Object
Reason:
Do not explicitly extend a class from java.lang.Object
Usage Example:
package com.rule;
public class Do_not_explicitly_extend_class_from_Object_violation extends java.lang.Object // Violation
{
}
Should be written as:
package com.rule;
public class Do_not_explicitly_extend_class_from_Object_correction// Correction
{
}
Reference:
http://www.cafeaulait.org/course/week4/33.html
Rule 12: Avoid_Unused_Imports
Severity:
Low
Rule:
Avoid importing classes which are not used.
Reason:
Avoid importing classes which are not used.
Usage Example:
package com.rule;
import java.awt.*; //voilation
import java.awt.Frame;
import javax.swing.*; //violation
import java.awt.event.ActionListener; //violation
public class Avoid_unused_imports_violation
{
Frame f;
java.awt.event.ActionListener al = null;
public Avoid_unused_imports(Frame mf)
{
f = mf;
}
}
Should be written as:
package com.rule;
//import java.awt.*; //correction
import java.awt.Frame;
//import javax.swing.*; //correction
//import java.awt.event.ActionListener; //correction
public class Avoid_unused_imports_correction //correction
{
Frame f;
java.awt.event.ActionListener al = null;
public Avoid_unused_imports(Frame mf)
{
f = mf;
}
}
Reference:
Reference Not Available.
Rule 13: Avoid_imports_from_same_package
Severity:
Low
Rule:
Importing classes from the same package as the current class is redundant.
Reason:
Importing classes from the same package as the current class is redundant.
Usage Example:
package myPackage.package1;
import myPackage.package1.*; // VIOLATION
import myPackage.package1.someClass; //VIOLATION
public class RedundantImport
{
}
Should be written as:
package myPackage.package1;
//FIXED
public class RedundantImport
{
}
Reference:
No references available.
Rule 14: Avoid_unused_private_fields
Severity:
Low
Rule:
Unused private fields should be removed.
Reason:
Unused private fields should be removed.
Usage Example:
package com.rule;
class Avoid_unused_private_fields_violation
{
private int i; // VIOLATION
}
Should be written as:
package com.rule;
class Avoid_unused_private_fields_correction
{
//private int i; // CORRECTION
}
Reference:
Reference Not Available.
Rule 15: Avoid_unused_private_classes_and_interfaces
Severity:
Low
Rule:
Avoid unused private classes and interfaces.
Reason:
Avoid unused private classes and interfaces.
Usage Example:
package com.rule;
public class Avoid_unused_private_classes_and_interfaces_violation
{
private class PrivateClass // VIOLATION
{
}
}
Should be written as:
package com.rule;
public class Avoid_unused_private_classes_and_interfaces_correction
{
// CORRECTION : private unused class removed
}
Reference:
Reference Not Available.
Rule 16: Avoid_unreacheable_if_cases
Severity:
Low
Rule:
These if-elseif cases are unreachable.
Reason:
These if-elseif cases are unreachable.
Usage Example:
public class UnreachableIf
{
public String getGradeFromScore(int score)
{
boolean b = true;
Object a = null;
if (score< 50)
{
return "FAIL";
}
else if (score>= 50)
{
return "PASS;
}
else if (score== 100) //VIOLATION
{
return "PERFECT"
}
else // VIOLATION
{
return "INVALID_SCORE";
}
if ( b )
{
//...
}
else if ( b == false )
{
//...
}
else // VIOLATION
{
//...
}
if ( a == null )
{
//...
}
else if ( a != null )
{
//...
}
else // VIOLATION
{
//...
}
}
}
Should be written as:
public class UnreachableIf
{
public String getGradeFromScore(int score)
{
boolean b = true;
Object a = null;
if (score>= 0 && score< 50)
{
return "FAIL";
}
else if (score>= 50 && score< 100)
{
return "PASS;
}
else if (score== 100) // CORRECTION
{
return "PERFECT"
}
else // CORRECTION
{
return "INVALID_SCORE";
}
if ( b )
{
//...
}
else if ( b == false )
{
//...
}
if ( a == null )
{
//...
}
else if ( a != null )
{
//...
}
}
}
Reference:
No references available.
Rule 17: Avoid_useless_method_overriding
Severity:
Low
Rule:
Avoid overriding methods which just call the same method defined in the superclass.
Reason:
Avoid overriding methods which just call the same method defined in the superclass.
Usage Example:
public class SuperClass
{
public void aMethod()
{
//...
}
}
public class SubClass extends SuperClass
{
public void aMethod() // VIOLATION
{
super.aMethod();
}
}
Should be written as:
Reference:
Reference not available.
Rule 18: Always_use_caught_exception
Severity:
Medium
Rule:
The caught exception should not be ignored.
Reason:
The caught exception should not be ignored.
Usage Example:
package com.rule;
import java.net.MalformedURLException;
import java.net.URL;
public class Always_use_caught_exception_violation
{
public void method(String str)
{
try
{
URL url = new URL(str);
}
catch (MalformedURLException e) // VIOLATION
{
}
}
}
Should be written as:
package com.rule;
import java.net.MalformedURLException;
import java.net.URL;
public class Always_use_caught_exception_correction
{
public void method(String str)
{
try
{
URL url = new URL(str);
}
catch (MalformedURLException e)
{
System.out.println("Error occurred: " + e.getMessage());
}
}
}
Reference:
No reference available.
Rule 19: Avoid_unused_parameters
Severity:
Low
Rule:
An unused parameter in the method should be removed.
Reason:
An unused parameter in the method should be removed.
Usage Example:
package com.rule;
class Avoid_unused_parameters_violation
{
public void method(int i) // VIOLATION
{
}
}
Should be written as:
package com.rule;
class Avoid_unused_parameters_correction
{
public void method() // CORRECTION
{
//...
}
}
Reference:
Reference Not Available.
Rule 20: Avoid_unused_local_variables
Severity:
Low
Rule:
An unused local variable should be removed.
Reason:
An unused local variable should be removed.
Usage Example:
package com.rule;
class Avoid_unused_local_variables_violation
{
public void method()
{
final int ZERO = 0;
final int TEN = 10;
int i = ZERO;
int j = ZERO; // VIOLATION
for(int k=ZERO;i<TEN;i++) // VIOLATION
{
i++;
}
}
}
Should be written as:
package com.rule;
class Avoid_unused_local_variables_correction
{
public void method()
{
final int ZERO = 0;
final int TEN = 10;
int i = ZERO;
for(i = ZERO;i<TEN;i++)
{
i++;
}
// CORRECTION : Variables j and k have been removed, as there were never been used.
}
}
Reference:
Reference Not Available.