Java language enumeration types have an ordinal()
method , which that returns the numerical position of each enumeration constant in its class declaration.unmigrated-wiki-markup
The _Java Language Specification_ \[[JLS 2005|AA. Bibliography#JLS 05]\] [§8.9, "Enums"|http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9] does not specify the use of {{ordinal()}} in programs. However, using the {{ordinal()}} method to derive the value associated with an enum constant is error-prone and should be avoided.
public final int ordinal()
returns Returns the ordinal of the enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as
EnumSet
andEnumMap
.
The Java Language Specification, §8.9, "Enums" [JLS 2013], does not specify the use of ordinal()
in programs. However, attaching external significance to the ordinal()
value of an enum
constant is error prone and should be avoided for defensive programming.
Noncompliant Code Example
This noncompliant code example declares enum Hydrocarbon
and uses its ordinal()
method to provide the result of the getNumberOfCarbons()
method.:
Code Block | ||
---|---|---|
| ||
enum Hydrocarbon {
METHANE, ETHANE, PROPANE, BUTANE, PENTANE,
HEXANE, HEPTANE, OCTANE, NONANE, DECANE;
public int getNumberOfCarbons() {
return ordinal() + 1;
}
}
|
While Although this noncompliant code example worksbehaves as expected, its maintenance is susceptible likely to vulnerabilitiesbe problematic. If the enum
constants were reordered, the getNumberOfCarbongetNumberOfCarbons()
method would return incorrect values. AlsoFurthermore, BENZENE
- which also has 6 carbons - cannot be added without violating the current enum designadding an additional BENZENE
constant to the model would break the invariant assumed by the getNumberOfCarbons()
method because benzene has six carbons, but the ordinal value 6 is already taken by HEXANE
.
Compliant Solution
In this compliant solution, enum
constants are explicitly associated with the corresponding integer values for the number of carbon atoms they contain.:
Code Block | ||
---|---|---|
| ||
enum Hydrocarbon { METHANE(1), ETHANE(2), PROPANE(3), BUTANE(4), PENTANE(5), HEXANE(6), BENZENE(6), HEPTANE(7), OCTANE(8), NONANE(9), DECANE(10); private final int numberOfCarbons; Hydrocarbon(int carbons) { this.numberOfCarbons = carbons; } public int getNumberOfCarbons() { return numberOfCarbons; } } |
Risk Assessment
The getNumberOfCarbons()
method no longer uses the ordinal()
to discover the number of carbon atoms for each value. Different enum
constants may be associated with the same value, as shown for HEXANE
and BENZENE
. Furthermore, this solution lacks any dependence on the order of the enumeration; the getNumberOfCarbons()
method would continue to work even if the enumeration were reordered.
Applicability
It is acceptable to use the ordinals associated with an enumerated type when the order of the enumeration constants is standard and extra constants cannot be added. For example, the use of ordinals is permitted with the following enumerated type:
Code Block |
---|
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY }
|
In general, use Use of ordinals to derive integer values reduces the program's maintainability and can lead to errors in the program.
Guideline | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL03-J | low | probable | medium | P4 | L3 |
Related Guidelines
C Secure Coding Standard: "INT09-C. Ensure enumeration constants map to unique values"
C++ Secure Coding Standard: "INT09-CPP. Ensure enumeration constants map to unique values"
Bibliography
Bibliography
[API 2013] | |||||||
[Bloch 2008] | Item 31, "Use Instance Fields Instead of Ordinals" | ||||||
[JLS 2013] | §8 | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="052d8f32-2461-4b1a-aaec-f79b1d00d237"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05] | [§8.9, "Enums" | http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0d28d92f-36a7-4da8-87d7-7329ba5bf0b2"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Enum | http://download.oracle.com/javase/6/docs/api/java/lang/Enum.html] | ]]></ac:plain-text-body></ac:structured-macro> |
...
VOID DCL10-J. Ensure proper initialization by declaring class and instance variables final 01. Declarations and Initialization (DCL) DCL12-J. Prevent class initialization cycles