Generics and forName()



If I have this class:

  class A {
    public A(String a);
  }

Is it possible to make an instance through reflection without getting a
compiler warning (java 1.5)?

The following gives an unchecked warning in the first line due to the
forced cast. Using "?" just postpone the the problem:

  Class<A> clazz = (Class<A>) Class.forName("A");
  Constructor<A> ctor = clazz.getConstructor(String.class);
  A a = ctor.newInstance("hello world");

The problem is that in my organization we treat warnings as errors and
the above simply isn't doable within such a regime.

What is the workaround?

(And why whould all the above be possible (by Class<?> and Class.newInstance()),
if just the constructor didn't take any arguments?)

Thanks!

.



Relevant Pages

  • seeking advice on building jdk15
    ... I discovered I needed Java first. ... warning: non-varargs call of varargs method with inexact argument type ... cast to java.lang.Objectfor a non-varargs call and to suppress this ... http://mail.yahoo.com Java HotSpotClient VM warning: Can't detect initial thread stack location ...
    (freebsd-questions)
  • Re: How to save the Firewall Zone setup for wireless Interface setting
    ... Executing /sbin/conf.d/SuSEconfig.fonts... ... Warning: cannot find a sans serif Japanese font. ... Japanese in Java ...
    (alt.os.linux.suse)
  • Re: java install question
    ... Java HotSpotClient VM warning: Can't detect initial thread stack location ... cast to java.lang.Objectfor a non-varargs call and to suppress this warning ...
    (comp.unix.bsd.freebsd.misc)
  • Re: Generics and forName()
    ... > compiler warning? ... > The following gives an unchecked warning in the first line due to the ... warning, due to the contrived nature of the Java generics implementation, so ... public static void main(final String... ...
    (comp.lang.java.programmer)
  • Java error.
    ... Java HotSpotClient VM warning: ... cast to java.lang.Objectfor a non-varargs call and to suppress this ...
    (freebsd-questions)