如何通过反射确定Java类是否是抽象的 [英] How can I determine whether a Java class is abstract by reflection

查看:36
本文介绍了如何通过反射确定Java类是否是抽象的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Jar 文件中的类进行交互,并希望找到那些不是抽象的.我可以通过实例化类并捕获 InstantiationException 来解决这个问题,但这会影响性能,因为某些类的启动量很大.我在 Class.java 文档中找不到任何明显类似于 isAbstract() 的内容.

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 InstantiationException but that has a performance hit as some classes have heavy startup. I can't find anything obviously like isAbstract() in the Class.java docs.

推荐答案

当您在类对象上调用 getModifiers() 时,它会将抽象作为其修饰符之一.

It'll have abstract as one of its modifiers when you call getModifiers() on the class object.

这个 link 应该帮助.

 Modifier.isAbstract( someClass.getModifiers() );

还有:

http://java.sun.com/javase/6/docs/api/java/lang/reflect/Modifier.html

http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getModifiers()

这篇关于如何通过反射确定Java类是否是抽象的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆