java反射用法示例

Java Reflection provides ability to inspect and modify the runtime behavior of application. Reflection in Java is one of the advance topic of core java. Using java reflection we can inspect a class, interface, enum, get their structure, methods and fields information at runtime even though class is not accessible at compile time. We can also use reflection to instantiate an object, invoke it’s methods, change field values.

Java Reflection提供了检查和修改应用程序运行时行为的功能。 Java中的反射是核心Java的高级主题之一。 使用java反射,即使在编译时无法访问类,我们也可以在运行时检查类, 接口 , 枚举 ,获取其结构,方法和字段信息。 我们还可以使用反射来实例化对象,调用其方法,更改字段值。

Java反射 (Java Reflection)

  1. Reflection in JavaJava中的反思
  2. Java Reflection for Classes
    • Get Class Object
    • Get Super Class
    • Get Public Member Classes
    • Get Declared Classes
    • Get Declaring Class
    • Getting Package Name
    • Getting Class Modifiers
    • Get Type Parameters
    • Get Implemented Interfaces
    • Get All Public Methods
    • Get All Public Constructors
    • Get All Public Fields
    • Get All Annotations

    Java反射类

    • 获取类对象
    • 获得超级班
    • 获取公共成员类
    • 获取声明的类
    • 获取声明类
    • 获取软件包名称
    • 获取类修饰符
    • 获取类型参数
    • 获取已实现的接口
    • 获取所有公共方法
    • 获取所有公共构造函数
    • 获取所有公共领域
    • 获取所有注释
  3. Java Reflection for Fields
    • Get Public Field
    • Field Declaring Class
    • Get Field Type
    • Get/Set Public Field Value
    • Get/Set Private Field Value

    Java字段反射

    • 获取公共领域
    • 现场申报课
    • 获取字段类型
    • 获取/设置公共字段值
    • 获取/设置私有字段值
  4. Java Reflection for Methods
    • Get Public Method
    • Invoking Public Method
    • Invoking Private Methods

    方法的Java反射

    • 获取公共方法
    • 调用公共方法
    • 调用私人方法
  5. Java Reflection for Constructors
    • Get Public Constructor
    • Instantiate Object using Constructor

    构造函数的Java反射

    • 获取公共构造函数
    • 使用构造函数实例化对象
  6. Java Reflection for Annotations注释的Java反射
  1. Java中的反思 (Reflection in Java)

Reflection in Java is a very powerful concept and it’s of little use in normal programming but it’s the backbone for most of the Java, J2EE frameworks. Some of the frameworks that use java reflection are:

Java中的反射是一个非常强大的概念,在常规编程中很少使用,但它是大多数Java,J2EE框架的基础。 使用Java反射的一些框架是:

  1. JUnit – uses reflection to parse @Test annotation to get the test methods and then invoke it.JUnit –使用反射来解析@Test批注以获取测试方法,然后调用它。
  2. Spring – dependency injection, read more at Spring Dependency InjectionSpring –依赖注入,更多内容请参见Spring Dependency Injection
  3. Tomcat web container to forward the request to correct module by parsing their web.xml files and request URI.Tomcat Web容器通过解析其web.xml文件和请求URI将请求转发至更正模块。
  4. Eclipse auto completion of method namesEclipse自动完成方法名称
  5. StrutsStruts
  6. Hibernate冬眠

The list is endless and they all use java reflection because all these frameworks have no knowledge and access of user defined classes, interfaces, their methods etc.

列表是无止境的,它们都使用Java反射,因为所有这些框架都不了解并且不能访问用户定义的类,接口,其方法等。

We should not use reflection in normal programming where we already have access to the classes and interfaces because of following drawbacks.

由于以下缺点,我们不应该在已经可以访问类和接口的常规编程中使用反射。

  • Poor Performance – Since java reflection resolve the types dynamically, it involves processing like scanning the classpath to find the class to load, causing slow performance.性能低下–由于Java反射会动态解析类型,因此涉及诸如扫描类路径以查找要加载的类的处理,从而导致性能降低。
  • Security Restrictions – Reflection requires runtime permissions that might not be available for system running under security manager. This can cause you application to fail at runtime because of security manager.安全限制 –反射需要运行时权限,该运行时权限可能不适用于在安全管理器下运行的系统。 由于安全管理器,这可能导致您的应用程序在运行时失败。
  • Security Issues – Using reflection we can access part of code that we are not supposed to access, for example we can access private fields of a class and change it’s value. This can be a serious security threat and cause your application to behave abnormally.安全问题 –使用反射,我们可以访问不应访问的部分代码,例如,我们可以访问类的私有字段并更改其值。 这可能是严重的安全威胁,并导致您的应用程序行为异常。
  • High Maintenance – Reflection code is hard to understand and debug, also any issues with the code can’t be found at compile time because the classes might not be available, making it less flexible and hard to maintain.高维护性 –反射代码很难理解和调试,并且在编译时找不到代码的任何问题,因为这些类可能不可用,从而使其灵活性较差且难以维护。
  1. Java反射类 (Java Reflection for Classes)

In java, every object is either a primitive type or reference. All the classes, enums, arrays are reference types and inherit from java.lang.Object. Primitive types are – boolean, byte, short, int, long, char, float, and double.

在Java中,每个对象都是原始类型或引用。 所有的类,枚举,数组都是引用类型,并且继承自java.lang.Object 。 基本类型为-布尔值,字节,短型,整数,长型,字符,浮点型和双精度型。

java.lang.Class is the entry point for all the reflection operations. For every type of object, JVM instantiates an immutable instance of java.lang.Class that provides methods to examine the runtime properties of the object and create new objects, invoke its method and get/set object fields.

java.lang.Class是所有反射操作的入口点。 对于每种类型的对象, JVM实例化一个java.lang.Class的不变实例,该实例提供方法来检查对象的运行时属性并创建新对象,调用其方法并获取/设置对象字段。

In this section, we will look into important methods of Class, for convenience, I am creating some classes and interfaces with inheritance hierarchy.

在本节中,我们将研究Class的重要方法,为方便起见,我将创建一些具有继承层次结构的类和接口。

package com.journaldev.reflection;public interface BaseInterface {public int interfaceInt=0;void method1();int method2(String str);
}
package com.journaldev.reflection;public class BaseClass {public int baseInt;private static void method3(){System.out.println("Method3");}public int method4(){System.out.println("Method4");return 0;}public static int method5(){System.out.println("Method5");return 0;}void method6(){System.out.println("Method6");}// inner public classpublic class BaseClassInnerClass{}//member public enumpublic enum BaseClassMemberEnum{}
}
package com.journaldev.reflection;@Deprecated
public class ConcreteClass extends BaseClass implements BaseInterface {public int publicInt;private String privateString="private string";protected boolean protectedBoolean;Object defaultObject;public ConcreteClass(int i){this.publicInt=i;}@Overridepublic void method1() {System.out.println("Method1 impl.");}@Overridepublic int method2(String str) {System.out.println("Method2 impl.");return 0;}@Overridepublic int method4(){System.out.println("Method4 overriden.");return 0;}public int method5(int i){System.out.println("Method4 overriden.");return 0;}// inner classespublic class ConcreteClassPublicClass{}private class ConcreteClassPrivateClass{}protected class ConcreteClassProtectedClass{}class ConcreteClassDefaultClass{}//member enumenum ConcreteClassDefaultEnum{}public enum ConcreteClassPublicEnum{}//member interfacepublic interface ConcreteClassPublicInterface{}}

Let’s look at some of the important refection methods for classes.

让我们看一下类的一些重要的refection方法。

获取类对象 (Get Class Object)

We can get Class of an object using three methods – through static variable class, using getClass() method of object and java.lang.Class.forName(String fullyClassifiedClassName). For primitive types and arrays, we can use static variable class. Wrapper classes provide another static variable TYPE to get the class.

我们可以使用三种方法获取对象的Class:通过静态变量class ,使用object的getClass()方法和java.lang.Class.forName(String fullyClassifiedClassName) 。 对于基本类型和数组,我们可以使用静态变量class 。 包装器类提供了另一个静态变量TYPE来获取该类。

// Get Class using reflection
Class<?> concreteClass = ConcreteClass.class;
concreteClass = new ConcreteClass(5).getClass();
try {// below method is used most of the times in frameworks like JUnit//Spring dependency injection, Tomcat web container//Eclipse auto completion of method names, hibernate, Struts2 etc.//because ConcreteClass is not available at compile timeconcreteClass = Class.forName("com.journaldev.reflection.ConcreteClass");
} catch (ClassNotFoundException e) {e.printStackTrace();
}
System.out.println(concreteClass.getCanonicalName()); // prints com.journaldev.reflection.ConcreteClass//for primitive types, wrapper classes and arrays
Class<?> booleanClass = boolean.class;
System.out.println(booleanClass.getCanonicalName()); // prints booleanClass<?> cDouble = Double.TYPE;
System.out.println(cDouble.getCanonicalName()); // prints doubleClass<?> cDoubleArray = Class.forName("[D");
System.out.println(cDoubleArray.getCanonicalName()); //prints double[]Class<?> twoDStringArray = String[][].class;
System.out.println(twoDStringArray.getCanonicalName()); // prints java.lang.String[][]

getCanonicalName() returns the canonical name of the underlying class. Notice that java.lang.Class uses Generics, it helps frameworks in making sure that the Class retrieved is subclass of framework Base Class. Check out Java Generics Tutorial to learn about generics and its wildcards.

getCanonicalName()返回基础类的规范名称。 请注意,java.lang.Class使用泛型,它有助于框架确保所检索的Class是框架Base Class的子类。 查看Java泛型教程,以了解泛型及其通配符。

获得超级班 (Get Super Class)

getSuperclass() method on a Class object returns the super class of the class. If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned. If this object represents an array class then the Class object representing the Object class is returned.

Class对象的getSuperclass()方法返回该类的超类。 如果该Class表示Object类,接口,原始类型或void,则返回null。 如果此对象表示数组类,则返回表示Object类的Class对象。

Class<?> superClass = Class.forName("com.journaldev.reflection.ConcreteClass").getSuperclass();
System.out.println(superClass); // prints "class com.journaldev.reflection.BaseClass"
System.out.println(Object.class.getSuperclass()); // prints "null"
System.out.println(String[][].class.getSuperclass());// prints "class java.lang.Object"

获取公共成员类 (Get Public Member Classes)

getClasses() method of a Class representation of object returns an array containing Class objects representing all the public classes, interfaces and enums that are members of the class represented by this Class object. This includes public class and interface members inherited from superclasses and public class and interface members declared by the class. This method returns an array of length 0 if this Class object has no public member classes or interfaces or if this Class object represents a primitive type, an array class, or void.

对象的Class表示形式的getClasses()方法返回一个数组,该数组包含Class对象,这些Class对象表示所有公共类,接口和枚举,这些公共类,接口和枚举都是该Class对象所表示的类的成员。 这包括从超类继承的公共类和接口成员,以及由该类声明的公共类和接口成员。 如果此Class对象没有公共成员类或接口,或者此Class对象表示原始类型,数组类或void,则此方法返回长度为0的数组。

Class<?>[] classes = concreteClass.getClasses();
//[class com.journaldev.reflection.ConcreteClass$ConcreteClassPublicClass,
//class com.journaldev.reflection.ConcreteClass$ConcreteClassPublicEnum,
//interface com.journaldev.reflection.ConcreteClass$ConcreteClassPublicInterface,
//class com.journaldev.reflection.BaseClass$BaseClassInnerClass,
//class com.journaldev.reflection.BaseClass$BaseClassMemberEnum]
System.out.println(Arrays.toString(classes));

获取声明的类 (Get Declared Classes)

getDeclaredClasses() method returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. The returned array doesn’t include classes declared in inherited classes and interfaces.

getDeclaredClasses()方法返回一个Class对象数组,该数组反映了所有声明为该Class对象表示的类成员的所有类和接口。 返回的数组不包括在继承的类和接口中声明的类。

//getting all of the classes, interfaces, and enums that are explicitly declared in ConcreteClass
Class<?>[] explicitClasses = Class.forName("com.journaldev.reflection.ConcreteClass").getDeclaredClasses();
//prints [class com.journaldev.reflection.ConcreteClass$ConcreteClassDefaultClass,
//class com.journaldev.reflection.ConcreteClass$ConcreteClassDefaultEnum,
//class com.journaldev.reflection.ConcreteClass$ConcreteClassPrivateClass,
//class com.journaldev.reflection.ConcreteClass$ConcreteClassProtectedClass,
//class com.journaldev.reflection.ConcreteClass$ConcreteClassPublicClass,
//class com.journaldev.reflection.ConcreteClass$ConcreteClassPublicEnum,
//interface com.journaldev.reflection.ConcreteClass$ConcreteClassPublicInterface]
System.out.println(Arrays.toString(explicitClasses));

获取声明类 (Get Declaring Class)

getDeclaringClass() method returns the Class object representing the class in which it was declared.

getDeclaringClass()方法返回表示声明它的类的Class对象。

Class<?> innerClass = Class.forName("com.journaldev.reflection.ConcreteClass$ConcreteClassDefaultClass");
//prints com.journaldev.reflection.ConcreteClass
System.out.println(innerClass.getDeclaringClass().getCanonicalName());
System.out.println(innerClass.getEnclosingClass().getCanonicalName());

获取软件包名称 (Getting Package Name)

getPackage() method returns the package for this class. The class loader of this class is used to find the package. We can invoke getName() method of Package to get the name of the package.

getPackage()方法返回此类的包。 此类的类加载器用于查找包。 我们可以调用Package的getName()方法来获取包的名称。

//prints "com.journaldev.reflection"
System.out.println(Class.forName("com.journaldev.reflection.BaseInterface").getPackage().getName());

获取类修饰符 (Getting Class Modifiers)

getModifiers() method returns the int representation of the class modifiers, we can use java.lang.reflect.Modifier.toString() method to get it in the string format as used in source code.

getModifiers()方法返回类修饰符的int表示形式,我们可以使用java.lang.reflect.Modifier.toString()方法以源代码中使用的字符串格式获取它。

System.out.println(Modifier.toString(concreteClass.getModifiers())); //prints "public"
//prints "public abstract interface"
System.out.println(Modifier.toString(Class.forName("com.journaldev.reflection.BaseInterface").getModifiers()));

获取类型参数 (Get Type Parameters)

getTypeParameters() returns the array of TypeVariable if there are any Type parameters associated with the class. The type parameters are returned in the same order as declared.

如果有任何与该类关联的Type参数,则getTypeParameters()返回TypeVariable数组。 类型参数的返回顺序与声明的顺序相同。

//Get Type parameters (generics)
TypeVariable<?>[] typeParameters = Class.forName("java.util.HashMap").getTypeParameters();
for(TypeVariable<?> t : typeParameters)
System.out.print(t.getName()+",");

获取已实现的接口 (Get Implemented Interfaces)

getGenericInterfaces() method returns the array of interfaces implemented by the class with generic type information. We can also use getInterfaces() to get the class representation of all the implemented interfaces.

getGenericInterfaces()方法返回具有通用类型信息的类实现的接口数组。 我们还可以使用getInterfaces()获取所有已实现接口的类表示。

Type[] interfaces = Class.forName("java.util.HashMap").getGenericInterfaces();
//prints "[java.util.Map<K, V>, interface java.lang.Cloneable, interface java.io.Serializable]"
System.out.println(Arrays.toString(interfaces));
//prints "[interface java.util.Map, interface java.lang.Cloneable, interface java.io.Serializable]"
System.out.println(Arrays.toString(Class.forName("java.util.HashMap").getInterfaces()));

获取所有公共方法 (Get All Public Methods)

getMethods() method returns the array of public methods of the Class including public methods of it’s superclasses and super interfaces.

getMethods()方法返回Class的公共方法的数组,包括其超类和超级接口的公共方法。

Method[] publicMethods = Class.forName("com.journaldev.reflection.ConcreteClass").getMethods();
//prints public methods of ConcreteClass, BaseClass, Object
System.out.println(Arrays.toString(publicMethods));

获取所有公共构造函数 (Get All Public Constructors)

getConstructors() method returns the list of public constructors of the class reference of object.

getConstructors()方法返回对象的类引用的公共构造函数的列表。

//Get All public constructors
Constructor<?>[] publicConstructors = Class.forName("com.journaldev.reflection.ConcreteClass").getConstructors();
//prints public constructors of ConcreteClass
System.out.println(Arrays.toString(publicConstructors));

获取所有公共领域 (Get All Public Fields)

getFields() method returns the array of public fields of the class including public fields of it’s super classes and super interfaces.

getFields()方法返回该类的公共字段数组,包括其超类和超级接口的公共字段。

//Get All public fields
Field[] publicFields = Class.forName("com.journaldev.reflection.ConcreteClass").getFields();
//prints public fields of ConcreteClass, it's superclass and super interfaces
System.out.println(Arrays.toString(publicFields));

获取所有注释 (Get All Annotations)

getAnnotations() method returns all the annotations for the element, we can use it with class, fields and methods also. Note that only annotations available with reflection are with retention policy of RUNTIME, check out Java Annotations Tutorial.
We will look into this in more details in later sections.

getAnnotations()方法返回元素的所有注释,我们也可以将其与类,字段和方法一起使用。 请注意,只有反射可用的注释才具有RUNTIME的保留策略,请查看Java注释教程
我们将在后面的部分中对此进行更详细的研究。

java.lang.annotation.Annotation[] annotations = Class.forName("com.journaldev.reflection.ConcreteClass").getAnnotations();
//prints [@java.lang.Deprecated()]
System.out.println(Arrays.toString(annotations));
  1. Java字段反射 (Java Reflection for Fields)

Reflection API provides several methods to analyze Class fields and modify their values at runtime, in this section we will look into some of the commonly used reflection functions for methods.

Reflection API提供了几种方法来分析Class字段并在运行时修改它们的值,在本节中,我们将研究一些方法的常用反射函数。

获取公共领域 (Get Public Field)

In last section, we saw how to get the list of all the public fields of a class. Reflection API also provides method to get specific public field of a class through getField() method. This method look for the field in the specified class reference and then in the super interfaces and then in the super classes.

在上一节中,我们看到了如何获取类的所有公共字段的列表。 Reflection API还提供了通过getField()方法获取类的特定公共字段的方法。 此方法在指定的类引用中查找字段,然后在超级接口中查找字段,然后在超级类中查找。

Field field = Class.forName("com.journaldev.reflection.ConcreteClass").getField("interfaceInt");

Above call will return the field from BaseInterface that is implemented by ConcreteClass. If there is no field found then it throws NoSuchFieldException.

上面的调用将从BaseInterface返回由ConcreteClass实现的字段。 如果没有找到任何字段,则抛出NoSuchFieldException。

现场申报课 (Field Declaring Class)

We can use getDeclaringClass() of field object to get the class declaring the field.

我们可以使用字段对象的getDeclaringClass()获取声明字段的类。

try {Field field = Class.forName("com.journaldev.reflection.ConcreteClass").getField("interfaceInt");Class<?> fieldClass = field.getDeclaringClass();System.out.println(fieldClass.getCanonicalName()); //prints com.journaldev.reflection.BaseInterface
} catch (NoSuchFieldException | SecurityException e) {e.printStackTrace();
}

获取字段类型 (Get Field Type)

getType() method returns the Class object for the declared field type, if field is primitive type, it returns the wrapper class object.

getType()方法返回声明的字段类型的Class对象,如果field是原始类型,则返回包装类对象。

Field field = Class.forName("com.journaldev.reflection.ConcreteClass").getField("publicInt");
Class<?> fieldType = field.getType();
System.out.println(fieldType.getCanonicalName()); //prints int

获取/设置公共字段值 (Get/Set Public Field Value)

We can get and set the value of a field in an Object using reflection.

我们可以使用反射获取并设置对象中字段的值。

Field field = Class.forName("com.journaldev.reflection.ConcreteClass").getField("publicInt");
ConcreteClass obj = new ConcreteClass(5);
System.out.println(field.get(obj)); //prints 5
field.setInt(obj, 10); //setting field value to 10 in object
System.out.println(field.get(obj)); //prints 10

get() method return Object, so if field is primitive type, it returns the corresponsing Wrapper Class. If the field is static, we can pass Object as null in get() method.

get()方法返回Object,因此,如果field是原始类型,则它将返回对应的Wrapper类 。 如果该字段是静态的,则可以在get()方法中将Object作为null传递。

There are several set*() methods to set Object to the field or set different types of primitive types to the field. We can get the type of field and then invoke correct function to set the field value correctly. If the field is final, the set() methods throw java.lang.IllegalAccessException.

有几种set *()方法可将Object设置为该字段或将不同类型的基本类型设置为该字段。 我们可以获取字段的类型,然后调用正确的函数来正确设置字段值。 如果该字段为final,则set()方法将引发java.lang.IllegalAccessException。

获取/设置私有字段值 (Get/Set Private Field Value)

We know that private fields and methods can’t be accessible outside of the class but using reflection we can get/set the private field value by turning off the java access check for field modifiers.

我们知道私有字段和方法无法在类外部访问,但是使用反射,我们可以通过关闭字段修饰符的Java访问检查来获取/设置私有字段值。

Field privateField = Class.forName("com.journaldev.reflection.ConcreteClass").getDeclaredField("privateString");
//turning off access check with below method call
privateField.setAccessible(true);
ConcreteClass objTest = new ConcreteClass(1);
System.out.println(privateField.get(objTest)); // prints "private string"
privateField.set(objTest, "private string updated");
System.out.println(privateField.get(objTest)); //prints "private string updated"
  1. 方法的Java反射 (Java Reflection for Methods)

Using reflection we can get information about a method and we can invoke it also. In this section, we will learn different ways to get a method, invoke a method and accessing private methods.

使用反射,我们可以获得有关方法的信息,我们也可以调用它。 在本节中,我们将学习获取方法,调用方法和访问私有方法的不同方法。

获取公共方法 (Get Public Method)

We can use getMethod() to get a public method of class, we need to pass the method name and parameter types of the method. If the method is not found in the class, reflection API looks for the method in superclass.

我们可以使用getMethod()获取类的公共方法,我们需要传递方法名称和方法的参数类型。 如果在类中未找到该方法,则反射API在超类中查找该方法。

In below example, I am getting put() method of HashMap using reflection. The example also shows how to get the parameter types, method modifiers and return type of a method.

在下面的示例中,我使用反射获取HashMap的put()方法。 该示例还显示了如何获取方法的参数类型,方法修饰符和返回类型。

Method method = Class.forName("java.util.HashMap").getMethod("put", Object.class, Object.class);
//get method parameter types, prints "[class java.lang.Object, class java.lang.Object]"
System.out.println(Arrays.toString(method.getParameterTypes()));
//get method return type, return "class java.lang.Object", class reference for void
System.out.println(method.getReturnType());
//get method modifiers
System.out.println(Modifier.toString(method.getModifiers())); //prints "public"

调用公共方法 (Invoking Public Method)

We can use invoke() method of Method object to invoke a method, in below example code I am invoking put method on HashMap using reflection.

我们可以使用Method对象的invoke()方法来调用方法,在下面的示例代码中,我使用反射在HashMap上调用put方法。

Method method = Class.forName("java.util.HashMap").getMethod("put", Object.class, Object.class);
Map<String, String> hm = new HashMap<>();
method.invoke(hm, "key", "value");
System.out.println(hm); // prints {key=value}

If the method is static, we can pass NULL as object argument.

如果该方法是静态的,则可以将NULL作为对象参数传递。

调用私人方法 (Invoking Private Methods)

We can use getDeclaredMethod() to get the private method and then turn off the access check to invoke it, below example shows how we can invoke method3() of BaseClass that is static and have no parameters.

我们可以使用getDeclaredMethod()获取私有方法,然后关闭访问检查以调用它,下面的示例显示了如何调用静态且没有参数的BaseClass的method3()。

//invoking private method
Method method = Class.forName("com.journaldev.reflection.BaseClass").getDeclaredMethod("method3", null);
method.setAccessible(true);
method.invoke(null, null); //prints "Method3"
  1. 构造函数的Java反射 (Java Reflection for Constructors)

Reflection API provides methods to get the constructors of a class to analyze and we can create new instances of class by invoking the constructor. We have already learned how to get all the public constructors.

Reflection API提供了获取类的构造函数进行分析的方法,我们可以通过调用构造函数来创建类的新实例。 我们已经学习了如何获取所有公共构造函数。

获取公共构造函数 (Get Public Constructor)

We can use getConstructor() method on the class representation of object to get specific public constructor. Below example shows how to get the constructor of ConcreteClass defined above and the no-argument constructor of HashMap. It also shows how to get the array of parameter types for the constructor.

我们可以在对象的类表示形式上使用getConstructor()方法来获取特定的公共构造函数。 下面的示例演示如何获取上面定义的ConcreteClass的构造函数和HashMap的无参数构造函数。 它还显示了如何获取构造函数的参数类型数组。

Constructor<?> constructor = Class.forName("com.journaldev.reflection.ConcreteClass").getConstructor(int.class);
//getting constructor parameters
System.out.println(Arrays.toString(constructor.getParameterTypes())); // prints "[int]"Constructor<?> hashMapConstructor = Class.forName("java.util.HashMap").getConstructor(null);
System.out.println(Arrays.toString(hashMapConstructor.getParameterTypes())); // prints "[]"

使用构造函数实例化对象 (Instantiate Object using Constructor)

We can use newInstance() method on the constructor object to instantiate a new instance of the class. Since we use reflection when we don’t have the classes information at compile time, we can assign it to Object and then further use reflection to access it’s fields and invoke it’s methods.

我们可以在构造函数对象上使用newInstance()方法来实例化该类的新实例。 由于我们在编译时没有类信息时使用反射,因此可以将其分配给Object,然后进一步使用反射来访问其字段并调用其方法。

Constructor<?> constructor = Class.forName("com.journaldev.reflection.ConcreteClass").getConstructor(int.class);
//getting constructor parameters
System.out.println(Arrays.toString(constructor.getParameterTypes())); // prints "[int]"Object myObj = constructor.newInstance(10);
Method myObjMethod = myObj.getClass().getMethod("method1", null);
myObjMethod.invoke(myObj, null); //prints "Method1 impl."Constructor<?> hashMapConstructor = Class.forName("java.util.HashMap").getConstructor(null);
System.out.println(Arrays.toString(hashMapConstructor.getParameterTypes())); // prints "[]"
HashMap<String,String> myMap = (HashMap<String,String>) hashMapConstructor.newInstance(null);
  1. 批注反射 (Reflection for Annotations)

Annotations was introduced in Java 1.5 to provide metadata information of the class, methods or fields and now it’s heavily used in frameworks like Spring and Hibernate. Reflection API was also extended to provide support to analyze the annotations at runtime.

Java 1.5中引入了注释,以提供有关类,方法或字段的元数据信息,现在它在诸如Spring和Hibernate之类的框架中大量使用。 反射API也进行了扩展,以提供在运行时分析注释的支持。

Using reflection API we can analyze annotations whose retention policy is Runtime. I have already written a detailed tutorial on annotations and how we can use reflection API to parse annotations, so I would suggest you to check out Java Annotations Tutorial.

使用反射API,我们可以分析其保留策略为Runtime的注释。 我已经写了一个详细的注释教程,以及如何使用反射API解析注释,因此建议您阅读Java注释教程

Thats all for java reflection example tutorial, I hope you liked the tutorial and understood the importance of Java Reflection API.

多数民众赞成在Java反射示例教程中,希望您喜欢该教程并理解Java Reflection API的重要性。

翻译自: https://www.journaldev.com/1789/java-reflection-example-tutorial

java反射用法示例

java反射用法示例_Java反射示例教程相关推荐

  1. java反射 数组类型_Java反射-数组

    通过反射使用数组有时会比较棘手.特别是需要获得一个特定类型数组的Class对象,如int[]等.本文将讨论如何通过反射创建数组和获得他们的Class对象. 注意:本文在阅读Eyal Lupu的博客&q ...

  2. java反射随意值_Java反射总结

    能够分析类能力的程序称为反射.对于给定的Java类名,可以通过反射获取类的信息.将类的各成分映射出相应的Java类. Class类 在程序运行期间,Java运行时系统始终对所有的对象维护一个被称为运行 ...

  3. java反射最佳实践_Java 反射最佳实践 – 码农网

    标签: 概要:最简单优雅的使用反射. 本文的例子都可以在示例代码中看到并下载,如果喜欢请star,如果觉得有纰漏请提交issue,如果你有更好的点子可以提交pull request.本文的示例代码主要 ...

  4. java反射源码_java反射技术详解附源码

    在学校学习Java时,由于学的不扎实,也没经历过太多实战项目,所以很多重要的知识点瞟一眼就过去了,比如现在要讲的反射,当时直接就忽略掉了,可现在发现很多地方需要反射,不得不重新学习一下,上学欠了太多债 ...

  5. java反射创建实例_Java反射创建实例

    Java反射创建实例 package com.wkcto.chapter08.demo02; import java.lang.reflect.Constructor; import java.lan ...

  6. java 反射 工厂模式_Java反射机制demo(七)—反射机制与工厂模式

    Java反射机制demo(七)-反射机制与工厂模式 工厂模式 简介 工厂模式是最常用的实例化对象模式. 工厂模式的主要作用就是使用工厂方法代替new操作. 为什么要使用工厂模式?直接new不好吗? 直 ...

  7. java反射的原理_java反射机制的实现原理

    java反射机制的实现原理 反射机制: 所谓的反射机制就是java语言在运行时拥有一项自观的能力. 通过这种能力可以彻底的了解自身的情况为下一步的动作做准备. 下面具体介绍一下java的反射机制.这里 ...

  8. java无向图代码实例_Java 图示例 · JavaTutorialNetwork 中文系列教程 · 看云

    # Java 图示例 > 原文: [https://javatutorial.net/graphs-java-example](https://javatutorial.net/graphs-j ...

  9. java 设计模式 示例_Java设计模式–示例教程

    java 设计模式 示例 Design Patterns are very popular among software developers. A design pattern is a well- ...

最新文章

  1. 全球所有货币币种汇总
  2. STM8L之外部中断
  3. windows下安装emscripten
  4. java打印两个小人_[原创]Java画小人与阶梯问题的解答
  5. 硬件平台(1)---骁龙710移动平台强势来袭
  6. 一种一致性HASH算法的实现方法,附核心代码
  7. 关于ssh整合后struts2拦截器不起作用(blog-1)
  8. 磁珠法RNA pull down试剂盒、蛋白质-核酸相互作用
  9. 计算机可以谭音乐吗,谭晶怎么被叫谭哈哈 《歌手》谭晶演唱什么歌曲
  10. 笔记本电脑共享WiFi
  11. 解析小型机、大型机和PC服务器间的差别
  12. 计数器集成芯片+分析时序逻辑电路
  13. Excel如何批量重命名文件
  14. 基于PyQt的分组工具
  15. webpack工具链热替换 -- angularjs的粗放式实现
  16. element——弹窗
  17. 【Windows】如何删除磁盘管理中的恢复分区
  18. CentOS7 使用 kubeadm 搭建 kubernetes 集群(极速篇)
  19. 构建情绪检测应用程序
  20. 晏子:拒欲不道,恶爱不祥

热门文章

  1. ASP.NET 防止F5刷新页面按钮重复提交
  2. 狼来了!中国房地产的实质--比喻太生动了
  3. 使用 Django 的日志模块,同时发送错误邮件到163邮箱
  4. [转载] python中append函数的用法
  5. [转载] Python算法
  6. 大兄dei,早点看清this吧
  7. HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
  8. 第一次大作业总结——四则运算程序
  9. 汇编语言学习之汇编语言源程序的输入
  10. bzoj 1029 贪心