Separate Class/Method level Generics Interpretations

Neal Gafter neal at gafter.com
Sat Sep 26 20:34:52 UTC 2009


Quentin-

By definition, the members of a RAW type (a generic type without type
parameters) are the ERASED members of the class.  This is not affected by
whether or not the signature of the member uses the type parameters of the
class.  Making it behave as you seem to expect would significantly
complicate the type system.  If you don't want the erasing to happen, then
use the wildcard <?> version of the generic class.

Generally speaking, you shouldn't expect generics to "work" in the presence
of raw types.  It is better not to mix raw and generic code.

Cheers,
Neal

On Sat, Sep 26, 2009 at 12:51 PM, Quintin Beukes <quintin at last.za.net>wrote:

> Hey,
>
> When you have parameterized methods in a parameterized class, and you
> don't use the parameters of the class, then the parameters of all it's
> methods are also ignored, even when not related. Is there any specific
> reason why it was done like this? If not, I would like to implement
> it, as I don't see why you have to loose the method's generic ability
> when you don't use that of the class.
>
> Take for instance, this example:
> class AnnotatedMethod
> {
>  public <M> M annotatedMethod(M obj)
>  {
>    return obj;
>  }
> }
>
> class AnnotatedClass<C>
> {
>  public <M> M annotatedMethod(M obj)
>  {
>    return obj;
>  }
> }
>
> public class AnnotationDemo
> {
>  public static void main(String[] args) throws Exception
>  {
>    AnnotatedMethod method = new AnnotatedMethod();
>    String s1 = method.annotatedMethod("string");
>
>    AnnotatedClass<?> clazzWorking = new AnnotatedClass();
>    String s2 = clazzWorking.annotatedMethod("string");
>
>    AnnotatedClass clazzNotWorking = new AnnotatedClass();
>    String s3 = clazzNotWorking.annotatedMethod("string");
>  }
> }
>
> Quintin Beukes
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20090926/7125b58c/attachment.html>


More information about the core-libs-dev mailing list