Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reflection of a implemented interface

Reply
Thread Tools

Reflection of a implemented interface

 
 
homecurr@yahoo.com
Guest
Posts: n/a
 
      05-20-2004
public interface IA{...}
public class A implements IA{...}
public class F{
public F(IA ia){...}
}

If I do this:

A a = new A();
F f = new F(a);

It works fine. But if I do this:

A a = new A();
Class clazz = Class.forName("F");
Constructor constructor = clazz.getConstructor(new Class[]{a.getClass()});

The last line does not work because F does not have a constructor like F(A){}.
Why? How can I fix it? I have to use the reflection in my project.

Thanks,

John
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      05-20-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> public interface IA{...}
> public class A implements IA{...}
> public class F{
> public F(IA ia){...}
> }


[...]

> Class clazz = Class.forName("F");
> Constructor constructor = clazz.getConstructor(new Class[]{a.getClass()});
>
> The last line does not work because F does not have a constructor like F(A){}.
> Why? How can I fix it? I have to use the reflection in my project.


Since you no longer have the compiler to help you at runtime, you'll
need to resolve the desired constructor on your own. That means walking
through the superclasses and superinterfaces of the class in question,
looking for a constructor that works.

Just as is the case with the compiler, it's possible to find an
ambiguous match (for example, two unrelated superinterfaces both match).
The compiler will give an error at compile-time in this case; you need
to decide how to deal with it at runtime.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How is tag interface functionality implemented in Java ? Lew Java 11 09-08-2008 04:00 PM
Automatically inserting method & function headers from an interface that are being implemented with VB.NET Nathan Sokalski ASP .Net 1 11-21-2006 08:32 AM
Where i can find the implemented methods of Statement Interface srinivas.veeranki@gmail.com Java 2 02-08-2006 10:49 PM
How to tell which interface is implemented gimme_this_gimme_that@yahoo.com Java 2 12-11-2004 06:32 AM
one interface implemented by two seperate "modules" Raffael Vogler Java 2 12-18-2003 08:23 AM



Advertisments