
1. preface
In the previous article, we encapsulated aMybatisUniversal Mapper. I use reflection to get entity class attributes. Most students also first felt that they could use reflection to achieve this. In fact, there is another technology that can also achieve it, which is introspection (Introspector)。
2. What is introspection
In computer science, introspection refers to the process of a computer program when it is running (Runtime) Inspection object (Object), which can also be commonly referred to as runtime type checking. Introspection should not be confused with reflection. Reflecting goes a step further than introspection, when a computer program is running (Runtime) An ability to access, detect, and modify its own state or behavior.
JavaIntrospection in the book is a right wayJavaBeanA default handling method for attribute,. Is it a little confusing after reading the concept? I am the same. So let's write an example to see. You need to understand before writingJavaBeanDefinition of;
-
Attributes are private.
-
with or without referencepublicConstruction method.
-
There are public statements for these attributesgetter/settermethod.
But if some classes are more "naughty", there aregetter/setterWithout a corresponding entity, it is easy to be introspective, but exceptions will be thrown. So abiding by the rules is a good way for us to use introspection.
in fact add/remove、is Also considered an operationJavaBeanAttribute methods, so we have to be careful.
3. Java introspective operations
JavaBeanGenerally used to transfer data, our database entity class is a typicalJavaBean。
public class UserInfo implements Serializable {
private static final long serialVersionUID = -8938650956516110149L;
private Long userId;
private String name;
private Integer age;
private String time;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Please note that I specifically did not givetime
property is setgetter/setter
。Next, I will demonstrate the use of introspection to manipulate entities.
Javabyjava.beans.Introspector
to perform introspective operations. Commonly used introspection operations include the following,Of course, there are other additional types。
3.1 BeanInfo
BeanInfo
Is an overall description of JavaBean by introspection. Here I just want to getUserInfo
, so it should be written like this:
BeanInfo beanInfo = Introspector.getBeanInfo(UserInfo.class,Object.class);
you can stop 、flag Two attributes control the depth of introspective analysis.
3.2 BeanDescriptor
and we'll seeBeanDescriptor
What are the main ones:
BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
// cn.felord.kono.entity.UserInfo.class
Class<?& gt; beanClass = beanDescriptor.getBeanClass();
// UserInfo
String name = beanDescriptor.getName();
turned out to beJavaBeantheClass
Type and name.
3.3 PropertyDescriptors
PropertyDescriptor
is to describeJavaBean, let's print it to have a look
Stream.of(beanInfo.getPropertyDescriptors())
.forEach(System.out::println);
java.beans.PropertyDescriptor[name=age; propertyType=class java.lang.Integer; readMethod=public java.lang.Integer cn.felord.kono.entity.UserInfo.getAge(); writeMethod=public void cn.felord.kono.entity.UserInfo.setAge(java.lang.Integer)]
java.beans.PropertyDescriptor[name=name; propertyType=class java.lang.String; readMethod=public java.lang.String cn.felord.kono.entity.UserInfo.getName(); writeMethod=public void cn.felord.kono.entity.UserInfo.setName(java.lang.String)]
java.beans.PropertyDescriptor[name=userId; propertyType=class java.lang.Long; readMethod=public java.lang.Long cn.felord.kono.entity.UserInfo.getUserId(); writeMethod=public void cn.felord.kono.entity.UserInfo.setUserId(java.lang.Long)]
originalPropertyDescriptor
Object that contains member attributesname、type、Method of reading、How to write。No. It doesn't includetime
Attribute because it does not havegetter/setterignored.
3.4 MethodDescriptors
As the name suggests, it must be a descriptionJavaBeanof the methods. A printout is displayed here.
java.beans.MethodDescriptor[name=foo; method=public static void cn.felord.kono.entity.UserInfo.foo(java.lang.String)]
Contains the method name and method object (Method
)。Notice the strange way to blend in here foo
, yes! I wrote a random method. MethodDescriptors can containJavaBeanUnder all methods, including static methods. Of course, it is limited by the depth of introspection.
3.5 EventSetDescriptors
Currently, the print is empty. I don't know what the functions of some paradigms related to publishing and subscribing JavaBean events are currently.
4. summary
JavaReflection obtains all information about a class at runtime and can manipulate the fields, methods, constructors, etc. of the class. It is very powerful. Introspection is actually a subset of reflection, based on reflection implementation. specialized operationsJavaBeanYes, only focus onJavaBeanSome properties of the properties, methods, and events of. Now that I understand this, I think it is necessary to put universalMapperRefactoring it.
Comments0