tyutNo4 / findbugs

Automatically exported from code.google.com/p/findbugs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Impossible downcast of toArray() result to org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ITypeConstraint[] in create(SingleVariableDeclaration) in FullConstraintCreator.java

GoogleCodeExporter opened this issue · comments

Bug report generated from FindBugs
Impossible downcast of toArray() result to 
org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ITypeConstraint[] 
in create(SingleVariableDeclaration)

In class 
org.eclipse.jdt.internal.corext.refactoring.typeconstraints.FullConstraintCreato
r
  In method create(SingleVariableDeclaration)
  Actual type Object[]
  Expected org.eclipse.jdt.internal.corext.refactoring.typeconstraints.ITypeConstraint[]
  Return value of java.util.List.toArray()
  At FullConstraintCreator.java:[line 393]
Classified as: MOSTLY_HARMLESS
test evaluation
Bug pattern explanation:
This code is casting the result of calling toArray() on a collection to a type 
more specific than Object[], as in: 
String[] getAsArray(Collection<String> c) {
  return (String[]) c.toArray();
  }
This will usually fail by throwing a ClassCastException. The toArray() of 
almost all collections return an Object[]. They can't really do anything else, 
since the Collection object has no reference to the declared generic type of 
the collection. 
The correct way to do get an array of a specific type from a collection is to 
use c.toArray(new String[]); or c.toArray(new String[c.size()]); (the latter 
is slightly more efficient). 
There is one common/known exception exception to this. The toArray() method of 
lists returned by Arrays.asList(...) will return a covariantly typed array. 
For example, Arrays.asArray(new String[] { "a" }).toArray() will return a 
String 
[]. FindBugs attempts to detect and suppress such cases, but may miss some.



FindBugs issue identifier (do not modify or remove): 
2c503602f300bdff0bb2465d0b3c7f2a

Original issue reported on code.google.com by keithl on 11 Feb 2010 at 12:17

Original comment by keithl on 11 Feb 2010 at 12:17

  • Changed state: Invalid