Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Generically typed code can be freely used with raw types when attempting to preserve compatibility between nongeneric legacy code and newer generic code. Using raw types with generic code causes most Java compilers to issue "unchecked" warnings but still compile the code. When generic and nongeneric types are used together correctly, these warnings can be ignored; at other times, these warnings can denote potentially unsafe operations.

Wiki MarkupAccording to the _Java Language Specification_, [§4, §4.8, "Raw Types,"|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.8] \[ [JLS 2005|AA. References#JLS 05]\]:

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

...

Code Block
List l = new ArrayList();
List<String> ls = l; // Produces unchecked warning

Wiki MarkupIt is insufficient to rely on unchecked warnings alone to detect violations of this rule. According to the _Java Language Specification_, [&#xA7;4, §4.12.2.1, "Heap Pollution," |http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#111088] \ [[JLS 2005|AA. References#JLS 05]\]:

Wiki MarkupNote that this does not imply that heap pollution only occurs if an unchecked warning actually occurred. It is possible to run a program where some of the binaries were compiled by a compiler for an older version of the Java programming language, or by a compiler that allows the unchecked warnings to suppressed _\[sic\]_. This practice is unhealthy at best.

Extending legacy classes and making the overriding methods generic fails because this is disallowed by the Java Language Specification..

...

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 nongeneric code.

Exceptions

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

Wiki Markup*OBJ03-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. References#Bloch 08]\].

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

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ03-J

low

probable

medium

P4

L3

Bibliography

...

[[Bloch 2008AA. References#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="dff2162f-f133-4db4-92f9-ca9873b8481b"><ac:plain-text-body><![CDATA [ [[Bloch 2007AA. References#Bloch 07]] ]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9d9c37f2-6aa8-4af1-bd13-074c10a8c92f"><ac:plain-text-body><![CDATA[

[ [Bloch 2005AA. References#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="c9974c17-2059-493e-bf01-0ecffd5130f3"><ac:plain-text-body><![CDATA[

[ [Darwin 2004AA. References#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="b1dec99c-0f9a-48f2-8dbb-62f7f2bcd81b"><ac:plain-text-body><![CDATA[

[ [JavaGenerics 2004AA. References#JavaGenerics 04] ]

 ]]></ac:plain-text-body></ac:structured-macro><ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="666326f0-f7b1-428a-aafd-2f8c60b0a8b7"><ac:plain-text-body><![CDATA

[ [[JLS 2005AA. References#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="0a494fd4-ca1d-4821-a9ac-c6bc91449051"><ac:plain-text-body><! [CDATA[ [[Langer 2008AA. References#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="29a3002f-27eb-4f9a-87a7-9d72c2d00bcb"><ac:plain-text-body><![CDATA

[ [[Naftalin 2006AA. References#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="97343d6a-da57-4b6a-8501-916abb42be11"><ac:plain-text-body><![CDATA[

[[Naftalin 2006bAA. References#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="255b1e91-5970-424c-af85-87637e3e333f"><ac:plain-text-body><![CDATA[

[[Schildt 2007AA. References#Schildt 07]]

Create a checked collection ]]></ac:plain-text-body></ac:structured-macro>

...

      04. Object Orientation (OBJ)