Home Java javaTutorial java reflection reflection basics

java reflection reflection basics

Nov 21, 2016 am 10:54 AM

1. Reflection

Reflection: The JAVA reflection mechanism is in the running state. For any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties; this The function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.

If you want to dissect a class, you must first obtain the bytecode file object of the class. The dissection uses the methods in the Class class. Therefore, you must first obtain the Class type object corresponding to each bytecode file. The operation of reflection is actually obtained through the Class object:

*a, java.lang.reflect.Field: Provides information about a single field of a class or interface, as well as dynamic access rights to it. The reflected field may be a class (static) field or an instance field. Member variables of the operating class.
*b, java.lang.reflect.Constructor: The constructor of the operation class.
*c, java.lang.reflect.Method: Method of operating class.

Create a Person object as an instance before learning the basics of reflection

package com.jalja.org.base.relfect;

public class Person {
    private String name;
    int age;
    public String address;

    public Person() {
    }

    private Person(String name) {
        this.name = name;
    }

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public void show() {
        System.out.println("show");
    }

    public void method(String s) {
        System.out.println("method " + s);
    }

    public String getString(String s, int i) {
        return s + "---" + i;
    }

    private void function() {
        System.out.println("function");
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", address=" + address
                + "]";
    }

}

2. Get the Class object of the class

public static void getClassObject() throws ClassNotFoundException{
        //方式一:Object的getClass()方法
        Person person1=new Person();
        Person person2=new Person();
        Class c1=person1.getClass();
        Class c2=person2.getClass();
        System.out.println(person1==person2);//false
        System.out.println(c1==c2);//true 不管JVM内存中有多少个对象,对于字节码文件来说只有一份
        //方式二:数据类型的静态class属性
        Class c3=Person.class;
        System.out.println(c1==c3);//true
        //方式三:Class 类的静态方法 
        //public static Class<?> forName(String className)throws ClassNotFoundException
        Class c4=Class.forName("com.jalja.org.base.relfect.Person");
        System.out.println(c1==c4);//true
    }

3. java.lang.reflect.Constructor: object and use the Constructor class.

1. Get the Constructor object

//获取Class 对象所表示的类的构造方法
    public static void getConstructorTest() throws  Exception{
        Class c4=Class.forName("com.jalja.org.base.relfect.Person");
        //1、获取Class 对象所表示的类所有公共构造方法
        //public Constructor<?>[] getConstructors() throws SecurityException
        Constructor [] cs=c4.getConstructors();
        //2、获取Class 对象所表示的类所有构造方法
        //public Constructor<?>[] getDeclaredConstructors() throws SecurityException
        Constructor[] cs2 =c4.getDeclaredConstructors();
        //3、获取Class对象所表示类的指定指定公共构造方法,  parameterTypes 参数是 Class 对象的一个数组 ,是指定数据类型的字节码
        //public Constructor<T> getConstructor(Class<?>... parameterTypes);
        Constructor cs3=c4.getConstructor();//获取公共的无参构造方法的Constructor对象
        //获取 该 构造函数  public Person(String name, int age, String address)
        Constructor cs4=c4.getConstructor(String.class,int.class,String.class);
        //4、获取Clss对象所表示类指定的构造范法官 parameterTypes 参数是 Class 对象的一个数组,它按声明顺序标识构造方法的形参类型的字节码。
        //public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes);
        //获取该构造 函数 private Person(String name)  的Constructor对象
        Constructor cs5=c4.getDeclaredConstructor(String.class);
        
    }

2. Create an instance of the class represented by the Class object through the Constructor object

public static void createObject() throws Exception{
        Class c4=Class.forName("com.jalja.org.base.relfect.Person");
        //使用此 Constructor 对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例
        //public T newInstance(Object... initargs);
        // Person person=new Person()
        Constructor cs3=c4.getConstructor();//获取公共的无参构造方法的Constructor对象
        Object obj=cs3.newInstance();
        
        //Person person=new Person("jalja", 21, "北京");
        Constructor cs4=c4.getConstructor(String.class,int.class,String.class);
        Object obj1=cs4.newInstance("jalja",21,"北京");
        System.out.println(obj1);//Person [name=jalja, age=21, address=北京]
        
        //实例化一个私有的构造函数 private Person(String name)
        //控制java的访问检查
        //public void setAccessible(boolean flag)
        //将此对象的 accessible 标志设置为指示的布尔值。值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。
        //值为 false 则指示反射的对象应该实施 Java 语言访问检查。 
        Constructor cs5=c4.getDeclaredConstructor(String.class);
        cs5.setAccessible(true);
        Object obj2=cs5.newInstance("张三丰");
        System.out.println(obj2);//Person [name=张三丰, age=0, address=null]
    }

4. java.lang.reflect.Field

1. Get the Field object

//获取Class类的Field对象
    public static void getFieldTest() throws Exception{
        Class cs=Class.forName("com.jalja.org.base.relfect.Person");
        //1、public Field[] getFields() throws SecurityException
        //获取Class 对象所表示的类或接口的所有可访问公共(public修饰的)字段
        Field [] fs=cs.getFields(); 
        //2、public Field[] getDeclaredFields() throws SecurityException
        // 获取Class 对象所表示的类或接口所声明的所有字段。包括公共、保护、默认(包)访问和私有字段,但不包括继承的字段
        Field [] fs1=cs.getDeclaredFields(); 
        //3、public Field getField(String name)throws NoSuchFieldException, SecurityException;
        //获取Class 对象所表示的类或接口的指定公共成员(public修饰)字段。name 参数是一个 String,用于指定所需字段的简称
        Field fs2=cs.getField("address");
        //public Field getDeclaredField(String name)  throws NoSuchFieldException,SecurityException
        //获取 Class 对象所表示的类或接口的指定已声明字段。name 参数是一个 String,它指定所需字段的简称
        Field fs3=cs.getDeclaredField("name");
        System.out.println(fs3);
    }

2. Through the Field object Assign a value to the specified class attribute

//使用 Field对象
    public static void createVarValue() throws Exception{
        Class cs=Class.forName("com.jalja.org.base.relfect.Person");
        Object obj=cs.getConstructor().newInstance();
        Field addressField=cs.getField("address");
        //public void set(Object obj, Object value);
        //将指定对象变量上此 Field 对象表示的字段设置为指定的新值。如果底层字段的类型为基本类型,则对新值进行自动解包
        //obj - 应该修改其字段的对象  value - 正被修改的 obj 的字段的新值 
        addressField.set(obj, "北京");
        System.out.println(obj); //Person [name=null, age=0, address=北京]
        
        //对非public修饰的变量操作
        Field nameField=cs.getDeclaredField("name");
        //控制java的访问检查
        nameField.setAccessible(true);
        nameField.set(obj, "张三丰");
        System.out.println(obj);//Person [name=张三丰, age=0, address=北京]
    }

5. java.lang.reflect.Method

1. Get the Method object

//获取Method对象
    public static void getMethodTest() throws Exception{
        Class cs=Class.forName("com.jalja.org.base.relfect.Person");
        //1、public Method[] getMethods() throws SecurityException
        //获取Class 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。
        Method [] m1=cs.getMethods();
        //2、public Method[] getDeclaredMethods() throws SecurityException
        //获取Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法
        Method [] m2=cs.getDeclaredMethods();
        //3、public Method getMethod(String name, Class<?>... parameterTypes)throws NoSuchMethodException, SecurityException;
        // 获取Class 对象所表示的类或接口的指定公共成员方法。name 参数是一个 String,用于指定所需方法的简称。parameterTypes 参数是按声明顺序标识该方法形参类型的 Class 对象的一个数组
        Method m3=cs.getMethod("show");//无参的方法
        Method m4=cs.getMethod("method",String.class);//带参的方法
        //public Method getDeclaredMethod(String name, Class<?>... parameterTypes)throws NoSuchMethodException,SecurityException
        // Class 对象所表示的类或接口的指定已声明方法。name 参数是一个 String,它指定所需方法的简称,parameterTypes 参数是 Class 对象的一个数组
        Method m5=cs.getDeclaredMethod("function");//无参的方法
        System.out.println(m5);
    }

2. Call the method of the specified class through the Method object

// Method对象的使用
    public static void createMethod() throws Exception{
        Class cs=Class.forName("com.jalja.org.base.relfect.Person");
        Object obj=cs.getConstructor().newInstance();
        Method m3=cs.getMethod("show");//无参的方法
        //public Object invoke(Object obj,Object... args)
        //对带有指定参数的指定对象调用由此 Method 对象表示的底层方法  obj - 从中调用底层方法的对象    args - 用于方法调用的参数 
        m3.invoke(obj);
        //对带参方法的操作
        Method m4=cs.getMethod("method",String.class);//带参的方法
        m4.invoke(obj,"北京");
        //对有返回值得方法操作
        Method m6=cs.getMethod("getString",String.class,int.class);//带参的方法
        Object str=m6.invoke(obj,"北京",200);
        System.out.println(str);
        //对私有无参方法的操作
        Method m5=cs.getDeclaredMethod("function");
        m5.setAccessible(true);
        m5.invoke(obj);
    }


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1598
276
How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

Building RESTful APIs in Java with Jakarta EE Building RESTful APIs in Java with Jakarta EE Jul 30, 2025 am 03:05 AM

SetupaMaven/GradleprojectwithJAX-RSdependencieslikeJersey;2.CreateaRESTresourceusingannotationssuchas@Pathand@GET;3.ConfiguretheapplicationviaApplicationsubclassorweb.xml;4.AddJacksonforJSONbindingbyincludingjersey-media-json-jackson;5.DeploytoaJakar

Mastering Dependency Injection in Java with Spring and Guice Mastering Dependency Injection in Java with Spring and Guice Aug 01, 2025 am 05:53 AM

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

Java Performance Optimization and Profiling Techniques Java Performance Optimization and Profiling Techniques Jul 31, 2025 am 03:58 AM

Use performance analysis tools to locate bottlenecks, use VisualVM or JProfiler in the development and testing stage, and give priority to Async-Profiler in the production environment; 2. Reduce object creation, reuse objects, use StringBuilder to replace string splicing, and select appropriate GC strategies; 3. Optimize collection usage, select and preset initial capacity according to the scene; 4. Optimize concurrency, use concurrent collections, reduce lock granularity, and set thread pool reasonably; 5. Tune JVM parameters, set reasonable heap size and low-latency garbage collector and enable GC logs; 6. Avoid reflection at the code level, replace wrapper classes with basic types, delay initialization, and use final and static; 7. Continuous performance testing and monitoring, combined with JMH

A Developer's Guide to Maven for Java Project Management A Developer's Guide to Maven for Java Project Management Jul 30, 2025 am 02:41 AM

Maven is a standard tool for Java project management and construction. The answer lies in the fact that it uses pom.xml to standardize project structure, dependency management, construction lifecycle automation and plug-in extensions; 1. Use pom.xml to define groupId, artifactId, version and dependencies; 2. Master core commands such as mvnclean, compile, test, package, install and deploy; 3. Use dependencyManagement and exclusions to manage dependency versions and conflicts; 4. Organize large applications through multi-module project structure and are managed uniformly by the parent POM; 5.

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

See all articles