Board index » jbuilder » Implemented intefaces

Implemented intefaces


2003-10-22 03:52:26 AM
jbuilder3
I am not sure if this is a bug (in Java API) or my misunderstanding on how
Reflection API is suppose to work. The problem that I am facing is to
determine if a class implements a specific interface. If it does then some
processing on that object will occur (like calling a method implemented by
the interface).
My code looks something like:
Class exClass = ex.getClass();
Class[] implInterfaces = exClass.getInterfaces();
// Determine if class implements specific interface
try{
if (isSpecificInterface(implInterfaces)){
ex.methodImplementedbyInterface();
}
boolean isSpecificInterface(Class[] interfaces){
boolean found = false;
for(int i=0; i<interfaces.length; i++){
if (interfaces[i].getClass().getName().equals("SPECIFIC
INTEFACE")){
found = true;
break;
}
}
return found;
}
The problem is that sometimes the object (ex) is a class that extends a
base class. The base class implements the desired interface (as will the
object - by inheritance). However what I am finding is that the interfaces
reported for object ex, does NOT include the desired interface. Instead, to
get this to work, I have to test the interfaces implemented by the
superclass as well. Is this a bug within the Reflection API or is this the
desired functionality?
After reflecting on this problem (okay I know it is a bad pun) I realize I
could also test to see if the method (defined by the specific interface) is
implemented by the class (object ex, or super class).
I expected that no matter what class/object, the getInterfaces() would
return all interfaces implemented by the class or any ancestor classes. Is
this not true?
 
 

Re:Implemented intefaces

"David Baginski" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote
...
...
The problem is that sometimes the object (ex) is a class that extends
a
base class. The base class implements the desired interface (as will the
object - by inheritance). However what I am finding is that the
interfaces
reported for object ex, does NOT include the desired interface. Instead,
to
get this to work, I have to test the interfaces implemented by the
superclass as well. Is this a bug within the Reflection API or is this
the
desired functionality?

After reflecting on this problem (okay I know it is a bad pun) I realize I
could also test to see if the method (defined by the specific interface)
is
implemented by the class (object ex, or super class).

I expected that no matter what class/object, the getInterfaces() would
return all interfaces implemented by the class or any ancestor classes.
Is
this not true?

I think the emphasis for "getInterfaces()" lies on "...Determines the
interfaces *implemented by the class* or interface *represented by this
object*". The javadoc for getInterfaces() doesn't mention that interfaces
from inherited objects are taken into account too. So all in all the
following seems to be based on how your exact interpretation of this is:
"You invoke the getInterfaces method to determine which interfaces a class
implements. ..... Each Class object in the array returned by getInterfaces
represents one of the interfaces implemented by the class."
So the way you have to go seems to be indeed:
- Load a class
Class c = Class.forName ("Classname")
- Determine if it's a class or interface
c.isInterface ()
- Determine lineage e.g. superclass/superinterfaces
+ Superclass
Class c1 = c.getSuperclass ()
+ Superinterface
Class[] c2 = c.getInterfaces ()
- Determine implemented interfaces
Class[] c2 = c.getInterfaces ()
-Valentino
 

Re:Implemented intefaces

Addon: just remembered some article I once read about a similar theme, so
maybe you also want to read this article:
www.javaworld.com/javatips/jw-javatip113_p.html
-Valentino
 

{smallsort}

Re:Implemented intefaces

Would it be possible for you to rewrite your code in terms of
Class.isAssignableFrom()? That will probably simplify things considerably
if all you're trying to do is determine interface compatibility.
--
Check out our latest white papers at
www.datadevelopment.com/papers/index.html
BladeNET Scores With Borland Enterprise Tools
Team Development with JBuilder and Borland Enterprise Server
Dolphin Data Development Ltd.
www.datadevelopment.com/
"David Baginski" < XXXX@XXXXX.COM >wrote in message
Quote
I am not sure if this is a bug (in Java API) or my misunderstanding on how
Reflection API is suppose to work. The problem that I am facing is to
determine if a class implements a specific interface. If it does then some
processing on that object will occur (like calling a method implemented by
the interface).
My code looks something like:
Class exClass = ex.getClass();

Class[] implInterfaces = exClass.getInterfaces();
// Determine if class implements specific interface
try{
if (isSpecificInterface(implInterfaces)){
ex.methodImplementedbyInterface();
}


boolean isSpecificInterface(Class[] interfaces){
boolean found = false;
for(int i=0; i<interfaces.length; i++){
if (interfaces[i].getClass().getName().equals("SPECIFIC
INTEFACE")){
found = true;
break;
}
}
return found;
}

The problem is that sometimes the object (ex) is a class that extends
a
base class. The base class implements the desired interface (as will the
object - by inheritance). However what I am finding is that the
interfaces
reported for object ex, does NOT include the desired interface. Instead,
to
get this to work, I have to test the interfaces implemented by the
superclass as well. Is this a bug within the Reflection API or is this
the
desired functionality?

After reflecting on this problem (okay I know it is a bad pun) I realize I
could also test to see if the method (defined by the specific interface)
is
implemented by the class (object ex, or super class).

I expected that no matter what class/object, the getInterfaces() would
return all interfaces implemented by the class or any ancestor classes.
Is
this not true?