Reputation: 28294
I have a Class
object. I want to determine if the type that the Class
object represents implements a specific interface. I was wondering how this could be achieved?
I have the following code. Basically what it does is gets an array of all the classes in a specified package. I then want to go through the array and add the Class objects that implement an interface to my map. Problem is the isInstance()
takes an object as a parameter. I can't instantiate an interface. So I am kind of at a loss with this. Any ideas?
Class[] classes = ClassUtils.getClasses(handlersPackage);
for(Class clazz : classes)
{
if(clazz.isInstance(/*Some object*/)) //Need something in this if statement
{
retVal.put(clazz.getSimpleName(), clazz);
}
}
Upvotes: 113
Views: 86844
Reputation: 6409
A contribution for all the other answers, when possible do not use the most upvoted answer of method isAssignableFrom
, even the "not great" answer of using clazz.getInterfaces()
has better performance than isAssignableFrom
.
A common mistake for developers when looking for an answer to the OP question, is to prefer isAssignableFrom
when an instance is available, wrongly doing this:
if (IMyInterface.isAssignableFrom(myObject.getClass())) {
...
When possible, use IMyInterface.class.isInstance
or instanceof
as both of those have way better performance. Of course, as the OP stated; they have the drawback that you must have an instance and not just the class
.
if (IMyInterface.class.isInstance(myObject)) {
...
if (myObject instanceof IMyInterface) { // +0.2% slower than `isInstance` (*see benchmark)
...
An even faster, but ugly solution would be to store an static Set
with all the "valid" classes instead of checking them, this ugly solution is only preferred when you need to test classes a lot, as its performance outperforms all the other approaches for direct class
check.
public static final Set<Class<?>> UGLY_SET = Stream.of(MyClass1.class, MyClass2.class, MyClass3.class).collect(Collectors.toCollection(HashSet::new));
if (UGLY_SET.contains(MyClass)) {
...
Please visit this answer from users @JBE, @Yura and @aleksandr-dubinsky, credits for them. Also, there's plenty of detail in that answer for the benchmark results to not be valid, so please take a look into it.
Upvotes: 0
Reputation: 11977
You should use isAssignableFrom
:
if (YourInterface.class.isAssignableFrom(clazz)) {
...
}
Upvotes: 261
Reputation: 15184
You can use class.getInterfaces()
and then check to see if the interface class is in there.
Class someInterface; // the interface you want to check for
Class x; //
Class[] interfaces = x.getInterfaces();
for (Class i : interfaces) {
if (i.toString().equals(someInterface.toString()) {
// if this is true, the class implements the interface you're looking for
}
}
Upvotes: 11
Reputation: 740
You can also set the instance adding ".class"
Class[] classes = ClassUtils.getClasses(handlersPackage);
for(Class clazz : classes)
{
if(Interface.class.isAssignableFrom(clazz))
{
retVal.put(clazz.getSimpleName(), clazz);
}
}
Upvotes: 1
Reputation: 12774
you can use the below function to get all the implemented interfaces
Class[] intfs = clazz.getInterfaces();
Upvotes: 19