Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

"illegal generic type for instanceof", but I'm not using generics

843793May 28 2008 — edited May 29 2008
I am wrapping the BlockingQueue to make it accept null values as shown in the code I post below. The line that reads "if (val instanceof NullValue) " gives me "Illegal generic type for instanceof". val is a variable of type Object, and NullValue is a class that derives from Object. Why am I getting this error? Granted I am using generics, but not on this line. I would understand it if I had written "if (val instanceof E)" but in this case I don't think this error makes sense. Could it be a compiler bug?
public class PipelineQueue<E extends AbstractDataType>
        implements InputBuffer<E>, OutputBuffer<E> {

    private ArrayBlockingQueue instance;

    private class NullValue {
    };

    @Override
    public E take() throws InterruptedException {
        Object val = instance.take();
        if (val instanceof NullValue) {
            return null;
        } else {
            return (E) val;
        }
    }

    public int size() {
        return instance.size();
    }

    public int remainingCapacity() {
        return instance.remainingCapacity();
    }

    @Override
    public void put(E e) throws InterruptedException {
        if (e == null) {
            instance.put(new NullValue());
        } else {
            instance.put(e);
        }
    }

    @Override
    public String toString() {
        return instance.toString();
    }

    public Iterator iterator() {
        return instance.iterator();
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 26 2008
Added on May 28 2008
9 comments
1,771 views