java反射的api
Reflection means ability of a software to analyze itself. In Java, Reflection API provides facility to analyze and change runtime behavior of a class at runtime.
反射是指软件分析自身的能力。 在Java中,Reflection API提供了在运行时分析和更改类的运行时行为的工具。
For example, using reflection at the runtime you can determine what method, field, constructor or modifers a class supports.
例如,在运行时使用反射,您可以确定类支持哪些方法,字段,构造函数或修饰符。
The java.lang.Class class provides methods that are used to get metadata and manipulate the run time behavior of a class.
java.lang.Class类提供了用于获取元数据和操纵类的运行时行为的方法。
The java.lang andjava.lang.reflect packages provide many classes for reflection and get metadata of a particular class.
java.lang和java.lang.reflect包提供了许多用于反射的类并获取特定类的元数据。
One of the advantage of reflection API is, we can manipulate private members of the class too.
反射API的优点之一是,我们也可以操纵类的私有成员。
什么是反映包裹? (What is reflect package?)
java.lang.reflect
package encapsulates several important interfaces and classes. These classes and interface define methods which are used for reflection.
java.lang.reflect
包封装了几个重要的接口和类。 这些类和接口定义了用于反射的方法。
一些重要的java.lang.reflect包类 (Some Important Classes of java.lang.reflect package)
Class | What it does ? |
---|---|
Array | allow you to dynamically create and manipulate arrays |
Constructor | gives information about constructor of a class |
Field | provide information about field |
Method | provide information about method |
Modifier | provide information about class and member access modifier |
Proxy | supports dynamic proxy classes |
类 | 它能做什么 ? |
---|---|
数组 | 允许您动态创建和操作数组 |
建设者 | 提供有关类的构造函数的信息 |
领域 | 提供有关字段的信息 |
方法 | 提供有关方法的信息 |
修饰符 | 提供有关类和成员访问修饰符的信息 |
代理 | 支持动态代理类 |
Apart from these classes java.lang.Class
is another very important class used in Reflection API.
除了这些类之外, java.lang.Class
是反射API中使用的另一个非常重要的类。
反射的用途 (Uses of Reflection)
Developing IDE
开发IDE
Debugging and Test tools
调试和测试工具
Loading drivers and providing dynamic information
加载驱动程序并提供动态信息
反思的缺点 (Disadvantages of Reflection)
Low performance
效能低下
Security risk
安全风险
Violation of Oops concept
违反概念
Java Class类 (Java Class class)
Java provides a class Class that contains methods to get the metadata of a class and manipulate the run time behavior of a class.
Java提供了一个类Class,该类包含一些方法,这些方法可获取类的元数据并操纵类的运行时行为。
To perform reflection operation, we must use java.lang.Class that has public constructors for creating object.
要执行反射操作,我们必须使用具有公共构造函数的java.lang.Class来创建对象。
Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
Java虚拟机在加载类时以及通过在类加载器中调用defineClass方法自动构造类对象。
Java类方法 (Java Class Methods)
Method | Description |
---|---|
public String getName() | returns the class name |
public static Class forName(String className)throws ClassNotFoundException | loads the class and returns the reference of Class class. |
public Object newInstance()throws InstantiationException,IllegalAccessException | creates new instance. |
public boolean isInterface() | checks if it is interface. |
public boolean isArray() | checks if it is array. |
public boolean isPrimitive() | checks if it is primitive. |
public Class getSuperclass() | returns the superclass class reference. |
public Field[] getDeclaredFields()throws SecurityException | returns the total number of fields of this class. |
public Method[] getDeclaredMethods()throws SecurityException | returns the total number of methods of this class. |
public Constructor[] getDeclaredConstructors()throws SecurityException | returns the total number of constructors of this class. |
public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException | returns the method class instance. |
public Class<?>[] getClasses() | the array the array of Class objects representing the public members of this classof Class objects representing the public members of this class |
public Field[] getFields() throws SecurityException | the array of Field objects representing the public fields |
public Method[] getMethods()throws SecurityException | the array of Method objects representing the public methods of this class |
public Constructor<?>[] getConstructors() throws SecurityException | the array of Constructor objects representing the public constructors of this class |
public Method getMethod(String name, Class<?>... parameterTypes) | the Method object that matches the specified name and parameterTypes |
方法 | 描述 |
---|---|
公共字符串getName() | 返回类名 |
公共静态类forName(String className)抛出ClassNotFoundException | 加载类并返回Class类的引用。 |
公共对象newInstance()引发InstantiationException,IllegalAccessException | 创建新实例。 |
公共布尔isInterface() | 检查它是否是接口。 |
公共布尔isArray() | 检查它是否为数组。 |
公共布尔isPrimitive() | 检查它是否是原始的。 |
公共类getSuperclass() | 返回超类的类引用。 |
public Field [] getDeclaredFields()引发SecurityException | 返回此类的字段总数。 |
public Method [] getDeclaredMethods()引发SecurityException | 返回此类的方法总数。 |
公共构造方法[] getDeclaredConstructors()引发SecurityException | 返回此类的构造函数总数。 |
公共方法getDeclaredMethod(字符串名称,类[] parameterTypes)引发NoSuchMethodException,SecurityException | 返回方法类实例。 |
公共类<?> [] getClasses() | 该数组代表该类的公共成员的Class对象的数组 |
public Field [] getFields()抛出SecurityException | 代表公共字段的Field对象的数组 |
public Method [] getMethods()引发SecurityException | 表示此类的公共方法的Method对象的数组 |
public Constructor <?> [] getConstructors()抛出SecurityException | 表示此类的公共构造方法的Constructor对象的数组 |
public Method getMethod(String name,Class <?> ... parameterTypes) | 与指定名称和parameterTypes匹配的Method对象 |
如何检索类的对象? (How to retrieve Object of class ?)
We can get object of a class using Class class. there are manly three ways that are listed below:
我们可以使用Class类获取类的对象。 下面列出了三种男子气概的方法:
Object.getClass()
Object.getClass()
Using .class syntax
使用.class语法
Class.forName()
Class.forName()
1. If we have instance of a class then we can get its qualified name using the getClass()
method. This method is defined in Object class.
1.如果有一个类的实例,则可以使用getClass()
方法获取其限定名称。 此方法在Object类中定义。
Lets take an example, where we are getting class name using the getclass method. The instance can be any custom or built-in class.
让我们举一个例子,我们使用getclass方法获取类名。 该实例可以是任何自定义或内置类。
class Employee{
int empId;
String name;
}
class Demo{
public static void main(String[] args) throws ClassNotFoundException {
Employee employee = new Employee();
// employee object
Class name = employee.getClass();
System.out.println(name);
// string object
name = "hello".getClass();
System.out.println(name);
}
}
class Employee class java.lang.String
雇员类java.lang.String
2. If the type is available but there is no instance then it is possible to get a Class by using .class to the name of the type. This is also the easiest way to obtain the Class for a primitive type.
2.如果类型可用,但没有实例,则可以通过使用.class作为类型名称来获得Class。 这也是获取原始类型的Class的最简单方法。
class Employee{
int empId;
String name;
}
class Demo{
public static void main(String[] args) throws ClassNotFoundException {
// Employee Type
Class name = Employee.class;
System.out.println(name);
// string Type
name = String.class;
System.out.println(name);
}
}
class Employee class java.lang.String
雇员类java.lang.String
3. If we have fully-qualified name of a class, we can get the corresponding Class using the static method Class.forName()
. This method cannot be used for primitive types. See the below example.
3.如果我们拥有一个类的完全限定名称,则可以使用静态方法Class.forName()
获得相应的Class。 此方法不能用于基本类型。 请参见以下示例。
class Employee{
int empId;
String name;
}
class Demo{
public static void main(String[] args) throws ClassNotFoundException {
// Employee class
Class name = Class.forName("Employee");
System.out.println(name);
// String class
name = Class.forName("java.lang.String");
System.out.println(name);
}
}
class Employee class java.lang.String
雇员类java.lang.String
In our next topic, we will discuss to get metadata of a class, method, fields etc.
在下一个主题中,我们将讨论获取类,方法,字段等的元数据。
java反射的api