0

I've got an IModelElement interface, and several classes implementing that interface. Somewhere in my code there's the following statement:

Object childClass = request.getNewObjectType();

Here getNewObjectType() returns a Class object. I need to check if that Class objects represents a class which implements the IModelElement interface, anyone knows how is this achieved?

roirodriguez
  • 1,685
  • 2
  • 17
  • 31

4 Answers4

5

Class.isAssignableFrom(), and if it's a Class, then request.getNewObjectType() should return Class, not Object.

if (IModelElement.isAssignableFrom((Class) childClass)) {
    // whatever
}
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • Thanks a lot... I can't declare childClass as Class however, as request.getNewObjectType() returns Object (effectively returning always a Class instance with the request interface implementation i'm using; i think the base request interface declares that function as returning an object to be compatible with any Class return type without using generics. What if i change the request implementation used?). Thank you for the comment, let me know if what i argue does not make sense, i'm a complete newbie with java... – roirodriguez Sep 15 '11 at 13:51
  • I'm not sure I understand your interface/generics problem. If `getNewObjectType()` always returns a Class object, there's no reason for it to return Object. You can use Class without generics as easily as anything else. – Ryan Stewart Sep 15 '11 at 14:39
  • Ok... It still remains the latter problem (implementation change of the request interface: request is a factory returning Object(s), with the actual implementation it always returns Class objects. All those interfaces and implementations are from an external API, i'm not sure if all factory implementations return always Class objects... I think casting in the if statement is always compatible, but declaring childClass as Class might not be compatible with factory implementations returning Object(s) of other type than Class, i'm i right? – roirodriguez Sep 15 '11 at 14:46
  • Okay, I see. Yes, if other implementations of getNewObjectType() can return things besides class, you're pretty much stuck; however, it suggests an incorrect abstraction. Usually you only deal directly with Object at the framework level. – Ryan Stewart Sep 15 '11 at 20:39
1

You can just try to cast the return value of request.getNewObjectType() to IModelElement. If there is not ClassCastException the returned object is of a class which implements the interface.

try {
  IModelElement temp = (IModelElement) request.getNewObjectType();
} catch (ClassCastException e) {
  // the interface is not implemented
}
fgysin
  • 11,329
  • 13
  • 61
  • 94
  • It works, but it's not a great solution when there are alternatives. CCE is one of those exceptions, like NullPointerException, that you just don't want to catch. – Ryan Stewart Sep 15 '11 at 12:12
  • I agree that there are more beautiful solutions - didn't know about `.isAssignableFrom(..)` before. I don't agree however, that catching CCEs is bad in every case. – fgysin Sep 15 '11 at 12:19
  • Now that you put your finger on it - no. At least not since we have generics... :) – fgysin Sep 15 '11 at 12:34
  • @fgysin: Catching exceptions can be *very* slow: http://stackoverflow.com/questions/299068/how-slow-are-java-exceptions – Matthijs Bierman Sep 18 '11 at 14:04
0

Using the instanceof operator?

if(childClass instanceof Class) {
  (Class) child = (Class) childClass;
  child.method();
}

http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Matthijs Bierman
  • 1,720
  • 9
  • 14
  • 1
    The questions isn't whether childClass is a Class instance. It's whether that Class implements a known interface. – Ryan Stewart Sep 15 '11 at 12:14
0

Try:

Object childClass = request.getNewObjectType();
if (childClass instanceof Class) {
    for (Class interfaceClass in ((Class)childClass).getInterfaces()) {
        if (interfaceClass.getCanonicalName().equals("org.xx.YourInterface")) {
            // Do some stuff.
        }
    }
}
David Oliván
  • 2,717
  • 1
  • 19
  • 26
  • 1
    I think `in` = `:`, and that's a ton of work for something simple, not to mention losing some compile-time type checking by referring to the interface by name. – Ryan Stewart Sep 15 '11 at 12:13