| Active/Severity | Name [expand / collapse] | Sort by: | ||
|---|---|---|---|---|
|
The usual convention for
Obtaining the object that will be returned by calling
For example, the following code:
class BaseClass implements Cloneable {
@Override
public Object clone() throws CloneNotSupportedException { // Non-Compliant - should return the super.clone() instance
return new BaseClass();
}
}
class DerivedClass extends BaseClass implements Cloneable {
/* Does not override clone() */
public void sayHello() {
System.out.println("Hello, world!");
}
}
class Application {
public static void main(String[] args) throws Exception {
DerivedClass instance = new DerivedClass();
((DerivedClass) instance.clone()).sayHello(); // Throws a ClassCastException because invariant #2 is violated
}
}
should be refactored into:
class BaseClass implements Cloneable {
@Override
public Object clone() throws CloneNotSupportedException { // Compliant
return super.clone();
}
}
class DerivedClass extends BaseClass implements Cloneable {
/* Does not override clone() */
public void sayHello() {
System.out.println("Hello, world!");
}
}
class Application {
public static void main(String[] args) throws Exception {
DerivedClass instance = new DerivedClass();
((DerivedClass) instance.clone()).sayHello(); // Displays "Hello, world!" as expected. Invariant #2 is satisfied
}
}
Repository: squid
Key: S1182
Available since 07 Jan 2014
|
||||
|
Overriding the The following code snippet illustrates this rule:
protected void finalize() { // Non-Compliant
releaseSomeResources();
}
protected void finalize() {
super.finalize(); // Non-Compliant
releaseSomeResources();
}
protected void finalize() {
releaseSomeResources();
super.finalize(); // Compliant
}
Repository: squid
Key: ObjectFinalizeOverridenCallsSuperFinalizeCheck
Available since 07 Jan 2014
|
||||
|
When the execution is not explicitly terminated at the end of a switch case, it continues to execute the statements of the following case. While this is sometimes intentional, it often is a mistake which leads to unexpected behavior. This rule doesn't apply to empty cases. Indeed those empty cases allow you to specify the same behavior for a group of cases. The following code snippet illustrates this rule:
switch (myVariable) {
case 0: // Compliant
case 1: // Compliant
doSomething();
break;
case 2: // Compliant
return;
case 3: // Compliant
throw new IllegalStateException();
case 4: // Compliant
continue;
case 5: // Non-Compliant - both 'doSomething()' and 'doSomethingElse()' will be executed
doSomething();
default: // Non-Compliant
doSomethingElse();
}
Repository: squid
Key: S128
Available since 07 Jan 2014
|
||||
|
Switch cases should remain small to keep the overall switch compact and readable. The following code snippet illustrates this rule with the default threshold of 5:
switch (myVariable) {
case 0: // Compliant - 5 lines till following case
System.out.println("");
System.out.println("");
System.out.println("");
break;
default: // Non-Compliant - 6 lines till switch end
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
break;
}
Repository: squid
Key: S1151
Available since 07 Jan 2014
|
||||
|
The requirement for a final default clause is defensive programming. This clause should either take appropriate action or contain a suitable comment as to why no action is taken. The following code snippet illustrates this rule:
switch (state) { // Non-Compliant - must have a default case
case 0:
case 1:
System.out.println("0 or 1!");
break;
}
switch (state) {
default: // Non-Compliant - must be last for better readability
throw new IllegalStateException();
case 0:
case 1:
System.out.println("0 or 1!");
break;
}
switch (state) {
case 0:
case 1:
System.out.println("0 or 1!");
break;
default: // Compliant
throw new IllegalStateException();
Repository: squid
Key: SwitchLastCaseIsDefaultCheck
Available since 07 Jan 2014
|
||||
|
Early classes of the Java API, such as It is better to use their new unsynchronized replacements:
Noncompliant Code ExampleVector cats = new Vector(); Compliant SolutionArrayList cats = new ArrayList(); ExceptionsUse of those synchronized classes is allowed in method signatures when overriding an existing method.
@Override
public Vector getCats() {...}
Repository: squid
Key: S1149
Available since 07 Jan 2014
|
||||
|
Calling Noncompliant Code ExampleSystem.exit(0); Runtime.getRuntime().exit(0);
Repository: squid
Key: S1147
Available since 07 Jan 2014
|
||||
|
Two important requirements must be fulfilled when logging messages:
If a program directly writes to the standard output, there is absolutely no way to comply with these requirements. That's why defining and using a dedicated logger is highly recommended. The following code snippet illustrates this rule:
System.out.println("My Message"); // Non-Compliant
logger.log("My Message"); // Compliant
Repository: squid
Key: S106
Available since 07 Jan 2014
|
||||
|
According to the Java Language Specification: Unnamed packages are provided by the Java platform principally for convenience when developing small or temporary applications or when just beginning development. To enforce this best practice, classes located in default package can no longer be access from named ones since Java 1.4. The following piece of code:
public class MyClass { /* ... */ }
should be refactored into:
package org.example;
public class MyClass{ /* ... */ }
Repository: squid
Key: S1220
Available since 07 Jan 2014
|
||||
|
According to the official Java documentation, The following code snippet illustrates this rule:
public void dispose() throws Throwable {
this.finalize(); // Non-Compliant
}
Repository: squid
Key: ObjectFinalizeCheck
Available since 07 Jan 2014
|
||||
|
This Object.finalize() method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. But there is absolutely no warranty that this method will be called AS SOON AS the last references to the object are removed. It can be few microseconds to few minutes later. So when some system resources need to be disposed by an object it's better to not rely on this asynchronous mechanism to dispose them. The following piece of code illustrates this rule:
public class MyClass {
protected void finalize() {
releaseSomeResources(); // Non-Compliant
}
}
Repository: squid
Key: ObjectFinalizeOverridenCheck
Available since 07 Jan 2014
|
||||
|
Catching either Only Noncompliant Code Example
try { /* ... */ } catch (Throwable t) { /* ... */ }
try { /* ... */ } catch (Error e) { /* ... */ }
Compliant Solution
try { /* ... */ } catch (Exception e) { /* ... */ }
try { /* ... */ } catch (RuntimeException e) { /* ... */ }
try { /* ... */ } catch (MyException e) { /* ... */ }
Repository: squid
Key: S1181
Available since 07 Jan 2014
|
||||
|
Loggers should be used instead to print throwables, as they have many advantages:
The following code:
try {
/* ... */
} catch(Exception e) {
e.printStackTrace(); // Non-Compliant
}
should be refactored into:
try {
/* ... */
} catch(Exception e) {
LOGGER.log("context", e); // Compliant
}
Repository: squid
Key: S1148
Available since 07 Jan 2014
|
||||
|
An exception in a
The following code:
void foo() throws MyException, MyException {} // Non-Compliant - should be listed once
void bar() throws Throwable, Exception {} // Non-Compliant - Exception is a subclass of Throwable
void baz() throws RuntimeException {} // Non-Compliant - RuntimeException can always be thrown
should be refactored into:
void foo() throws MyException {} // Compliant
void bar() throws Throwable {} // Compliant
void baz() {} // Compliant
Repository: squid
Key: RedundantThrowsDeclarationCheck
Available since 07 Jan 2014
|
||||
|
The following code illustrates this rule:
void doSomething() {
// TODO
}
Repository: squid
Key: S1135
Available since 07 Jan 2014
|
||||
|
Nesting The following code:
try {
try { // Non-Compliant
doSomething();
} catch (RuntimeException e) {
/* Ignore */
}
doSomethingElse();
} catch (Exception e) {
/* ... */
}
should be refactored into:
try {
dedicatedMethod(); // Compliant
doSomethingElse();
} catch (Exception e) {
/* ... */
}
/* ... */
private void dedicatedMethod() {
try { // Compliant
doSomething();
} catch (RuntimeException e) {
/* Ignore */
}
}
Repository: squid
Key: S1141
Available since 07 Jan 2014
|
||||
|
Sharing some naming conventions is a key point to make it possible for a team to efficiently collaborate. This rule allows to check that all type parameter names match a provided regular expression. The following code snippet illustrates this rule when the regular expression value is "^[A-Z]$":
class MyClass<TYPE> { // Non-Compliant
<TYPE> void addAll(Collection<TYPE> c) { // Non-compliant
}
}
public class MyClass<T> { // Compliant
<T> void addAll(Collection<T> c) { // Compliant
}
}
Repository: squid
Key: S00119
Available since 07 Jan 2014
|
||||
|
Deprecated
Detects when a local variable is declared and/or assigned, but not used. This rule is deprecated, use squid:S1481 instead.
Repository: pmd
Key: UnusedLocalVariable
Available since 07 Jan 2014
|
||||
|
Fields in interfaces are automatically public static final, and methods are public abstract. Classes or interfaces nested in an interface are automatically public and static (all nested interfaces are automatically static). For historical reasons, modifiers which are implied by the context are accepted by the compiler, but are superfluous.
Repository: pmd
Key: UnusedModifier
Available since 07 Jan 2014
|
||||
|
Deprecated
Detects when a private field is declared and/or assigned a value, but not used. This rule is deprecated, use squid:S1068 instead.
Repository: pmd
Key: UnusedPrivateField
Available since 07 Jan 2014
|
||||
|
Private methods that are never executed are dead code. Dead code means unnecessary, inoperative code that should be removed. This helps in maintenance by decreasing the maintained code size, making it easier to understand the program and preventing bugs from being introduced. In the following two cases, private methods are not considered as dead code by SonarQube :
Repository: squid
Key: UnusedPrivateMethod
Available since 07 Jan 2014
|
||||