When will reflections support casting (especially for constructors, etc) properly?



Consider the following code:

class A
{
}

class B extends A
{
}

class C
{
public void foobar(A a)
{
}
}

....

Object b=Class.forName("B").newInstance();

Class clazzA=Class.forName("A");
Class clazzC=Class.forName("C");
Object c=clazz.newInstance();

Method m=clazzC.getMethod("foobar", clazzA);
m.invoke(c, b);

This will result in an exception, since b is not of type A (the extend
is not considered). Anyway around this? Clearly typecasting will do,
but can't figure out how to typecast using reflection if you only have
the class string "A" as a reference of the target typecast class.

Also the private/public/protected modifier for the method (doSomething,
in this example) MUST be public (even if reflection code lives within
the class C), any around this?


PS sorry about the cheezy class names, my client does not allow
specific example code to be posted to the web.
bvh

.



Relevant Pages

  • Re: Thread doesnt interrupt!? HELP please
    ... I took a boolean "stopUpdate" as nos proposed to do. ... "isUpdating" to false. ... public class MyClass extends JFrame{ ... public void updateThumbs() { ...
    (comp.lang.java.programmer)
  • Re: Compiler bug? "reference to addAll is ambiguous"
    ... extends E>) in java.util.List<capture#420 ... super gov.sc.eip.report.birt.items.ChangeRow> match ... I assume E extends ChangeRow. ... public void addAll(Collection<? ...
    (comp.lang.java.programmer)
  • Re: Return pressed event
    ... > public class Frame1 extends JFrame implements ActionListener, ... public void actionPerformed ... Sun Certified Developer for the Java 2 Platform ...
    (comp.lang.java.programmer)
  • Re: When will reflections support casting (especially for constructors, etc) properly?
    ... class B extends A ... public void foobar ... Even once I got it to compile, it doesn't result in an exception ... The idea of private method is such that objects not of the same class cannot access those methods. ...
    (comp.lang.java.programmer)