Simple summary of reflection
Reflection: obtain the relevant information of the Class to which the Object belongs through the Class Object of the Object, so as to realize various operations on the Class.
1, Method to get Class object
1. Class. Forname (fully qualified class name)
Fully qualified class name=Package name+Don't write the class name wrong here, otherwise it will be reported ClassNotFoundException For example: com.company.day23_reflect.Student Class personCls = Class.forName("com.company.reflectdemo.demo1.Person");
2. Class name.class
Class personCls2 = Person.class;
3. Object. getClass()
Person person = new Person(); Class personCls3 = person.getClass();
2, Use of reflection
Reflection is often used with configuration files. Here is a brief introduction to the use of the Properties class.
Member method
Return value | method | explain |
---|---|---|
void | load(InputStream inStream) | Read the attribute list (key value pair) from the input stream. |
void | load(Reader reader) | Read the attribute list (key value pair) from the input character stream in a simple line oriented format. |
String | getProperty(String key) | Search for attributes in this attribute list with the specified key. |
// Create Properties object Properties properties = new Properties(); //There are two loading methods for loading the properties configuration file. Generally, there is no Chinese. The first parameter is the load method of InputStream type. //If there are non ASCII characters in the configuration file, such as Chinese, you need to use the load method of the second Reader type parameter to specify the read character set properties.load(new InputStreamReader(new FileInputStream("dict.properties"),"gbk"));
Three aspects of information can be obtained through Class objects
1. Construction method
method | effect |
---|---|
getConstructor(Class<?>... parameterTypes) | Gets a public constructor object |
getConstructors() | Get all public constructor objects |
getDeclaredConstructor(Class<?>... parameterTypes) | Gets a constructor object |
getDeclaredConstructors() | Get all constructor objects |
2. Member variables
method | effect |
---|---|
getField(String name) | Gets a public member variable object |
getFields() | Get all public member variable objects |
getDeclaredField(String name) | Get a member variable object (including private) |
getDeclaredFields() | Get all member variable objects (including private) |
3. Membership method
method | effect |
---|---|
getMethod(String methodName, Class<?>... parameterTypes) | Get a public method |
getMethods() | Get all public methods |
getDeclaredMethod(String methodName, Class<?>... parameterTypes) | Get a method |
getDeclaredMethod() | Get all methods |
Setaccessible (true) is the method to release private permission restrictions
Simple summary
- Get type to get a public type object
- gets type to get all public type objects
- getDeclared type, get a type object (can be private)
- Getdeclarations type, get all types of objects (including private)
Simple to use: the method of executing objects by reading configuration files
//configuration file className=com.company.day23_reflect.Student methodName=doSomething public class Work2 { /** *This method creates a new object instance by reading the content of the configuration file, so as to execute the ordinary parameterless method of the object * @param configFilePath Represents the configuration file path */ public void callTargetMethod(String configFilePath) throws Exception { Properties properties = new Properties(); properties.load(new FileInputStream(configFilePath)); //Get the method name and object name. The class name in the configuration file needs to write the fully qualified class name String className = properties.getProperty("className"); String methodName = properties.getProperty("methodName"); //Get Class objects, default parameterless construction methods, and specify normal parameterless methods (methods should preferably be set to be accessible) Class clazz = Class.forName(className); Constructor constructor = clazz.getConstructor(); Method method = clazz.getMethod(methodName); method.setAccessible(true); Object o = constructor.newInstance(); method.invoke(o); } } //Test class //Try to write relative paths to reduce trouble public class Test2 { public static void main(String[] args) throws Exception{ new Work2().callTargetMethod("src\\com\\company\\reflectdemo\\dict.properties"); } }