The number of contours in the image, the size of the vector represents the number of points on the contour. Understand JavaBeans
The corresponding English word introspector is IntroSpector, which is mainly used to operate JavaBeans. JavaBean is a special Java class, and some methods meet certain naming rules. If some methods in a Java class meet certain naming rules, they can be used as JavaBeans.
JavaBean is a special Java class, mainly used to pass data information. The methods in this java class are mainly used to access private fields, and the method name complies with certain naming rules.
If you want to pass multiple information between two modules, you can encapsulate this information into a JavaBean, which is usually called a ValueObject (VO for short). This information is stored in the class using private fields. If the values of these fields are read or set, they need to be accessed through some corresponding methods. What do you think the names of these methods are good? The properties of JavaBean are determined based on the setter and getter methods in it, rather than based on the member variables in it. If the method is called setId, it means setting id in Chinese. As for which variable you store it on, will it work? If the method is called getId, it means to get id in Chinese. As for which variable you take it from, will it be useful? Remove the set prefix and the remaining part is the attribute name. If the second letter of the remaining part is lowercase, change the first letter of the remaining part to a small one.
For example:
The attribute name of setId() -->id
isLast()'s attribute name -->last
What is the attribute name of the setCPU? -->CPU
What is the attribute name of getUPS? -->UPS
In short, when a class is used as a javaBean, the properties of the JavaBean are inferred based on the method name, and it cannot see the member variables inside the java class at all.
A class that conforms to the characteristics of JavaBeans can be used as a normal class, but using it as a JavaBean will definitely bring some additional benefits before we can understand and apply JavaBeans! The benefits are as follows:
In JavaEE development, JavaBeans are often used. Many environments require operations in JavaBean mode. If others use and require this, then you have no choice!
JDK provides some APIs for operating JavaBeans, and this set of APIs is called introspection. If you want to access private x by yourself through the getX method, how to do it is difficult, right? Using introspection to operate JavaBeans is more convenient than using ordinary classes.
Simple introspection of JavaBean
The java.beans.PropertyDescriptor class is mainly used to obtain a JavaBean property in a certain Class object property set, and then call the getReadMethod() and getWriteMethod() methods to obtain the corresponding get and set methods.
Code example:
Domain class:
[cpp]viewplaincopy
intmain()
package usstc.lichunchun.bean;import java.util.Date;public class ReflectPoint { private Date birthday = new Date();private int x;public int y;public String str1 = "ball";public String str2 = "basketball";public String str3 = "itcast";public ReflectPoint(int x, int y) {super();this.x = x;this.y = y;}@Override public int hashCode() {final int prime = 31;int result = 1;result = prime * result + x;result = prime * result + y;return result;}@Override public Boolean equals(Object obj) {if (this == obj) return true;if (obj == null) return false;if (getClass() != obj.getClass()) return false;final ReflectPoint other = (ReflectPoint) obj;if (x != other.x) return false;if (y != other.y) return false;return true;}@Override public String toString(){return str1 + ":" + str2 + ":" + str3;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}Simple introspection operation:
package usstc.lichunchun.bean;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class IntroSpectorTest {public static void main(String[] args) throws Exception {ReflectPoint pt1 = new ReflectPoint(3, 5);String propertyName = "x";//"x"-->"X"-->"getX"-->MethodGetX--> getProperty(pt1, propertyName);Object value = 7;setProperty(pt1, propertyName, value);System.out.println(pt1.getX());}private static void setProperty(Object pt1, String propertyName, Object value) throws IntrospectionException, IllegalAccessException, InvocationTargetException {PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());Method methodSetX = pd.getWriteMethod();methodSetX.invoke(pt1, value);}private static Object getProperty(Object pt1, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException {PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());Method methodGetX = pd.getReadMethod();methodGetX.invoke(pt1);}}Complex introspective operations on JavaBeans
Use the method of traversing all BeanInfo's properties to find and set the x properties of a RefectPoint object. In the program, treat a class as a JavaBean, it means calling the IntroSpector.getBeanInfo method, and the resulting BeanInfo object encapsulates the result information of treating this class as JavaBean.
Complex introspective operation:
package usstc.lichunchun.bean;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class IntroSpectorTest {public static void main(String[] args) throws Exception {ReflectPoint pt1 = new ReflectPoint(3, 5);String propertyName = "x";//"x"-->"X"-->"getX"-->MethodGetX--> Object retVal = getProperty(pt1, propertyName);System.out.println(retVal);Object value = 7;setProperty(pt1, propertyName, value);System.out.println(pt1.getX());}private static void setProperty(Object pt1, String propertyName, Object value) throws IntrospectionException, IllegalAccessException, InvocationTargetException {PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());Method methodSetX = pd.getWriteMethod();methodSetX.invoke(pt1, value);}private static Object getProperty(Object pt1, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException {/* PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodGetX = pd.getReadMethod(); methodGetX.invoke(pt1); */BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();Object retVal = null;for (PropertyDescriptor pd: pds){if(pd.getName().equals(propertyName)){Method methodGetX = pd.getReadMethod();retVal = methodGetX.invoke(pt1);break;}}return retVal;}}}Manipulating JavaBeans using the BeanUtils toolkit
Based on the previous introspection example, use the BeanUtils class to get the original set property first, and then set it to a new value. The result returned when the get attribute is a string. When the set attribute, it can accept any type of objects, and usually strings are used.
Use the PropertyUtils class to get the original set property first, and then set it to a new value. The result returned when the get attribute is the original type of the attribute, and when the set attribute is only the original type of the attribute.
Note: Before using these two classes, you need to import the two jar packages commons-beanutils.jar and commons-logging-1.1.jar in the lib folder of the eclipse project, and AddtoBuildPath.
Code example:
package usstc.lichunchun.bean;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;public class IntroSpectorTest {public static void main(String[] args) throws Exception {ReflectPoint pt1 = new ReflectPoint(3, 5);String propertyName = "x";//"x"-->"X"-->"getX"-->MethodGetX--> Object retVal = getProperty(pt1, propertyName);System.out.println(retVal);Object value = 7;setProperty(pt1, propertyName, value);System.out.println(BeanUtils.getProperty(pt1, "x").getClass().getName());//String BeanUtils.setProperty(pt1, "x", "9");System.out.println(pt1.getX());/* Map map = {name:"zxx",age:18};//The new features of java7 BeanUtils.setProperty(map, "name", "lcc"); */BeanUtils.setProperty(pt1, "birthday.time", "111");//Support attribute chain System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));PropertyUtils.setProperty(pt1, "x", 23);System.out.println(PropertyUtils.getProperty(pt1, "x").getClass().getName());//Integer /* Difference between BeanUtils and PropertyUtils: BeanUtils operates JavaBeans in string form, can also operate Map classes, and JavaBeans and Map can be converted to each other (describe, populate) PropertyUtils operates with the data type of the JavaBean property itself*/}private static void setProperty(Object pt1, String propertyName, Object value) throws IntrospectionException, IllegalAccessException, InvocationTargetException {PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass());Method methodSetX = pd.getWriteMethod(); methodSetX.invoke(pt1, value);}private static Object getProperty(Object pt1, String propertyName) throws IntrospectionException, IllegalAccessException, InvocationTargetException {/* PropertyDescriptor pd = new PropertyDescriptor(propertyName, pt1.getClass()); Method methodGetX = pd.getReadMethod(); methodGetX.invoke(pt1); */BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();Object retVal = null;for (PropertyDescriptor pd : pds){if(pd.getName().equals(propertyName)){Method methodGetX = pd.getReadMethod();retVal = methodGetX.invoke(pt1);break;}}return retVal;}}}Summarize
The above is all about the analysis of Java introspective instances, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!