You should use isAssignableFrom:
if (YourInterface.class.isAssignableFrom(clazz)) {
...
}
Answer from Flavio on Stack Overflowreflection - Java - Check if an object (class) implements an interface - Stack Overflow
java - How to check if an object implements an interface? - Stack Overflow
class - How to check if object implements particular interface, but not the descendants of this interface, in Java - Stack Overflow
java - Is it bad programming practice to check if a class referenced by its interface is an instance of another class? - Software Engineering Stack Exchange
The instanceof operator does the work in a NullPointerException safe way. For example:
if ("" instanceof java.io.Serializable) {
// it's true
}
yields true. Since:
if (null instanceof AnyType) {
// never reached
}
yields false, the instanceof operator is null safe (the code you posted isn't).
instanceof is the built-in, compile-time safe alternative to Class#isInstance(Object)
This should do:
public static boolean implementsInterface(Object object, Class interf){
return interf.isInstance(object);
}
For example,
java.io.Serializable.class.isInstance("a test string")
evaluates to true.
If you need to know if instance foo is of a class implementing a given interface, you can use instanceof:
if (foo instanceof TheInterface)
You can use the instanceof operator in Java. JLS says,
At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException.
instanceof operator is a binary operator used to test if an object (instance) is a sub type of a given type.
// checks if bar object's class implements SomeInterface
if (bar instanceof SomeInterface)
You can use http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getInterfaces()
but to me the thing you are trying to do is like patching up badly written app. To me, the better way is creating a new interface (which only desired object will implement) and making the "tree structure" nodes require that particular interface.
You can get the list of implemented interfaces using getInterfaces.
Assuming you already casted your instance to the desired interface, you just have to test that yourInstance.getClass().getInterfaces().length==1
Yes this is indeed a bad practice. You are using an interface to abstract your code from the implementation. When you work with the interface and access the implementation (via casting) you are violating the interface definition.
To solve this kind of problem extend your interface by another method, that you would access from within your code.
public Timable[] findObjectsWithTarget(Cue target) {
ArrayList<Timable> result = new ArrayList<Timable>();
for (Timable timed : fireable) { //fireable is the array (actually a HashSet) of Timed objects
if (timed.hasTarget(target))
result.add(timed);
}
return (Timable[]) result.toArray();
}
You have to implement that method also in Timerclass but you can return false per default.
Yes, it's a bad practice, because you're adding functionality to the Timeable interface that you're not specifically declaring, namely the possibility of also being a Trigger. There are a few different ways to do it better:
- Explicitly add the functionality to the
Timeableinterface, as in woni's answer. This is better than usinginstanceof, but still violates the interface segregation principle. - Keep separate lists of
TimeableandTriggerobjects in the calling code, or at the very least, only keep a combined list in contexts where theTriggerfunctionality isn't necessary. - Keep a back reference from your
targetinto all theTimeableobjects that use it. In other words, calltarget.getTriggers()instead oftimeableList.findObjectsWithTarget(target). Often when a method feels awkward, it's because you've put it into the wrong class. The fact that "a lot of methods" need to do this search means this approach would also have significant time efficiency benefits.