Mục đích của mình khi viết series Java Sec là để lưu lại các kiến thức trong quá trình tìm hiểu và học JAVA của mình.
Java reflection là một tính năng trong ngôn ngữ Java. Trước tiên cần hiểu 2 định nghĩa về compile time
và runtime
Compile Time:
– Compile time refers to the phase of program development when the source code is translated into bytecode by the Java compiler (javac). During this phase, the compiler checks the syntax, type correctness, and other static aspects of the code. If there are any syntax errors, type mismatches, or other issues that violate the language rules, the compiler will generate error messages, and the code won't be compiled until these issues are resolved.
– During compilation, the code is converted into an intermediate form called bytecode, which is a platform-independent representation of the source code. This bytecode is then executed by the Java Virtual Machine (JVM) during runtime.
– Là giai đoạn mà source code được compiler biên dịch thành code mà máy tính có thể thực thi (executable code).
Trong Java, đó là quá trình biên dịch file .java thành file .class.
Trong compile time, chỉ một số chức năng biên dịch được thực hiện và code không được đưa vào bộ nhớ để chạy mà chỉ hoạt động dưới dạng văn bản, chẳng hạn như kiểm tra lỗi (errors).
Run Time:
– Run time, also known as execution time, is the phase when the compiled bytecode is executed by the Java Virtual Machine. The JVM loads the classes and executes the methods as needed. During runtime, dynamic aspects of the program are evaluated, such as user inputs, calculations, and interactions with the environment.
– During runtime, if the program encounters runtime errors, such as division by zero, null pointer dereference, or exceptions that are not caught, the program may terminate abruptly or handle these errors based on exception handling mechanisms. These runtime errors are not detected by the compiler during compilation because they depend on the actual execution flow and data values.
– Là giai đoạn mà executable code bắt đầu chạy cho đến khi kết thúc chương trình.
Là giai đoạn mà code trên đĩa (disk) được đưa vào bộ nhớ và thực thi (execute).
=> In summary, compile time is the phase when the code is translated into bytecode and checked for syntactic and type-related issues, while run time is the phase when the compiled bytecode is executed by the JVM, and dynamic aspects of the program are evaluated.
Ví dụ về 2 định nghĩa trên
In this example, the missing semicolon is a compile-time error because it violates the syntax rules of Java. The division by zero, on the other hand, is a run-time error because it involves a specific computation that leads to an exceptional situation.
Tiếp tục quay lại với reflection:
Ví dụ cụ thể chúng ta có thể sửa private property của class:
Hình dung hệ thống phân cấp sẽ như sau:
java.lang.reflect
Object
Class Object là class gốc trong hệ thống phân lớp các class.
Mọi class đều là con của class Object, hay có thể nói class Object là class cha của toàn bộ các class.
Class
Class là class được cung cấp bởi package java.lang.Class.
Một instance của class Class đại diện cho toàn bộ các kiểu dữ liệu trong Java, bao gồm: các kiểu dữ liệu cơ bản (boolean, byte, char, short, int, long), void, array, class, interface, enumeration, annotation.
Class không có public constructor. Thay vào đó object của Class được tạo ra tự động bởi JVM trong quả trình tải class.
Khi sử dụng java reflection thì việc đầu tiên thường phải làm đó là có được một đối tượng kiểu Class, từ các đối tượng kiểu Class chúng ta có thể lấy được các thông tin về:
Có 3 cách để lấy object Class
Code minh hoạ:
Code minh hoạ đi lượm nhặt :v :
Sau đây là code minh hoạ về cách lấy field (set,get,..) và method, …
Cách set up và tạo class
File Main:
File Person:
Output:
Vậy tóm lại Java reflection sẽ làm được gì trong websec, đặc biệt là để áp dụng trong phần deserialize
Lý thuyết vậy đủ rồi chúng ta sẽ đi qua ví dụ về các bài CTF ở phần 2.
- https://tsublogs.wordpress.com
- https://sio.hashnode.dev/java-learning-6-reflection-api