The Java compiler type-checks the arguments to each varargs method to ensure that the arguments are of the same type or object reference. However, the compile-time checking is ineffective when {{Wiki Markup Object}} or generic {{T}} parameter types are used \ [[Bloch 2008|AA. References#Bloch 08]\]. Another requirement for providing strong compile-time type checking of variable argument methods is to be as specific as possible when declaring the type of the method parameter.
Noncompliant Code Example (Object)
...
| Code Block | ||
|---|---|---|
| ||
ReturnType method(SpecificObjectType... args) { }
|
...
Retrofitting old methods containing final array parameters with generically typed varargs is not always a good idea. For example, given a method that does not accept an argument of a particular type, it could be possible to override the compile-time checking --- — through the use of generic varargs parameters --- — so that the method would compile cleanly rather than correctly, causing a compile-time error \[ [Bloch 2008|AA. References#Bloch 08]\].
Also, note that autoboxing does not allow strong compile-time type checking of primitive types and their corresponding wrapper classes.
...
Automated detection appears to be straightforward.
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="073baa4a-484a-4153-b1b1-68bb344cb796"><ac:plain-text-body><![CDATA[ | [[Bloch 2008AA. References#Bloch 08]] | Item 42: "Use Varargs Judiciously" | ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7b467cc2-e33b-4f4f-99c8-85fecc29acb2"><ac:plain-text-body><![CDATA[ |
[[Steinberg 2005AA. References#Steinberg 05]] | "Using the Varargs Language Feature" | ]]></ac:plain-text-body></ac:structured-macro> | <ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="56f56ab4-d2a6-4732-8f80-b4abbcfa7aa0"><ac:plain-text-body><![CDATA[ | |
[[Sun 2006AA. References#Sun 06]] [varargs | http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html] ]]></ac:plain-text-body></ac:structured-macro> |
...
DCL52-J. Avoid ambiguous overloading of varargs methods 01. Declarations and Initialization (DCL) DCL57-J. Do not derive a value associated with an enum from its ordinal