Java: How to check if an object is an instance of a non-static inner class, regardless of the outer object?

后端 未结 6 1066
遥遥无期
遥遥无期 2021-02-05 10:35

If I have an inner class e.g.

class Outer{
    class Inner{}
}

Is there any way to check if an arbitrary Object is an instance of

6条回答
  •  遥遥无期
    2021-02-05 10:46

    I was googling for finding out better answers, to find out that there are none out there.

    Here is what I have which works pretty well:

        public static boolean isStatic(Class klass) {
                return Modifier.isStatic(klass.getModifiers());
        }
    
        /**
         * Non static inner class
         */
        public static boolean isInnerclass(Class klass) {
                return klass.getDeclaringClass() != null && !isStatic(klass);
        }
    

    Will return true for local inner classes. isMemberClass and others do not work for this purpose.

提交回复
热议问题