How can I determine whether a Java class is abstract by reflection

前端 未结 3 689
自闭症患者
自闭症患者 2020-12-02 14:54

I am interating through classes in a Jar file and wish to find those which are not abstract. I can solve this by instantiating the classes and trapping InstantiationExceptio

3条回答
  •  醉酒成梦
    2020-12-02 15:47

    public static boolean isInstantiable(Class clz) {
        if(clz.isPrimitive() || Modifier.isAbstract( clz.getModifiers()) ||clz.isInterface()  || clz.isArray() || String.class.getName().equals(clz.getName()) || Integer.class.getName().equals(clz.getName())){
            return false;
        }
        return true;
    }
    

提交回复
热议问题