简体   繁体   中英

How to check if the “?” of a Class<?> object instanceof SomeInterface

Simple- looking -problem, but I'm starting to think I'm trying to achieve something the wrong way. Let's say I have a Method object correctly initialized.

I need to check if that method will return an object implementing the Comparable interface.

The problem is method.getReturnType() returns a Class<?> object, I want to check if this "?" actually is an instance of Comparable, but I can't write ? instanceof Comparable ? instanceof Comparable , how would anyone do that?

EDIT: I know I could do result = method.invoke(someObject) and then result instanceof Comparable but I need to do these kind of checks in the constructor of my bigger object, dunno if I'm clear.

Comparable.class.isAssignableFrom(method.getReturnType())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM