美文网首页
Java反射机制:类

Java反射机制:类

作者: SHUTUP | 来源:发表于2016-07-13 07:59 被阅读145次

使用Java反射机制,你可以在运行时检视Java类。检视Java类通常是你使用反射机制的第一步。从类对象你可以获取到如下信息

还有其它一些和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.

Next: Java Reflection - Constructors

相关文章

  • 详解Java反射机制(Reflection)

    详解Java反射机制(Reflection) 反射机制的作用 JAVA反射机制是在运行状态中,对于任意一个类,都能...

  • Android 使用注解和反射自制简单版的butternife

    一.Java反射机制。 1.反射机制的定义。 Java反射机制是指在运行状态中,对于任意一个类,都能知道这个类的所...

  • Java反射笔记

    Java反射的概述 什么是Java的反射机制 Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所...

  • [JAVA学习笔记] 反射机制

    [JAVA学习笔记] 反射机制 Java的反射通过Class类和java.lang.reflect类库实现。通过它...

  • Java基础之反射

    Java-Reflect Class类的使用 方法的反射 成员变量的反射 构造函数的反射 Java类加载机制 一、...

  • java反射机制

    java反射机制是为了动态获取类的结构,动态地调用对象的方法 java反射机制 获取类Class对象 A.clas...

  • b04-2 spring容器相关java知识-反射(精通Spri

    本篇内容: 1、java反射实现 2、java类加载器ClassLoader 3、java反射机制 一、java反...

  • Java反射使用总结

    Java反射总结 1.反射机制的定义 1.1定义 Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类...

  • 反射机制基础

    一、反射 1、反射机制 反射机制: JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方...

  • 浅析java的反射机制

    反射 一、Java的反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对...

网友评论

      本文标题:Java反射机制:类

      本文链接:https://www.haomeiwen.com/subject/eausjttx.html