开发者

Java Generics Bound mismatch with a raw type

I have a super class with a signature like

public abstract class Foo<C extends Comparable<? super C>>{..}

So the C class is supposed to be a Comparable object.

I want to use org.joda.time.Instant (version 1.6) as the type parameter in a subclass

public class SubFoo<Instant>

unfortunately i get this error:

Bound mismatch: The type Instant is not a valid substitute for the bounded parameter <C extends Comparable<? super C>> of the type BigtablePoller<T,C>

The Instant class extends AbstractInstant which implements Comparable (no type parameters)

Is there any way around this issue?

The only way i have been able to make it work is to change Foo to:

@SuppressWarnings("rawtypes")
public abstract class Foo<C extends开发者_如何学C Comparable> {}

I would like to avoid the warning and Josh Bloch says not to use raw types in new code (effective java Item 23).


Just use this:

public abstract class Foo<C extends Comparable<C>>

The reason your version doesn't work is that Instant is defined as extends Comparable<Instant> not extends Comparable<? super Instant>. If you use bounding, the bounding has to match too.


Joda time 1.6 doesn't support Generics so there are three options.

  1. Create a wrapper class that implements the correct interface and use that.
  2. Suppress Warnings on the super class and deal with the uglyness
  3. upgrade to joda time 2.0 which supports the generic comparable interface on Instant.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜