Statically loaded classes and dynamically loaded classes
-
Statically loaded classes: new creates objects that are statically loaded and need to load all possible classes at compile time
-
Dynamically loaded classes: loaded at run time
Reflection is a mechanism for dynamically loading classes
Second, the pros and cons of reflection
- Advantages: Runtime type determination, dynamic class loading: improved code flexibility, functionality can be modified without modifying the source code
- Disadvantages: Performance bottlenecks: security checks are required, reflection is equivalent to a series of interpretive operations, and is slower than direct Java code
Understand the nature of generics through reflection
1. Generics only take effect at compile time
Public class Test {public static void main(String[] args) {list1 = new ArrayList(); List<String> list2 = new ArrayList<String>(); System.out.println(list1.getClass()==list2.getClass()); }}Copy the code
Run result: true
Collection generics are used for type checking to avoid typing errors
List<String> list2 = new ArrayList<String>();
list2.add("a");
list2.add(20);
Copy the code
Compilation error: Int cannot be converted to java.lang.string
3. You can add elements of different types by reflection to bypass the generics check
List<String> list2 = new ArrayList<String>(); list2.add("a"); Class<? > c = list2.getClass(); Method method = c.getDeclaredMethod("add",Object.class); Method. The invoke (list2, 20); System.out.println(list2.size());Copy the code
Running result: 2
As you can see, since the type checking of generics only works at compile time, you can use the dynamic loading principle of reflection to bypass the checking of generics and add elements of different types to the collection
Fourth, the application of reflection
1. Load the database driver
// DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
Class.forName("com.mysql.cj.jdbc.Driver");
Copy the code
2. Load configuration files such as XML or properties
-
Spring loads beans through XML configuration schema
- Load any XML or Properties configuration files in your program into memory
- The Java class parses the contents of XML or Properties to get the bytecode string of the entity class and the associated attribute information
- Use reflection to get an instance of a Class based on this string
- Dynamically configure the properties of the instance
The configuration file
className=com.example.reflectdemo.TestInvoke methodName=printlnState Copy the code
Entity class
public class TestInvoke { private void printlnState(){ System.out.println("I am fine"); }}Copy the code
Parses the contents of the configuration file
// Parse the contents of XML or properties, Public static String getName(String Key) throws IOException {Properties Properties = new Properties(); FileInputStream in = new FileInputStream("D:\IdeaProjects\AllDemos\language-specification\src\main\resources\application.properties"); properties.load(in); in.close(); return properties.getProperty(key); }Copy the code
Use reflection to get the Class instance of the entity Class, create the instance object of the entity Class, and call the method
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, ClassNotFoundException, InstantiationException {// Use reflection to get the Class object Class<? > c = Class.forName(getName("className")); System.out.println(c.getSimpleName()); Method = c.getName ("methodName"); // Bypass the security check method.setaccessible (true); TestInvoke TestInvoke = (TestInvoke) c.newinstance (); Invoke (testInvoke); }Copy the code
Running results: