2010-11-09 7 views
9

Ich erhalte die folgende Warnung von meiner NetBeans IDE.Verdächtiger Aufruf von java.util.Collection.contains

Suspicious call to java.util.Collection.contains 
Expected type T, actual type Object 

Darf ich wissen, was das bedeutet?

Das ergibt für mich keinen Sinn. Die Methoden List und Collection der Klasse contains verwenden Object als ihren Methodenparameter.

import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Iterator; 
import java.util.List; 

/** 
* 
* @author yan-cheng.cheok 
*/ 
public abstract class AbstractCollection<T> implements Collection<T> { 

    protected List<T> list = new ArrayList<T>(); 

    public boolean contains(Object o) { 
     // Suspicious call to java.util.Collection.contains 
     // Expected type T, actual type Object 
     return list.contains(o); 
    } 

-Code-Schnipsel aus Klasse Collection

/** 
* Returns <tt>true</tt> if this collection contains the specified element. 
* More formally, returns <tt>true</tt> if and only if this collection 
* contains at least one element <tt>e</tt> such that 
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>. 
* 
* @param o element whose presence in this collection is to be tested 
* @return <tt>true</tt> if this collection contains the specified 
*   element 
* @throws ClassCastException if the type of the specified element 
*   is incompatible with this collection (optional) 
* @throws NullPointerException if the specified element is null and this 
*   collection does not permit null elements (optional) 
*/ 
boolean contains(Object o); 

Code-Snippet aus List-Klasse

/** 
* Returns <tt>true</tt> if this list contains the specified element. 
* More formally, returns <tt>true</tt> if and only if this list contains 
* at least one element <tt>e</tt> such that 
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>. 
* 
* @param o element whose presence in this list is to be tested 
* @return <tt>true</tt> if this list contains the specified element 
* @throws ClassCastException if the type of the specified element 
*   is incompatible with this list (optional) 
* @throws NullPointerException if the specified element is null and this 
*   list does not permit null elements (optional) 
*/ 
boolean contains(Object o); 

Antwort

8

Im Aufruf von list.contains vergleichen Sie ein Objekt mit einem Typ T. Wenn Sie o drücken, wird Ihre Warnung aufgelöst.

+3

Wäre es nicht besser, die 'Object' Deklaration zu' T' zu ändern? – Bobby

+0

Ja, du hast recht, fest. – dpsthree

+3

@Bobby: Eigentlich ist es eine Implementierung der abstrakten Methode definiert in 'Collection' Schnittstelle. Daher müssen wir bei der Erklärung bleiben. –

0

Der Aufruf der contains-Methode mit einem Objekt anstelle des generischen Typs kann ein Programmierfehler sein. Da der Code immer noch gültig ist, zeigt der Compiler nur eine Warnung an.

Ein Beispiel, warum diese Warnung notwendig ist:

List<Long> l = new ArrayList<Long>(); 
l.add(1l); 
l.contains(1); 

Der Code gültig ist, aber würde immer false zurück. Ein Fehler, der normalerweise versteckt wird, enthält ein akzeptierendes Objekt anstelle eines generischen Typs, so dass der Compiler auf Warnungen beschränkt ist.

Da es gültige Anwendungsfälle für die Übergabe eines Objekts gibt, sollten Sie eine @SuppressWarnings() -Anmerkung verwenden können, um diese Warnung auszublenden (tun Sie dies nur, wenn Sie wissen, was Sie tun).