Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
Extending legacy classes and generifying the overriding methods fails because this is disallowed by the _Java Language Specification_ \[[JLS 2005|AA. Bibliography#JLS 05]\].

Noncompliant Code Example

This noncompliant code example compiles although it produces an unchecked warning because the raw type of the List.add() method is used (the list parameter in the addToList() method) rather than the parameterized type.

...

When executed, this code produces an exception not because a List<String> receives an Integer but because the value returned by list.get(0) is an improper type. In other words, the code throws an exception some time after execution of the operation that actually caused the exception, complicating debugging.

Compliant Solution (Parameterized Collection)

This compliant solution enforces type safety by changing the addToList() function signature to enforce proper type checking. It also complies by adding a String rather than an Integer.

...

The compiler does not allow insertion of an Object once list is parameterized. Likewise, addToList() cannot be called with an argument whose type produces a mismatch.

Compliant Solution (Legacy Code)

While the previous compliant solution eliminates use of raw collections, this could be infeasible when interoperating with legacy code.

...

The compiler still issues the "unchecked warning," which may still be ignored. However, the code now fails precisely when it attempts to add the Integer to the list, consequently preventing the program from proceeding with invalid data.

Noncompliant Code Example

This noncompliant code example compiles and runs cleanly because it suppresses the unchecked warning produced by the raw List.add() method. The printOne() method intends to print the value one, either as an int or as a double depending on the type of the variable type.

...

However, despite list being correctly parameterized, this method prints '1' and never '1.0' because the int value '1' is always added to list without being type checked. This code produces the following output:

Code Block
1.0
1
1
1

Compliant Solution

This compliant solution generifies the addToList() method, which eliminates any possible type violations.

...

If the method addToList() is externally defined (such as in a library or as an upcall method) and cannot be changed, the same compliant method printOne() can be used, but no warnings result if addToList(1) is used instead of addToList(1.0). Great care must be taken to ensure type safety when generics are mixed with non-generic code.

Exceptions

Wiki Markup
*OBJ14OBJ03-EX0* Raw types must be used in class literals. For example, because {{List<Integer>.class}} is illegal, it is permissible to use the raw type {{List.class}} \[[Bloch 2008|AA. Bibliography#Bloch 08]\].

Wiki Markup
*OBJ14OBJ03-EX1* The {{instanceof}} operator cannot be used with generic types. It is permissible to mix generic and raw code in such cases \[[Bloch 2008|AA. Bibliography#Bloch 08]\].

Code Block
if(o instanceof Set) { // Raw type
  Set<?> m = (Set<?>) o; // Wildcard type
  // ...
}

Risk Assessment

Mixing generic and non-generic code can produce unexpected results and exceptional conditions.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ14 OBJ03-J

low

probable

medium

P4

L3

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dcd2110e18d369ef-758af78b-445947b8-a60aa6bf-e0ae90275eb161c2a023b3a1"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 23: "Don't use raw types in new code"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="015ec707d73599ce-e696b4c5-4e2d4386-be23b5f2-edee726d646a08899ed98a3b"><ac:plain-text-body><![CDATA[

[[Bloch 2007

AA. Bibliography#Bloch 07]]

Generics, 1. "Avoid Raw Types in New Code"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2539496d36b91367-cd7593e4-47d74470-9ed583c7-4ad7be85a9dba793a7bc980f"><ac:plain-text-body><![CDATA[

[[Bloch 2005

AA. Bibliography#Bloch 05]]

Puzzle 88: Raw Deal

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3efd4c886a38eac1-377a2e0f-48ec45bf-b054a36d-9426fb0c88aa19833128c72d"><ac:plain-text-body><![CDATA[

[[Darwin 2004

AA. Bibliography#Darwin 04]]

8.3 Avoid Casting by Using Generics

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9b096c799b087337-af563059-4f524377-b276a940-962994675e7144f2a43b4aa7"><ac:plain-text-body><![CDATA[

[[JavaGenerics 2004

AA. Bibliography#JavaGenerics 04]]

 

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b28bd74ad16f2329-f9e48ec5-492a4e1f-b048a92c-24bf0803df82b3df182c964f"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[Chapter 5 "Conversions and Promotions"

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html]

]]></ac:plain-text-body></ac:structured-macro>

 

§4.8 "Raw Types"

 

§5.1.9 "Unchecked Conversion"

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="6f4e3b5b3d2153a0-1325eb28-42d444de-856aafce-96d5d1ccac44bfd5d6e0ace9"><ac:plain-text-body><![CDATA[

[[Langer 2008

AA. Bibliography#Langer 08]]

Topic 3, "[Coping with Legacy

http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Topic3]"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cec67822bfb1f496-77c312df-45f34b58-8f199cdb-3322ffc066215911ae7c59f1"><ac:plain-text-body><![CDATA[

[[Naftalin 2006

AA. Bibliography#Naftalin 06]]

Chapter 8, "Effective Generics"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="156090689a1709c2-9d80db48-45334d7a-9601961a-52089b9383a117eb39caea5b"><ac:plain-text-body><![CDATA[

[[Naftalin 2006b

AA. Bibliography#Naftalin 06b]]

"Principle of Indecent Exposure"

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9b310cbee4eead5c-055cc190-4c98435e-a2238357-b9efd413c334793bef218fb0"><ac:plain-text-body><![CDATA[

[[Schildt 2007

AA. Bibliography#Schildt 07]]

"Create a checked collection"

]]></ac:plain-text-body></ac:structured-macro>

...