Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
brian.vanheesch@gmail.com - 31 Jan 2006 12:03 GMT
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
Timbo - 31 Jan 2006 12:53 GMT
> Consider the following code:
>
[quoted text clipped - 28 lines]
> 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.

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.

> 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?

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.
Larry Barowski - 31 Jan 2006 19:17 GMT
> 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?

Method.setAccessible(boolean)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.