Trying to iterate through Class[ ] comparing using instanceof

I'm trying to iterate through an array of Class to determine if candidate object is 'interesting'. A, B, C, D don't have an appropriate parent class (they are siblings, but the not all siblings are interesting). I can't add a parent class nor can I modify A, B, C, D. I want to do something like the below, but the syntax isn't legal.

private static final Class[] types = { A.class, B.class, C.class, D.class};
....
void method(Object cand) {
    for (Class c : types) {
        if (cand instanceof c) {//cand is interesting, but c isn't type
             ( (c) cand).methodInInterestingClasses();
        }
    }
}
Jon Skeet
people
quotationmark

You can only use the instanceof with the name of a type, known at compile-time.

You can fix the first part using the Class.isInstance method:

if (c.isInstance(cand))

... but that's not going to help you with methodInInterestingClasses(), which it sounds like you'll have to call with reflection.

people

See more on this question at Stackoverflow