Re: When will reflections support casting (especially for constructors, etc) properly?
- From: Timbo <timbo@xxxxxxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 12:53:17 +0000
brian.vanheesch@xxxxxxxxx wrote:
You should try running this before posting... it doesn't compile. Even once I got it to compile, it doesn't result in an exception for me.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.
I suspect you mean "foobar" and not "doSomething", but I don't see why you would want it any other way. The idea of private method is such that objects not of the same class cannot access those methods.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?
.
- References:
- When will reflections support casting (especially for constructors, etc) properly?
- From: brian . vanheesch
- When will reflections support casting (especially for constructors, etc) properly?
- Prev by Date: Re: Any Checkstyle users?
- Next by Date: Re: Any Checkstyle users?
- Previous by thread: When will reflections support casting (especially for constructors, etc) properly?
- Next by thread: Re: When will reflections support casting (especially for constructors, etc) properly?
- Index(es):
Relevant Pages
|
|