java reflection reflection basics
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
*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
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); }

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

HashMap implements key-value pair storage through hash tables in Java, and its core lies in quickly positioning data locations. 1. First use the hashCode() method of the key to generate a hash value and convert it into an array index through bit operations; 2. Different objects may generate the same hash value, resulting in conflicts. At this time, the node is mounted in the form of a linked list. After JDK8, the linked list is too long (default length 8) and it will be converted to a red and black tree to improve efficiency; 3. When using a custom class as a key, the equals() and hashCode() methods must be rewritten; 4. HashMap dynamically expands capacity. When the number of elements exceeds the capacity and multiplies by the load factor (default 0.75), expand and rehash; 5. HashMap is not thread-safe, and Concu should be used in multithreaded

TosetJAVA_HOMEonWindows,firstlocatetheJDKinstallationpath(e.g.,C:\ProgramFiles\Java\jdk-17),thencreateasystemenvironmentvariablenamedJAVA_HOMEwiththatpath.Next,updatethePATHvariablebyadding%JAVA\_HOME%\bin,andverifythesetupusingjava-versionandjavac-v

Virtual threads have significant performance advantages in highly concurrency and IO-intensive scenarios, but attention should be paid to the test methods and applicable scenarios. 1. Correct tests should simulate real business, especially IO blocking scenarios, and use tools such as JMH or Gatling to compare platform threads; 2. The throughput gap is obvious, and it can be several times to ten times higher than 100,000 concurrent requests, because it is lighter and efficient in scheduling; 3. During the test, it is necessary to avoid blindly pursuing high concurrency numbers, adapting to non-blocking IO models, and paying attention to monitoring indicators such as latency and GC; 4. In actual applications, it is suitable for web backend, asynchronous task processing and a large number of concurrent IO scenarios, while CPU-intensive tasks are still suitable for platform threads or ForkJoinPool.

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.

The key to implementing a linked list is to define node classes and implement basic operations. ①First create the Node class, including data and references to the next node; ② Then create the LinkedList class, implementing the insertion, deletion and printing functions; ③ Append method is used to add nodes at the tail; ④ printList method is used to output the content of the linked list; ⑤ deleteWithValue method is used to delete nodes with specified values and handle different situations of the head node and the intermediate node.

ServiceMesh is an inevitable choice for the evolution of Java microservice architecture, and its core lies in decoupling network logic and business code. 1. ServiceMesh handles load balancing, fuse, monitoring and other functions through Sidecar agents to focus on business; 2. Istio Envoy is suitable for medium and large projects, and Linkerd is lighter and suitable for small-scale trials; 3. Java microservices should close Feign, Ribbon and other components and hand them over to Istiod for discovery and communication; 4. Ensure automatic injection of Sidecar during deployment, pay attention to traffic rules configuration, protocol compatibility, and log tracking system construction, and adopt incremental migration and pre-control monitoring planning.

Create and use SimpleDateFormat requires passing in format strings, such as newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); 2. Pay attention to case sensitivity and avoid misuse of mixed single-letter formats and YYYY and DD; 3. SimpleDateFormat is not thread-safe. In a multi-thread environment, you should create a new instance or use ThreadLocal every time; 4. When parsing a string using the parse method, you need to catch ParseException, and note that the result does not contain time zone information; 5. It is recommended to use DateTimeFormatter and Lo

To improve the performance of Java collection framework, we can optimize from the following four points: 1. Choose the appropriate type according to the scenario, such as frequent random access to ArrayList, quick search to HashSet, and concurrentHashMap for concurrent environments; 2. Set capacity and load factors reasonably during initialization to reduce capacity expansion overhead, but avoid memory waste; 3. Use immutable sets (such as List.of()) to improve security and performance, suitable for constant or read-only data; 4. Prevent memory leaks, and use weak references or professional cache libraries to manage long-term survival sets. These details significantly affect program stability and efficiency.
