Separate Class/Method level Generics Interpretations

Quintin Beukes quintin at last.za.net
Sat Sep 26 19:51:51 UTC 2009


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



More information about the core-libs-dev mailing list