Reuse of identifier names in subscopes leads to shadowing, that is, the names obscuration or shadowing. Reused identifiers in the current scope mask can render those defined elsewhere . This creates ambiguity especially when the originals need to be used and also leaves the code hard to maintain. The problem gets aggravated inaccessible. Although the Java Language Specification (JLS) [JLS 2013] clearly resolves any syntactic ambiguity arising from obscuring or shadowing, such ambiguity burdens code maintainers and auditors, especially when code requires access to both the original named entity and the inaccessible one. The problem is exacerbated when the reused name is defined in a different package.
Noncompliant Code Example
This noncompliant example implements a class that reuses the name of class java.util.Vector
. The intent of this class is to introduce a different condition for the isEmpty
method for native legacy code interfacing. A future programmer may not know about this extension and may incorrectly use the Vector
idiom to use the original Vector
class. This behavior is clearly undesirable.
According to §6.4.2, "Obscuring," of the JLS [JLS 2013],
A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package.
This implies that a variable can obscure a type or a package, and a type can obscure a package name. Shadowing, on the other hand, refers to one variable rendering another variable inaccessible in a containing scope. One type can also shadow another type.
No identifier should obscure or shadow another identifier in a containing scope. For example, a local variable should not reuse the name of a class field or method or a class name or package name. Similarly, an inner class name should not reuse the name of an outer class or package.
Both overriding and shadowing differ from hiding, in which an accessible member (typically nonprivate) that should have been inherited by a subclass is replaced by a locally declared subclass member that assumes the same name but has a different, incompatible method signature.
Noncompliant Code Example (Field Shadowing)
This noncompliant code example reuses the name of the val
instance field in the scope of an instance method.Well defined import statements do resolve these issues but may get confusing when the reused name is defined in a different package. Moreover, a common (and misleading) tendency is to include the import statements after writing the code (many IDEs allow automatic inclusion as per requirements). As a result, such instances can go undetected.
Code Block | ||
---|---|---|
| ||
class VectorMyVector { private int val = 1; publicprivate booleanvoid isEmptydoLogic() { int if(val; == 1) //compares... with 1 instead of } } |
The resulting behavior can be classified as shadowing; the method variable renders the instance variable inaccessible within the scope of the method. For example, assigning to val
from within the method does not affect the value of the instance variable, although assigning to this.val
from within the method does.
Compliant Solution (Field Shadowing)
This compliant solution eliminates shadowing by changing the name of the variable defined in the method scope from val
to newValue
:
Code Block | ||
---|---|---|
| ||
class MyVector { private int val = 1; private void doLogic() { 0 return true; else int return falsenewValue; } //other... functionality is same as java.util.Vector } public class VectorUser { public static void main(String[] args} } |
Noncompliant Code Example (Variable Shadowing)
This example is noncompliant because the variable i
defined in the scope of the second for
loop block shadows the definition of the instance variable i
defined in the MyVector
class:
Code Block | ||
---|---|---|
| ||
class MyVector { private int i = 0; private void doLogic() { Vectorfor v(i = new Vector(); 0; i < 10; i++) {/* ... */} for if(v.isEmpty()) System.out.println("Vector is empty");(int i = 0; i < 20; i++) {/* ... */} } } |
Compliant Solution
...
As a tenet, do not:
- Reuse the name of a superclass
- Reuse the name of an interface
- Reuse the name of a field defined in a superclass
- Reuse the name of a field that appears in the same method (in different scope)
(Variable Shadowing)
In this compliant solution, the loop counter i
is defined in the scope of each for
loop blockThis compliant solution declares the class Vector
with a different name:
Code Block | ||
---|---|---|
| ||
class MyVector { private void doLogic() { for (int i = 0; i < 10; i++) {/* ... */} for (int i = 0; i < 20; i++) {//other code* ... */} } } |
Risk Assessment
Applicability
Name reuse makes code more difficult Reusing names leads to code that is harder to read and maintain and may , which can result in security weaknesses. An automated tool can easily detect reuse of identifiers in containing scopes.
...
Automated Detection
Tool |
---|
Version |
---|
Checker |
---|
Description |
---|
Level
SCP03-J
low
unlikely
medium
P2
L3
Automated Detection
TODO
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
This rule appears in the C Secure Coding Standard as DCL01-C. Do not reuse variable names in subscopes.
This rule appears in the C++ Secure Coding Standard as DCL01-CPP. Do not reuse variable names in subscopes.
References
Wiki Markup |
---|
\[[Bloch 08|AA. Java References#Bloch 08]\] Puzzle 67: All Strung Out
\[[FindBugs 08|AA. Java References#FindBugs 08]\]:
Nm: Class names shouldn't shadow simple name of implemented interface
Nm: Class names shouldn't shadow simple name of superclass
MF: Class defines field that masks a superclass field
MF: Method defines a variable that obscures a field |
Parasoft Jtest |
| CERT.DCL51.HMF | Do not give method local variables and parameters the same name as class fields | ||||||
SonarQube |
| HiddenFieldCheck |
Bibliography
Puzzle 67, "All Strung Out" | |
Item 16, "Prefer Interfaces to Abstract Classes" | |
§6.3, "Placement" | |
[JLS 2013] | §6.4.1, "Shadowing" |
...
SCP02-J. Use nested classes carefully 03. Scope (SCP) 04. Integers (INT)