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
*OBJ10-EX1* 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]\].

...

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

OBJ10-J

low

probable

medium

P4

L3

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="669603a772e62881-82ea96c6-49ea45dc-90649027-f2ecf1d8b19dfc7ee893c2ea"><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="b50badc6ca14b019-29aca856-49cd49d6-844382b1-a5eca4fc3429ece3379a21c5"><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="51fa5b05f524a423-b2118710-4cc44d99-b9fbbfbe-053324dde416c7debaeb664e"><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="ed3c19379f2f9f4c-e6796855-411a4703-91c6ac5f-a290c8bf780ecd7e195cf184"><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="64a09abd11fda1a9-52727dee-4bed491a-b20b8654-81b0d4dbbebdf714e9472148"><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="b3fffa951647760d-0e81d8e0-4461497c-a6ca87eb-161ac9b62f252f8737435f17"><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="976ef127aec0e252-1b461a08-488e4588-85a6b5a8-dd0c871b109ab6c86451b2f3"><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="9d898176b29f9db0-d79b8eeb-46224b33-951dbc2e-bb3b88d3685bb410cd154da9"><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="0e652d5cae3ac459-b1003cc3-47314340-a598b5e2-23dd9e426bf40e182b9801ed"><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="9ecb4418408a054a-d944cc4d-4ef14185-b7b9a41d-7c882dcba28d89d95520fcba"><ac:plain-text-body><![CDATA[

[[Schildt 2007

AA. Bibliography#Schildt 07]]

"Create a checked collection"

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

...