使用Java反射机制,你可以在运行时检视Java类。检视Java类通常是你使用反射机制的第一步。从类对象你可以获取到如下信息
- Class Name(类名称)
- Class Modifies (类修饰 public, private, synchronized etc.)
- Package Info (包信息)
- Superclass (父类)
- Implemented Interfaces (实现接口)
- Constructors (构造函数)
- Methods (方法)
- Fields (变量)
- Annotations (注解)
还有其它一些和Java类相关的信息。完整的列表可以查看 JavaDoc for java.lang.Class。本文将简单的描述上面列出来的信息的获取,部分主题也会在后续单独文章中进行详细说明。举个例子,本文将向你展示如何获取全部方法或特定方法,同时会有一篇单独的文章向你展示如何调用方法、如何通过给定参数集合来从一组相同方法名的方法集中查找匹配的方法、通过反射调用方法会抛什么异常、如何查找 getter/setter
方法等。本文主要介绍Class
对象,以及通过它你可以获取的信息。
The Class Object
在对一个类做任何检视之前,我们需要首先获取一个Class
对象,在Java中任何类型,包括基本类型(int,float,long etc)、数组都有与之关联的类对象。如果在编译阶段你就知道一个类的名称,你可以像下面的代码一样来获取一个类对象:
<pre>
Class myObjectClass = MyObject.class
</pre>
如果你在编译的阶段不知道类的名称,但是在运行时有字符串形式的类名,那么可以像下面的代码一样来获取一个类对象
<pre>
String className = ... //obtain class name as string at runtime
Class class = Class.forName(className);
</pre>
在使用Class.forName()
时,你必须确保使用了类名称的完整形式,也就是包含有包名的类名称。比如,如果类MyObject
位于com.jenkov.myapp
包路径下,那么完整的类名称为com.jenkov.myapp.MyObject
。
如果在运行时的classpath中找不到对应的类,Class.forName()
方法可能会抛出ClassNotFoundException
异常。
Class Name
From a Class
object you can obtain its name in two versions. The fully qualified class name (including package name) is obtained using the getName()
method like this:
Class aClass = ... //obtain Class object. See prev. section String className = aClass.getName();
If you want the class name without the pacakge name you can obtain it using the getSimpleName()
method, like this:
Class aClass = ... //obtain Class object. See prev. section String simpleClassName = aClass.getSimpleName();
Modifiers
You can access the modifiers of a class via the Class
object. The class modifiers are the keywords "public", "private", "static" etc. You obtain the class modifiers like this:
Class aClass = ... //obtain Class object. See prev. section int modifiers = aClass.getModifiers();
The modifiers are packed into an int
where each modifier is a flag bit that is either set or cleared. You can check the modifiers using these methods in the class java.lang.reflect.Modifier
:
Modifier.isAbstract(int modifiers) Modifier.isFinal(int modifiers) Modifier.isInterface(int modifiers) Modifier.isNative(int modifiers) Modifier.isPrivate(int modifiers) Modifier.isProtected(int modifiers) Modifier.isPublic(int modifiers) Modifier.isStatic(int modifiers) Modifier.isStrict(int modifiers) Modifier.isSynchronized(int modifiers) Modifier.isTransient(int modifiers) Modifier.isVolatile(int modifiers)
Package Info
You can obtain information about the package from a Class
object like this:
Class aClass = ... //obtain Class object. See prev. sectionPackage package = aClass.getPackage();
From the Package
object you have access to information about the package like its name. You can also access information specified for this package in the Manifest
file of the JAR file this package is located in on the classpath. For instance, you can specify package version numbers in the Manifest
file. You can read more about the Package
class here: java.lang.Package
Superclass
From the Class
object you can access the superclass of the class. Here is how:
Class superclass = aClass.getSuperclass();
The superclass class object is a Class
object like any other, so you can continue doing class reflection on that too.
Implemented Interfaces
It is possible to get a list of the interfaces implemented by a given class. Here is how:
Class aClass = ... //obtain Class object. See prev. sectionClass[] interfaces = aClass.getInterfaces();
A class can implement many interfaces. Therefore an array of Class
is returned. Interfaces are also represented by Class
objects in Java Reflection.
NOTE: Only the interfaces specifically declared implemented by a given class is returned. If a superclass of the class implements an interface, but the class doesn't specifically state that it also implements that interface, that interface will not be returned in the array. Even if the class in practice implements that interface, because the superclass does.
To get a complete list of the interfaces implemented by a given class you will have to consult both the class and its superclasses recursively.
Constructors
You can access the constructors of a class like this:
Constructor[] constructors = aClass.getConstructors();
Constructors are covered in more detail in the text on Constructors.
Methods
You can access the methods of a class like this:
<pre>
Method[] method = aClass.getMethods();
</pre>
Methods are covered in more detail in the text on Methods.
Fields
You can access the fields (member variables) of a class like this:
Field[] method = aClass.getFields();
Fields are covered in more detail in the text on Fields.
Annotations
You can access the class annotations of a class like this:
Annotation[] annotations = aClass.getAnnotations();
Annotations are covered in more detail in the text on Annotations.